SMTP

Rails (SMTP)

Send email from Rails through MailBlastr using Action Mailer over SMTP.

Rails' Action Mailer sends over SMTP. Configure its smtp_settings to use the MailBlastr relay and authenticate with your API key — then send mailers as usual. SMTP sends appear in your dashboard and event log just like API sends.

Prerequisites

1. Configure your environment

Add these lines to your environment config file, reading the API key from ENV.

config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address   => 'smtp.mailblastr.com',
  :port      => 465,
  :user_name => 'mailblastr',
  :password  => ENV['MAILBLASTR_API_KEY'],
  :tls => true
}

2. Create a mailer

Define a UserMailer. The default from: must be on a domain verified with MailBlastr.

app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
  default from: 'Acme <onboarding@yourdomain.com>' # this domain must be verified with MailBlastr
  def welcome_email
    @user = params[:user]
    @url = 'http://example.com/login'
    mail(to: ["delivered@example.com"], subject: 'hello world')
  end
end

And create the matching ERB email template.

app/views/user_mailer/welcome_email.html.erb
<!doctype html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>You have successfully signed up to example.com,</p>
    <p>To log in to the site, just follow this link: <%= @url %>.</p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

3. Send the email

Build the mailer instance and deliver it.

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

mailer.deliver_now!
Use port 587 with :enable_starttls_auto => true instead of :tls => true to send over STARTTLS. See Send an email for the HTTP API.