Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active November 4, 2016 09:29
Show Gist options
  • Save maxivak/24238d2c1437a3718d3e to your computer and use it in GitHub Desktop.
Save maxivak/24238d2c1437a3718d3e to your computer and use it in GitHub Desktop.
Rails 4 mailer

SMTP settings

in initializer

  # config/initializers/mailer.rb
  ActionMailer::Base.delivery_method = :smtp


  ActionMailer::Base.smtp_settings = {
    :address => "yoursite.com",
    :port => 587,
    :authentication => :plain,
    :user_name => "[email protected]",
    :password => 'password',
    :enable_starttls_auto => true,
    :openssl_verify_mode=> 'none',
  }

Dinamically set SMTP settings in Mailer class

  Spree::OrderMailer.class_eval do
        SMTP_SETTINGS = {
          :address => "yoursite.com",
          :port => 587,
          :authentication => :plain,
          :user_name => "[email protected]",
          :password => 'password',
          :enable_starttls_auto => true,
          :openssl_verify_mode=> 'none',
  
  
      }


        def my_email()
        
              set_smtp_settings
        
              mail(to: '[email protected]', subject: 'test email')
        end
  
  end

Store SMTP settings in secrets.yml

  • secrets.yml specify settings for each environment
development:
  ... other settings ..

  smtp:
    address: "myserver.com"
    port: 587
    domain: "mydomain.com"
    authentication: 'plain'
    user_name: "username"
    password: "mypwd"
    enable_starttls_auto: true

  • config/application.rb
    config.action_mailer.smtp_settings = {
        :address        => Rails.application.secrets.smtp['address'],
        :port           => Rails.application.secrets.smtp['port'],
        :domain         => Rails.application.secrets.smtp['domain'],
        :authentication => Rails.application.secrets.smtp['authentication'],
        :user_name      => Rails.application.secrets.smtp['user_name'],
        :password       => Rails.application.secrets.smtp['password'],
        :enable_starttls_auto => Rails.application.secrets.smtp['enable_starttls_auto']
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment