Using Amazon SES with Rails ActionMailer

前端 未结 10 1358
南旧
南旧 2020-12-04 09:42

What would be the best way to go about making ActionMailer send mail via Amazon SES in Rails 3?

Edit:

This is now a gem:

gem install amazon-         


        
相关标签:
10条回答
  • 2020-12-04 10:04

    Configuring your Rails application with Amazon SES

    set action_mailer.perform_deliveries to true as it is set to false by default in the development/production environment

    config.action_mailer.perform_deliveries = true
    

    then paste this code in your development/production environment

    config.action_mailer.smtp_settings = {
      :address => ENV["SES_SMTP_ADDRESS"],
      :port => 587,
      :user_name => ENV["SES_SMTP_USERNAME"], 
      :password => ENV["SES_SMTP_PASSWORD"],
      :authentication => :login,
      :enable_starttls_auto => true
    }
    
    0 讨论(0)
  • 2020-12-04 10:09

    Setting up Rails 3.2 for sending emails using Amazon's Simple Email Service (SES) is easy. You do not require any additional gem or monkey patching to make it work.

    SES supports both STARTTLS over SMTP as well as TLS/SSL. The following demonstrates how to set up Rails for STARTTLS with SES.

    Prerequisites

    1. If you are running rails Mac OS X, you may need to configure OpenSSL for Ruby correctly before you can use STARTTLS. If you are using Ruby 1.9.3 and RVM, here is one way to do this:

      rvm pkg install openssl
      rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr 
      

    If you do not do this, there is a possibility that Ruby will segfault when you try to send an email.

    1. Make sure you have verified your sender email address with AWS. You can only send emails with a verified email address as the sender. Go to the "Verified Senders" option on the left menu in AWS console for SES.

    2. Make sure you have the AWS SMTP user name and password for authentication. Go to the "SMTP Settings" option on the left menu in AWS console for SES to set this up. You will first be prompted to create an IAM user (default: ses-smtp-user) and then you will be shown the SMTP user and password, which look like usual AWS key and secret. Note that the IAM user, i.e., ses-smtp-user is not the SMTP user that you will be using for authentication.

    Configuring Rails

    In config/environments/development.rb and config/environments/production.rb, add the following:

      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
          :address => "email-smtp.us-east-1.amazonaws.com",
          :port => 587, # Port 25 is throttled on AWS
          :user_name => "...", # Your SMTP user here.
          :password => "...", # Your SMTP password here.
          :authentication => :login,
          :enable_starttls_auto => true
      }
    

    Sending an email

    This is it. Now you can go ahead and create a mailer and start sending emails for fun and profit!

    Create a sample mailer

    rails g mailer user_mailer
    

    In app/mailer/user_mailer.rb:

        class UserMailer < ActionMailer::Base
          # Make sure to set this to your verified sender!
          default from: "your@verifiedsender.com"  
    
          def test(email)
            mail(:to => email, :subject => "Hello World!")
          end
        end 
    

    In views/user_mailer/test.erb:

        A quick brown fox jumped over the lazy dog.
    

    Now, launch the console and shoot off a test email:

        rails c
    
        Loading development environment (Rails 3.2.1)
        1.9.3p125 :001 > UserMailer.test("your@email.com").deliver
    
    0 讨论(0)
  • 2020-12-04 10:10

    I use the following gem:

    https://github.com/aws/aws-sdk-rails

    It pulls in the standard aws-sdk, plus allows to set ActionMailer to use AWS SES. Example:

    # config/production.rb
    # ...
    config.action_mailer.delivery_method     = :aws_sdk
    
    0 讨论(0)
  • 2020-12-04 10:13

    For TLS SSL setup [Recommended by Amazon SES]

    Spoiler Alert: NO GEM Required

    smtp is defualt way of sending email in rails but you can add this line to explicitly define in config/application.rb file

    config.action_mailer.delivery_method = :smtp
    

    In config/application.rb or you can specify in certain environment file

    config.action_mailer.smtp_settings = {
        address: 'Amazon SES SMTP HOSTNAME',
        port: 465,   #TLS port
        domain: 'example.com',
        user_name: 'SMTP_USERNAME',
        password: 'SMTP_PASSWORD',
        authentication: 'plain',   #you can also use login
        ssl: true,   #For TLS SSL connection
    }
    

    The Amazon SES SMTP HOSTNAME is specific for every region, so you that name which you are in, following are hostnames wrt regions.

    1. email-smtp.us-east-1.amazonaws.com (for region us-east-1)
    2. email-smtp.us-west-2.amazonaws.com (for region us-west-2)
    3. email-smtp.eu-west-1.amazonaws.com (for region eu-west-1)

    StackOverFlow | Amazon-getting-started-send-using-smtp

    0 讨论(0)
提交回复
热议问题