Class: Lazylead::Postman

Inherits:
Object
  • Object
show all
Includes:
Emailing
Defined in:
lib/lazylead/postman.rb

Overview

TODO:

#/DEV Merge Smtp and Postman objects. There might be different postman’s based on different mail servers, thus its better to keep together the instantiation and sending. For each type of mail servers we should have separate object.

TODO:

#/DEV TestMail.deliveries -> store all emails to the files in /test/resources/testmailer/*.html. Right now after each test with email sending we taking the body and test it content. Quite ofter we need to check visual style in mails, etc, thus its better to store them on disk.

A postman to send emails.

Author

Yurii Dubinka ([email protected])

Copyright

Copyright © 2019-2020 Yurii Dubinka

License

MIT

Instance Method Summary collapse

Methods included from Emailing

#make_body, #split

Constructor Details

#initialize(log = Log.new) ⇒ Postman

Returns a new instance of Postman.



49
50
51
# File 'lib/lazylead/postman.rb', line 49

def initialize(log = Log.new)
  @log = log
end

Instance Method Details

#add_attachments(mail, opts) ⇒ Object



77
78
79
80
81
82
# File 'lib/lazylead/postman.rb', line 77

def add_attachments(mail, opts)
  return unless opts.key?("attachments") || opts.key?(:attachments)
  attach = opts["attachments"] || opts[:attachments]
  return if attach.nil?
  attach.select { |a| File.file? a }.each { |a| mail.add_file a }
end

#make_email(opts) ⇒ Object

Construct an email based on input arguments



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lazylead/postman.rb', line 62

def make_email(opts)
  mail = Mail.new
  mail.to opts[:to] || opts["to"]
  mail.from opts["from"]
  mail.cc opts["cc"] if opts.key? "cc"
  mail.subject opts["subject"]
  html = make_body(opts)
  mail.html_part do
    content_type "text/html; charset=UTF-8"
    body html
  end
  add_attachments mail, opts
  mail
end

#send(opts) ⇒ Object

Send an email.

:opts

the mail configuration like to, from, cc, subject, template.



55
56
57
58
59
# File 'lib/lazylead/postman.rb', line 55

def send(opts)
  mail = make_email(opts)
  mail.deliver
  @log.debug "#{__FILE__} sent '#{mail.subject}' to '#{mail.to}'."
end