17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/easy_smtp/smtp.rb', line 17
def send_email(email_data)
smtp_server = email_data['server'] || EasySMTP.config['smtp']['server']
smtp_port = email_data['port'] || EasySMTP.config['smtp']['port']
raise "SMTP server missing from config. Add the entry to the #{EasySMTP.config.path} \"smtp\": { \"server\": \"hostname_or_ip\" }" if smtp_server.nil? || smtp_server.empty?
email_data = Hashly.stringify_all_keys(email_data.dup)
part_boundary = 'PART_BOUNDARY'
leading_horizontal_whitespace = /^[^\S\n\r]+/
message = <<-MESSAGE_END.gsub(leading_horizontal_whitespace, '')
From: #{email_data['from_address']}
To: #{email_data['recipients']}
Cc: #{email_data['cc_recipients']}
Bcc: #{email_data['bcc_recipients']}
Subject: #{email_data['subject']}
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=#{part_boundary}
--#{part_boundary}
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=us-ascii
#{email_data['body']}
--#{part_boundary}
Content-Transfer-Encoding: 7bit
Content-Type: text/html; charset=us-ascii
#{email_data['html_body'] || email_data['body']}
MESSAGE_END
unless email_data['attachments'].nil?
email_data['attachments'].each do |attachment_path|
file_content = [File.binread(attachment_path)].pack('m') message += <<-ATTACHMENT_END.gsub(leading_horizontal_whitespace, '')
--#{part_boundary}
Content-Type: application/octet-stream; name="#{File.basename(attachment_path)}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{File.basename(attachment_path)}"; size=#{File.size(attachment_path)}
#{file_content}
ATTACHMENT_END
end
end
message += "--#{part_boundary}--"
Net::SMTP.start(smtp_server, smtp_port) do |smtp|
smtp.send_message message, email_data['from_address'], "#{email_data['recipients']};#{email_data['cc_recipients']};#{email_data['bcc_recipients']}".split(';').reject(&:empty?)
end
end
|