Skip to content

Commit

Permalink
Support tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
klizhentas committed Dec 2, 2010
1 parent 192e2e4 commit 7b3e718
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 6 additions & 1 deletion mailgun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def prepare_request(url_string)
class MailgunMessage
extend Mailgun::RequestBuilder

MAILGUN_TAG = 'X-Mailgun-Tag'

# Sends a MIME-formatted message
#
# raw_mime =
Expand All @@ -90,9 +92,12 @@ def self.send_raw(sender, recipients, raw_body, servername='')
# "Subject",
# "Hi!\nThis is message body")
#
def self.send_text(sender, recipients, subject, text, servername='')
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)
Expand Down
17 changes: 13 additions & 4 deletions samples/sending-messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@

# Sending a simple text message:
MailgunMessage::send_text("me@samples.mailgun.org",
"you@yourhost, 'Him' <you@mailgun.info>",
"Hello",
"Hi!\nI am sending the message using Mailgun Ruby API")
"you@yourhost, 'Him' <you@mailgun.info>",
"Hello text Ruby API",
"Hi!\nI am sending the message using Mailgun Ruby API")

# Sending a simple text message + tag
MailgunMessage::send_text("me@samples.mailgun.org",
"you@yourhost, 'Him' <you@mailgun.info>",
"Hello text Ruby API",
"Hi!\nI am sending the message using Mailgun Ruby API",
"",
{:headers => {MailgunMessage::MAILGUN_TAG => "sample_text_ruby"}})

# Sending a MIME message:
sender = "me@samples.mailgun.org"
receiver = "you@mailgun.info"
raw_mime =
"X-Mailgun-Tag: sample_raw_ruby" +
"Content-Type: text/plain;charset=utf-8\n" +
"From: #{sender}\n" +
"To: #{receiver}\n" +
"Content-Type: text/plain;charset=utf-8\n" +
"Subject: Hello!\n" +
"Subject: Hello raw Ruby API!\n" +
"\n" +
"Sending the message using Mailgun Ruby API"
MailgunMessage::send_raw(sender, receiver, raw_mime)
Expand Down

0 comments on commit 7b3e718

Please sign in to comment.