Friday, 7 November 2014

Sending emails in Rails using sendgrid

In most of the application you need to send bulk or single emails on a periodic time.like newsletter or digest or any other thing.for sending emails in your rails application you use SMTP servers to send emails.here is an example of sending emails by using sendgrid:

1). First of all you have to create a sendgrid account and make it provisioned.for sign-up in send grid you have to give a website(domain name) for the provision.if your website doesn't have enough content then your account can't be provisioned.
you will not able to send emails from sendgrid if your account is not provisioned.  

2). Goto https://sendgrid.com/developer after you logged in to your sendgrid account and copy SMTP settings.

3). In your environment.rb write the following settings:

 # Action mailer settings
  config.action_mailer.default_url_options = {:host => 'your_host_name(domain_name)'}

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :user_name => 'user_name',
      :password => 'your_password',
      :address => 'smtp.sendgrid.net',
      :port => 587,
      :authentication => :plain,
      :enable_starttls_auto => true
  }

now you emails sent from your application will be delivered via sendgrid.com

Handling Email Activity: 

you can login to your sendgrid account and see email activities.

1). Handling Bounce Emails: if you want to handle bounced emails then there are two ways to do:
a). Forword emails to a perticular email_id: if you want to send the bounced emails to another email-id then follow these steps:

click Email Reports ->Bounces -> settings

in the setting page click the check box and give the email-id on which you want to receive the bounced emails.

2). Handle post request from sendgrid: The other way to enable Event Notification and click on settings. on the settings page give an url and select events. when any of the selected event occur sendgrid send a post request on the given url with all possible parameters(ex. bounced,drop email information's). 

the url which you give above have to manage the post request.


No comments:

Post a Comment