Module: EasySMTP

Defined in:
lib/easy_smtp/smtp.rb,
lib/easy_smtp/config.rb

Class Method Summary collapse

Class Method Details

.configObject



4
5
6
# File 'lib/easy_smtp/config.rb', line 4

def config
  @config ||= EasyJSON.config(defaults: defaults)
end

.defaultsObject



8
9
10
11
12
13
14
15
# File 'lib/easy_smtp/config.rb', line 8

def defaults
  {
    'smtp' => {
      'server' => nil,
      'port' => 25,
    },
  }
end

.send_email(email_data) ⇒ Object

Example parameter for send_email method email_data =

recipients: '[email protected];[email protected]',
cc_recipients: '[email protected]', # (optional)
bcc_recipients: '[email protected]', # (optional)
from_address: '[email protected]',
subject: 'Subject of this email',
body: 'Content of the email!',
html_body: '<html><head></head><body>Content of the email in HTML format!</body></html>',
attachments: ['Array of paths to files']



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
64
65
# File 'lib/easy_smtp/smtp.rb', line 19

def send_email(email_data)
  smtp_server = EasySMTP.config['smtp']['server']
  smtp_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 = "              From: \#{email_data['from_address']}\n              To: \#{email_data['recipients']}\n              Cc: \#{email_data['cc_recipients']}\n              Bcc: \#{email_data['bcc_recipients']}\n              Subject: \#{email_data['subject']}\n              Mime-Version: 1.0\n              Content-Type: multipart/mixed; boundary=\#{part_boundary}\n              --\#{part_boundary}\n              Content-Transfer-Encoding: 7bit\n              Content-Type: text/plain; charset=us-ascii\n\n              \#{email_data['body']}\n              --\#{part_boundary}\n              Content-Transfer-Encoding: 7bit\n              Content-Type: text/html; charset=us-ascii\n\n              \#{email_data['html_body'] || email_data['body']}\n            MESSAGE_END\n\n  unless email_data['attachments'].nil?\n    email_data['attachments'].each do |attachment_path|\n      file_content = [File.binread(attachment_path)].pack('m') # Encode contents into base64\n      message += <<-ATTACHMENT_END.gsub(leading_horizontal_whitespace, '')\n        --\#{part_boundary}\n        Content-Type: application/octet-stream; name=\"\#{File.basename(attachment_path)}\"\n        Content-Transfer-Encoding:base64\n        Content-Disposition: attachment; filename=\"\#{File.basename(attachment_path)}\"; size=\#{File.size(attachment_path)}\n\n        \#{file_content}\n      ATTACHMENT_END\n    end\n  end\n  message += \"--\#{part_boundary}--\"\n\n  Net::SMTP.start(smtp_server, smtp_port) do |smtp|\n    smtp.send_message message, email_data['from_address'], \"\#{email_data['recipients']};\#{email_data['cc_recipients']};\#{email_data['bcc_recipients']}\".split(';').reject(&:empty?)\n  end\nend\n".gsub(leading_horizontal_whitespace, '')