Module: Railgun

Defined in:
lib/railgun.rb,
lib/railgun/errors.rb,
lib/railgun/mailer.rb,
lib/railgun/railtie.rb,
lib/railgun/attachment.rb

Defined Under Namespace

Classes: Attachment, ConfigurationError, Error, InternalError, Mailer, Railtie

Class Method Summary collapse

Class Method Details

.build_message_object(mail) ⇒ Object

Acts on a Rails/ActionMailer message object and uses Mailgun::MessageBuilder to construct a new message.

Parameters:


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/railgun/mailer.rb', line 99

def build_message_object(mail)
  mb = Mailgun::MessageBuilder.new

  mb.from mail[:from]
  mb.reply_to(mail[:reply_to].to_s) if mail[:reply_to].present?
  mb.subject mail.subject
  mb.body_html extract_body_html(mail)
  mb.body_text extract_body_text(mail)

  [:to, :cc, :bcc].each do |rcpt_type|
    addrs = mail[rcpt_type] || nil
    case addrs
    when String
      # Likely a single recipient
      mb.add_recipient rcpt_type.to_s, addrs
    when Array
      addrs.each do |addr|
        mb.add_recipient rcpt_type.to_s, addr
      end
    when Mail::Field
      mb.add_recipient rcpt_type.to_s, addrs.to_s
    end
  end

  return mb.message if mail.attachments.empty?

  mail.attachments.each do |attach|
    attach = Attachment.new(attach, encoding: 'ascii-8bit', inline: attach.inline?)
    attach.attach_to_message! mb
  end

  return mb.message
end

.extract_body_html(mail) ⇒ String

Returns the decoded HTML body from the Mail::Message object if available, otherwise nil.

Parameters:

Returns:

  • (String)

139
140
141
142
143
144
145
# File 'lib/railgun/mailer.rb', line 139

def extract_body_html(mail)
  begin
    (mail.html_part || mail).body.decoded || nil
  rescue
    nil
  end
end

.extract_body_text(mail) ⇒ String

Returns the decoded text body from the Mail::Message object if it is available, otherwise nil.

Parameters:

Returns:

  • (String)

153
154
155
156
157
158
159
# File 'lib/railgun/mailer.rb', line 153

def extract_body_text(mail)
  begin
    (mail.text_part || mail).body.decoded || nil
  rescue
    nil
  end
end

.transform_for_mailgun(mail) ⇒ Hash

Performs a series of transformations on the `mailgun*` attributes. After prefixing them with the proper option type, they are added to the message hash where they will then be sent to the API as JSON.

Parameters:

Returns:

  • (Hash)

    transformed message hash


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/railgun/mailer.rb', line 64

def transform_for_mailgun(mail)
  message = build_message_object(mail)

  # v:* attributes (variables)
  mail.mailgun_variables.try(:each) do |k, v|
    message["v:#{k}"] = v
  end

  # o:* attributes (options)
  mail.mailgun_options.try(:each) do |k, v|
    message["o:#{k}"] = v
  end

  # h:* attributes (headers)
  mail.mailgun_headers.try(:each) do |k, v|
    message["h:#{k}"] = v
  end

  # recipient variables
  message['recipient-variables'] = mail.mailgun_recipient_variables.to_json if mail.mailgun_recipient_variables

  # reject blank values
  message.delete_if do |k, v|
    v.nil? or (v.respond_to?(:empty) and v.empty?)
  end

  return message
end