Skip to content

Latest commit

 

History

History
107 lines (81 loc) · 3.89 KB

Messages.md

File metadata and controls

107 lines (81 loc) · 3.89 KB

Mailgun - Messages

This is the Mailgun Ruby Message utilities.

The below assumes you've already installed the Mailgun Ruby SDK in to your project. If not, go back to the master README for instructions.

There are two utilities included, Message Builder and Batch Message.

Message Builder: Allows you to build a message object by calling methods for each MIME attribute. Batch Message: Inherits Message Builder and allows you to iterate through recipients from a list. Messages will fire after the 1,000th recipient has been added.

Usage - Message Builder

Here's how to use Message Builder to build your Message.

# First, instantiate the Mailgun Client with your API key
mg_client = Mailgun::Client.new("your-api-key")
mb_obj = Mailgun::MessageBuilder.new

# Define the from address.
mb_obj.set_from_address("[email protected]", {"first"=>"Ruby", "last" => "SDK"})

# Define a to recipient.
mb_obj.add_recipient(:to, "[email protected]", {"first" => "John", "last" => "Doe"})

# Define a cc recipient.
mb_obj.add_recipient(:cc, "[email protected]", {"first" => "Sally", "last" => "Doe"})

# Define the subject.
mb_obj.set_subject("A message from the Ruby SDK using Message Builder!")

# Define the body of the message.
mb_obj.set_text_body("This is the text body of the message!")

# Define the HTML text of the message
mb_obj.set_html_body("<html><body><p>This is the text body of the message</p></body></html>")

# Set the Message-Id header. Pass in a valid Message-Id.
mb_obj.set_message_id("<[email protected]>")

# Clear the Message-Id header. Pass in nil or empty string.
mb_obj.set_message_id(nil)
mb_obj.set_message_id('')

# Other Optional Parameters.
mb_obj.add_campaign_id("My-Awesome-Campaign")
mb_obj.header("Customer-Id", "12345")
mb_obj.add_attachment("./tron.jpg")
mb_obj.set_delivery_time("tomorrow 8:00AM PST")
mb_obj.set_click_tracking(true)

# Send your message through the client
mg_client.send_message("sending_domain.com", mb_obj)

Usage - Batch Message

Here's how to use Batch Message to easily handle batch sending jobs.

# First, instantiate the Mailgun Client with your API key
mg_client = Mailgun::Client.new("your-api-key")
mb_obj = Mailgun::BatchMessage.new(mg_client, "example.com")

# Define the from address.
mb_obj.set_from_address("[email protected]", {"first"=>"Ruby", "last" => "SDK"})

# Define the subject.
mb_obj.set_subject("A message from the Ruby SDK using Message Builder!")

# Define the body of the message.
mb_obj.set_text_body("Hello %recipient.first%,
                     This is the text body of the message
                     using recipient variables!
                     If you need to include custom data,
                     you could do it like this: %recipient.account-id%.")

mb_obj.add_recipient(:to, "[email protected]", {"first"      => "John",
                                                   "last"       => "Doe",
                                                   "account-id" => 1234})

mb_obj.add_recipient(:to, "[email protected]", {"first"      => "Jane",
                                                   "last"       => "Doe",
                                                   "account-id" => 5678})

mb_obj.add_recipient(:to, "[email protected]", {"first"       => "Bob",
                                                  "last"        => "Doe",
                                                  "account-id"  => 91011})
...
mb_obj.add_recipient(:to, "[email protected]", {"first"      => "Sally",
                                                    "last"       => "Doe",
                                                    "account-id" => 121314})

# Send your message through the client
message_ids = mb_obj.finalize

More Documentation

See the official Mailgun Docs for more information.