For Developers By Developers

You can integrate Mailazy in minutes with your platform. Build and monitor your email solution on a trusted foundation with technical and strategic support when you need it the most.

Trusted by 120+ partners and delivering over 650 Million emails/month

Configure Order confirmation emails with SMTP for Ruby

Order confirmation emails is an important component of any email communication strategy. In order to ensure delivery of Order confirmation emails to your customers, reply on a trusted cloud-based email provider like Mailazy as a partner in your growth journey.

Prerequisites

You need to complete these given prerequisites, you can skip the step if you have already completed.

  1. Sign up for a Mailazy account.

  2. Complete Domain Authentication.

  3. Generate the Mailazy Access Key

Integrate Mailazy with Ruby on Rails

This example shows how to send an email for user signups. You can also check out this gem for more advanced features.

Setup ActionMailer

Let's generate a Mailer class. Mailer classes function as our controllers for email views.

$ rails generate mailer UserNotifierMailer

Now we open up the mailer we've just generated, app/mailers/user_notifier_mailer.rb and add a mailer action that sends users a signup email.

class UserNotifierMailer < ApplicationMailer
default :from => 'any_from_address@example.com'
# send a signup email to the user, pass in the user object that contains the user's email address
def send_signup_email(user)
@user  = user
mail( :to =>  @user.email,
:subject => 'Thanks for signing up for our amazing app' )
end
end

Now we need a view that corresponds to our action and outputs HTML for our email. Create a file app/views/user_notifier_mailer/send_signup_email.html.erb as follows:

    < !DOCTYPE html>
    < html>
        < head>
            < meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
        < /head>
        < body>
            < h1>Thanks for signing up, <%= @user.name %>!
            < p>Thanks for joining and have a great day! Now sign in and do awesome things!

< /body> < /html>

If you don't have a user model quite yet, generate one quickly.

$ rails generate scaffold user name email login
$ rake db:migrate

Now in the controller for the user model app/controllers/users_controller.rb, add a call to UserNotifierMailer.send_signup_email when a user is saved.

class UsersController < ApplicationController
    def create
        # Create the user from params
        @user  = User.new(user_params)
            if  @user.save
                # Deliver the signup email
                UserNotifierMailer.send_signup_email(@user).deliver
                redirect_to(@user, :notice => 'User created')
            else
                render :action => 'new'
            end
        end

    private
        def user_params
            params.require(:user).permit(:name, :email, :login)
        end
end

Alright, now we're cooking! Let's get it all going through Mailazy.

Configure ActionMailer to Use Mailazy

In config/environment.rb specify your ActionMailer settings to point to Mailazy's servers.

ActionMailer::Base.smtp_settings = {
:user_name => 'mailazy_apikey', # This is the string literal 'mailazy_apikey', NOT the ID of your API key
:password => 'mailazy_secret', # This is the secret mailazy secret key which was issued during API key creation
:address => 'smtp.mailazy.com',
:port =>  587,
:authentication => :plain,
:enable_starttls_auto =>  true
}

That's it! When a new user object is saved, an email will be sent to the user via Mailazy.

As a best practice, you should not store your credentials directly in the source but should instead store them in configuration files or environment variables. See this tutorial on environment variables in Rails. And for Rails Versions 5.2+ see Securely storing custom credentials in Rails.

Send transactional emails with Mailazy

Mailazy email delivery platform is powered by AI and uses the global infrastructure so that you can scale effortlessly. Mailazy is the trusted transactional email provider for hundreds of businesses and we are growing every day. We are known for being the most reliable and trusted senders on the internet.

Ready to get started?

Get started — it's free