Integrations

Rails SMTP

Send emails in Rails using our SMTP relay
Developer
Price

Free up to 3,000 emails/month

Last Updated

24/08/2023

Integration Support

Contact us via the chat beacon or our support email.

Prerequisites

To get the most out of this guide, you’ll need to:

1. Setup your environment

Add these lines of code into your environment config file:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address   => 'smtp.mailersend.net',
  :port      => 587,
  :user_name => 'XXX@verified-domain.com',
  :password  => ENV['MAILERSEND_API_KEY'],
  :starttls => true
}

2. Send email using Rails Action Mailer

Then create a UserMailer class definition:

class UserMailer < ApplicationMailer
  default from: 'Jane <hello@verified-domain.com>' # this domain must be verified on your MailerSend dashboard
  def welcome_email
    @user = params[:user]
    @url = 'https://mailersend.com/login'
    mail(to: ["jane@mailersend.com"], subject: 'Hello from MailerSend!'
  end
end

And create your ERB email template:

<!doctype html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <b>This is just a friendly hello from your friends at {$company}.</b>
  </body>
</html>

Initialize your UserMailer class. This should return a UserMailer instance.

u = User.new name: "Jane"
mailer = UserMailer.with(user: u).welcome_email

Finally, you can now send emails using the deliver_now! method:

mailer.deliver_now!