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']



15
16
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
# File 'lib/easy_smtp/smtp.rb', line 15

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 = <<-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') # Encode contents into base64
      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