Sending Emails with MailerSend and Ruby

Install MailerSend Ruby SDK

Install the MailerSend Ruby SDK using the following command:

gem install mailersend-ruby

Set up API token

Create a .env file in your project root directory (if you don't have one already) and add your MAILERSEND_API_TOKEN to it. You can find an example of how the MAILERSEND_API_TOKEN should look like in the .env.example file.

MAILERSEND_API_TOKEN=YOUR_API_TOKEN_HERE

Alternatively, you can set the MAILERSEND_API_TOKEN variable system-wide, which is useful if you're using Docker/Kubernetes.

Import the mailersend-ruby library

Import the mailersend-ruby library in your Ruby file.

require "mailersend-ruby"

Create an instance of the Mailersend::Email class

Create an instance of the Mailersend::Email class.

ms_email = Mailersend::Email.new

Add necessary parameters

Add the necessary parameters to the email using the add_recipients, add_from, add_subject, add_text, and add_html methods.

ms_email.add_recipients("email" => "ron@parksandrec.com", "name" => "Ron")
ms_email.add_recipients("email" => "leslie@parksandrec.com", "name" => "Leslie")
ms_email.add_from("email" => "april@parksandrec.com", "name" => "April")
ms_email.add_subject("Time")
ms_email.add_text("Time is money, money is power, power is pizza, and pizza is knowledge. Let's go.")
ms_email.add_html("<b>Time is money, money is power, power is pizza, and pizza is knowledge. Let's go.</b>")

Send the email

Send the email using the send method.

ms_email.send

That's it! You should now be able to send emails using the MailerSend Ruby SDK.

What’s next?

Last Updated: