Class: MailgunMessage

Inherits:
Object
  • Object
show all
Extended by:
Mailgun::RequestBuilder
Defined in:
lib/howitzer/utils/email/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-8n” + “From: me@hostn” + “To: you@hostn” + “Subject: Hello!nn” + “Body” MailgunMessage::send_raw(“me@host”, “you@host”, raw_mime)



79
80
81
82
83
84
85
# File 'lib/howitzer/utils/email/mailgun.rb', line 79

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”)



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

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