Class: DcidevMailer::RailsMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/dcidev_mailer/rails_mailer.rb

Instance Method Summary collapse

Instance Method Details

#email(html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: nil, cc: nil, bcc: nil, from: nil, subject: "", template_path: "") ⇒ Object



12
13
14
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/dcidev_mailer/rails_mailer.rb', line 12

def email(html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: nil, cc: nil, bcc: nil, from: nil, subject: "", template_path: "")
  raise DcidevMailer::Errors::InvalidRecipients unless to.present?
  raise DcidevMailer::Errors::InvalidBody unless html_body.present? && html_body.is_a?(String)
  raise DcidevMailer::Errors::InvalidTemplate unless template_path.present?
  wording, images = DcidevMailer.format_image_from_html(html_body)

  locals = { wording: wording, header: nil, footer: nil }
  locals, _ = DcidevMailer.format_header_footer(header_url: header_url, footer_url: footer_url, locals: locals, images: images) if header_url.present? || footer_url.present?
  
  file_attachments = DcidevMailer.format_attachments(file_attachments) if file_attachments.present?
  if file_attachments.present?
    file_attachments.each do |a|
      attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
    end
  end

  begin
    header_file = File.read(DcidevUtility.download_to_file(header_url))
    header_mime = MimeMagic.by_magic(header_file)
    attachments.inline['header'] = header_file
    attachments.inline['header']['content-type'] = header_mime
    attachments.inline['header']['content-id'] = '<header>'
  rescue => _
  end

  begin
    footer_file = File.read(DcidevUtility.download_to_file(footer_url))
    footer_mime = MimeMagic.by_magic(footer_file)
    attachments.inline['footer'] = footer_file
    attachments.inline['footer']['content-type'] = footer_mime
    attachments.inline['footer']['content-id'] = '<footer>'
  rescue => _
  end

  mail(
    to: to,
    cc: cc,
    bcc: bcc,
    subject: subject,
    format: "text/html",
    from: from,
    # template_path: "dcidev_mailer/rails_mailer",
    # template_name: 'a',
  ) do |format|
    format.html {
      render locals: locals, html: ActionController::Base.new.render_to_string(template: template_path, locals: locals)

    }
  end
end