Rails : Common SMTP settings

Gmail

# config/initializers/smtp_settings.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :user_name            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

 

SendGrid

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings   = {
    :address              => 'smtp.sendgrid.net',
    :port                 => '587',
    :user_name            => ENV['smtp_username'],
    :password             => ENV['smtp_password'],
    :authentication       => 'plain',
    :enable_starttls_auto => true
}

Many hosting providers and ISPs block port 25 as a default practice. When trying to connect to smtp.sendgrid.net remember that ports 25, 2525, 587, and 465 are all available for use.

We recommend port 587 to avoid any rate limiting that your server host may apply.

Setting attributes and their meaning

Allows detailed configuration for :smtp delivery method:

  • :address – Allows you to use a remote mail server. Just change it from its default "localhost" setting.
  • :port – On the off chance that your mail server doesn’t run on port 25, you can change it.
  • :domain – If you need to specify a HELO domain, you can do it here.
  • :user_name – If your mail server requires authentication, set the username in this setting.
  • :password – If your mail server requires authentication, set the password in this setting.
  • :authentication – If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain (will send the password in the clear), :login (will send password Base64 encoded) or :cram_md5 (combines a Challenge/Response mechanism to exchange information and a cryptographic Message Digest 5 algorithm to hash important information)
  • :enable_starttls_auto – Detects if STARTTLS is enabled in your SMTP server and starts to use it. Defaults to true.
  • :openssl_verify_mode – When using TLS, you can set how OpenSSL checks the certificate. This is really useful if you need to validate a self-signed and/or a wildcard certificate. You can use the name of an OpenSSL verify constant (‘none’, ‘peer’, ‘client_once’, ‘fail_if_no_peer_cert’) or directly the constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER, …).

 

Sources

http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration

https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

https://cbabhusal.wordpress.com/2014/09/29/easiest-way-to-send-email-via-rails-action-mailer/

Advertisements

Ruby On Rails : Engineyard : ActionMailer’s asset_url helper method not working as expected

I have been using Engineyard as PaaS for deploying my Ruby On Rails application. To have my app server intact I have created a utility instance called ‘redis’ to host Redis-Server and Sidekiq. Continue reading

Ruby On Rails: ActionMailer : Get full URL to an image in a Rails asynchronous mailer

Well this is odd behavior, but I’ve resolved this issue. It turns out that unless the action_mailer.asset_host starts with http:// then it will be ignored. There is a Regex in actionpack/lib/action_view/helpers/asset_url_helper.rb that defines a valid ActionMailer URI:

URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

However, Continue reading

Easiest way to send EMail via Rails : Action mailer

It’s very easy to send Email via rails 3 and above. You just need to write few lines of codes and you are done. There are two ways you want to send email

  1. You can just send email via any Email address you wish, eg: someone@example.com
  2. Next is, you can send email via email servers of any services you are registered with, such as Google Mail, Outlook, sendgrid etc

The First way: Sending Email from anonymous Email Address

Create a file names setup_mail.rb in config/initializers/ directory and write the following code

#config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = {
 :address => "smtp.ntc.net.np",
 :port => 25
 }

#smtp.ntc.net.np is the smtp address of my ISP, see this for your ISP in your ISP's website

#25 is default port number for SMTP, see if it is different for your's ISP

Then generate a mailer ( Mailer is just like controller). Write following code to generate mailer

rails generate mailer mailer_name

eg: rails g mailer user_mailer

This will create a file names user_mailer.rb in app/mailers directory and user_mailer directory in app/views directory

replace the codes in the file with following

#app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  def registration_confirmation(user)
    mail(:to => 'destination@example.com', :subject => "Newly Registered", :from => "sourceemail@example.com")
  end
end

#'destination@example.com' can be replace with user.email if you have sent User object via method call

Like controllers, mailers also have views associated with it in views directory. No file is deafulty created in views directory, you just need to create one. Create a file named registration_confirmation.html.erb in user_mailer directory and write text you want your user to see in email. Like

<h1>Thank you dear for registering</h1>
<p>The new one</p>

You can also embed ruby code as in other views. You can also render these views to see Email Preview.

Now you need to trigger the rails to send the Email. You can do this by invoking the action you created in mailer. In this  case

UserMailer.registration_confirmation(@user).deliver

Example

#app/controllers/user_controller.rb

def create
@user = User.new(params[:user])

if @user.save
UserMailer.registration_confirmation(@user).deliver
end
end

To send Email via Google, or Microsoft Mail servers, you need to change settings in setup_mail.rb file

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

For Outlook just change the attributes according to the vendor