Class: BRFMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/brf_mailer.rb

Overview

Delivers a mail to Brigitte with the current BRF file attached.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brf_filepath, month_name) ⇒ BRFMailer

Returns a new instance of BRFMailer.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brf_mailer.rb', line 11

def initialize(brf_filepath, month_name)
  @brf_filepath = brf_filepath
  @month_name   = month_name

  # Load mailer config.
  config.mailer_config.each do |k, v|
    send("#{k}=", v) if respond_to? k
  end

  f = Tempfile.new 'body'
  f.write body
  f.rewind
  system "#{config.text_editor} #{f.path}"
  f.rewind
  @body = f.read
ensure
  f.close
end

Instance Attribute Details

#bccObject

Returns the value of attribute bcc


8
9
10
# File 'lib/brf_mailer.rb', line 8

def bcc
  @bcc
end

#bodyObject

Returns the value of attribute body


8
9
10
# File 'lib/brf_mailer.rb', line 8

def body
  @body
end

#brf_filepathObject

Returns the value of attribute brf_filepath


8
9
10
# File 'lib/brf_mailer.rb', line 8

def brf_filepath
  @brf_filepath
end

#ccObject

Returns the value of attribute cc


8
9
10
# File 'lib/brf_mailer.rb', line 8

def cc
  @cc
end

#month_nameObject

Returns the value of attribute month_name


8
9
10
# File 'lib/brf_mailer.rb', line 8

def month_name
  @month_name
end

#receiverObject

Returns the value of attribute receiver


8
9
10
# File 'lib/brf_mailer.rb', line 8

def receiver
  @receiver
end

#smtp_domainObject

Returns the value of attribute smtp_domain


8
9
10
# File 'lib/brf_mailer.rb', line 8

def smtp_domain
  @smtp_domain
end

#smtp_portObject

Returns the value of attribute smtp_port


8
9
10
# File 'lib/brf_mailer.rb', line 8

def smtp_port
  @smtp_port
end

#smtp_pwObject

Returns the value of attribute smtp_pw


8
9
10
# File 'lib/brf_mailer.rb', line 8

def smtp_pw
  @smtp_pw
end

#smtp_serverObject

Returns the value of attribute smtp_server


8
9
10
# File 'lib/brf_mailer.rb', line 8

def smtp_server
  @smtp_server
end

#smtp_userObject

Returns the value of attribute smtp_user


8
9
10
# File 'lib/brf_mailer.rb', line 8

def smtp_user
  @smtp_user
end

Instance Method Details

#configObject


81
82
83
# File 'lib/brf_mailer.rb', line 81

def config
  Punch.config
end

#deliverObject


73
74
75
76
77
78
79
# File 'lib/brf_mailer.rb', line 73

def deliver
  smtp = Net::SMTP.new smtp_server, smtp_port
  smtp.enable_ssl
  smtp.start(smtp_domain, smtp_user, smtp_pw, :plain) do |sender|
    sender.send_message(message, smtp_user, receiver, cc)
  end
end

#message(encode_attachment = true) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/brf_mailer.rb', line 43

def message(encode_attachment = true)
  boundary = "superUniqueIdentifier567"
  filename = File.basename brf_filepath
  file_content = File.read brf_filepath
  # Base64
  encoded_brf_file_content = [file_content].pack "m"

<<EOM
From: #{smtp_user}
To: #{receiver}
Cc: #{cc}
Subject: Stunden #{month_name.capitalize}
MIME-VERSION: 1.0
Content-Type: multipart/mixed; boundary=#{boundary}
--#{boundary}
Content-Type: text/plain
Content-Transfer-Encoding:8bit

#{body}

--#{boundary}
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"

#{encode_attachment ? encoded_brf_file_content : file_content}
--#{boundary}--
EOM
end