๐Ÿ“งTransactional Emails

Step 1: Create an Account on Postmark

  1. Visit the Postmark Website: Go to Postmark's official website.

  2. Sign Up: Click on the โ€œSign Upโ€ button typically located at the top right of the homepage.

  3. Fill in Your Details: Enter your email address, password, and any other required information to create a new account.

  4. Confirm Your Email: Check your inbox for a confirmation email from Postmark. Click the link in the email to verify your account.

  5. Set Up Your Account: Log in to your newly created account and complete the setup process as prompted. This may include providing additional information about your business or intended use of the service. โ—๏ธ Make sure to mention that you are using postmark for transactional emails (welcome emails, payment confirmation etc...) and in NO case for promotional emails.

Step 2: Add Your Domain

Once your account is set up, you need to verify and add your domain:

  1. Navigate to the Domains Section: In your Postmark dashboard, find the "Domains" section.

  2. Add Your Domain: Click the โ€œAdd Domainโ€ button and follow the instructions to verify your domain. This usually involves adding DNS records provided by Postmark to your domain's DNS settings in Namecheap or other domain providers.

Step 3: Configure Your Application

After your domain is set up, configure your application:

  1. Add Your Domain Name: Open your application.rb file and add your domain name as necessary.

    config.action_mailer.delivery_method     = :postmark
    config.action_mailer.postmark_settings   = { api_token: ENV['POSTMARK_API_KEY'] }
    config.action_mailer.default_url_options = { host: "www.yourdomain.io" }
  1. For Development, we don't want to use our Postmark credits. So we will use the gem Letter Opener instead, which is already installed by default in LR. Open development.rb and add this line:

config.action_mailer.delivery_method = :letter_oppener

The Gem will open a new .html tab with the view of the email. Use it for styling ๐ŸŽจ

  1. Set Your API Key: Create a .env file in the root of your project (if it doesn't already exist) and add your Postmark API key. You can find your API key in the Postmark dashboard under the โ€œAPI Tokensโ€ section.

# .env
POSTMARK_API_KEY=your_postmark_api_key

Step 4 (optional): Examples of use

Create a welcome mailer to trigger after a user creates an account:

rails generate mailer UserMailer

Change the sender's default email:

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout "mailer"
end

This will create a few files under the mailers and views folder. Go to user_mailer.rb and write the following code:

# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
  def welcome(user)
    mail(to: user.email, subject: 'You are in :)')
  end
end

Then create the mailer view:

<p>Hi!</p>

<p>Thank you for signing up to LightningRails. After <%= link_to "purchasing your first license", checkout_url %>  you will be able to download the boilerplate from the source repository and start building your app.</p>

<p>Visit the <a href="https://docs.lightningrails.com">documentation</a> to get started and learn how to implement the different features (authentication, payments, etc), how to use the different components, and how to deploy your app.</p>

<p>Cheers!</p>
<p>Dani, maker of LightningRailsยฉ</p>

In user.rb we want to create a callback method to automatically send the welcome email as soon as the user is created.

class User < ApplicationRecord
    after_create :send_welcome_email # <-- Dont forget this ๐Ÿ‘€
    
    [...]
    
    private
    
    def send_welcome_email
        UserMailer.welcome(self).deliver_now
    end
end

Et voila! Your users will receive transactional emails, so make sure to watch out for your free credits, you should have 100 emails per month for free, enough to get your first MVP users ๐Ÿš€

Last updated