Class: MailgunMessage

Inherits:
Object
  • Object
show all
Extended by:
Mailgun::RequestBuilder
Defined in:
lib/mailgun.rb

Constant Summary collapse

MAILGUN_TAG =
'X-Mailgun-Tag'

Class Method Summary collapse

Methods included from Mailgun::RequestBuilder

prepare_request

Class Method Details

.send_raw(sender, recipients, raw_body, servername = '') ⇒ Object

Sends a MIME-formatted message

raw_mime =
  "Content-Type: text/plain;charset=utf-8\n" +
  "From: me@host\n" + 
  "To: you@host\n" + 
  "Subject: Hello!\n\n" +
  "Body"
MailgunMessage::send_raw("me@host", "you@host", raw_mime)


80
81
82
83
84
85
86
# File 'lib/mailgun.rb', line 80

def self.send_raw(sender, recipients, raw_body, servername='')
  uri_str = "#{MailgunResource.site}messages.eml?api_key=#{MailgunResource.password}&servername=#{servername}"
  http, url = prepare_request(uri_str)
  data = "#{sender}\n#{recipients}\n\n#{raw_body}"
  res = http.post(url, data, {"Content-type" => "text/plain" })
  Mailgun::handle_response(res)
end

.send_text(sender, recipients, subject, text, servername = '', options = nil) ⇒ Object

Sends a plain-text message

MailgunMessage::send_text(“[email protected]”,

"[email protected]",
"Subject",
"Hi!\nThis is message body")


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mailgun.rb', line 95

def self.send_text(sender, recipients, subject, text, servername='', options = nil)
  uri_str = "#{MailgunResource.site}messages.txt?api_key=#{MailgunResource.password}&servername=#{servername}"
  params = { :sender => sender, :recipients => recipients, :subject => subject, :body => text}
  unless options.nil?
    params['options'] = ActiveSupport::JSON.encode(options)
  end
  http, url = prepare_request(uri_str)
  req = Net::HTTP::Post.new(url)
  req.set_form_data(params)
  res = http.request(req)
  Mailgun::handle_response(res)
end