Class: TMailMail
Instance Method Summary
collapse
Methods inherited from BaseMail
#default_from, #default_subject, #send_email
Instance Method Details
#create_attachment(file_name) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/libisi/mail/tmail.rb', line 94
def create_attachment(file_name)
file = Pathname.new(file_name)
raise "File not readable: #{file_name}" unless file.readable?
$log.info("Creating attachment #{file_name}")
attachment = TMail::Mail.new
if file_name =~ /bz2/ or file_name =~ /bzip2/
attachment.body = Base64.encode64(Pathname.new(file_name).readlines.join)
file_name = file.basename
else
$log.info("Compressing attachment to bz2")
command = "|cat #{file_name.to_s.inspect} | bzip2"
$log.debug{"Compression command is: #{command.inspect}"}
compr = open(command) {|f| f.readlines.join}
raise "Error compressing attachment." unless $?.success?
$log.info("Compressed file has length #{compr.length}")
attachment.body = Base64.encode64(compr)
file_name = "#{file.basename}.bz2"
end
$log.debug("Attaching file #{file_name}")
attachment.transfer_encoding = '7bit'
attachment.encoding = 'base64'
attachment.set_content_type('application', 'x-bzip', 'name' => "#{file_name.to_s}")
attachment.["Content-Disposition"] = "attachment; filename=#{file_name.to_s}"
attachment
end
|
#create_text_part(text, content_type = "plain") ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/libisi/mail/tmail.rb', line 85
def create_text_part(text, content_type = "plain")
text_part = TMail::Mail.new
text_part.body = text
text_part.charset = 'utf-8'
text_part.transfer_encoding = '7bit'
text_part.set_content_type('text', content_type)
text_part
end
|
#send_mail(recipients, text, options = {}) ⇒ Object
42
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
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/libisi/mail/tmail.rb', line 42
def send_mail(recipients, text, options = {})
mail = TMail::Mail.new()
mail.date = Time.now
mail.to = recipients
mail.from = (options[:from] or self.default_from)
mail.subject = (options[:subject] or self.default_subject)
if (ccs = options[:cc])
$log.debug("Sending mail cc to #{ccs.inspect}")
mail.cc = ccs
end
if (bccs = options[:bcc])
$log.debug("Sending mail bcc to #{bccs.inspect}")
mail.bcc = bccs
end
mail.mime_version = "1.0"
text = text.readlines.join("\n") if text.respond_to?(:readlines)
if text =~ /\<html/
$log.debug("Expecting text has content type html")
mail.parts.push(create_text_part(text, "html"))
else
$log.debug("Expecting text has content type text")
mail.parts.push(create_text_part(text))
end
(options[:attachments] or []).each {|a|
mail.parts.push(create_attachment(a))
}
mail.set_content_type( 'multipart', 'mixed' )
mail.message_id = TMail.new_message_id(full_qualified_domainname)
Net::SMTP.start("localhost",25) do |smtpclient|
$log.info("Sending mail with subject #{mail.subject.to_s.inspect} to #{mail.to.to_s.inspect}")
smtpclient.send_message(mail.to_s, mail.from, mail.to)
end
end
|