Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActionMailer with SendGrid

I'm using SendGrid to send emails on Heroku...

The problem so far is while it works great on Heroku, on my local host it fails.

Right now I have SendGrig install here, config/setup_mail.rb:

ActionMailer::Base.smtp_settings = {
  :address        => "smtp.sendgrid.net",
  :port           => "25",
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => ENV['SENDGRID_DOMAIN']
}

What's a Heroku/SendGrid way to allow me to make sure my mailers work in DEV. Is this setup_mail.rb file a good thing? Should it be in the env file? Any other thoughts?

Thanks

like image 862
AnApprentice Avatar asked Oct 18 '10 00:10

AnApprentice


People also ask

How do I connect to SendGrid?

There are two ways to connect to SendGrid. The easy way is to just connect your username and password, but you can also create a SendGrid API key and set the credentials with the key instead. (Thanks to Kevin Berthier for the code snippet!)

What is the difference between mail and Action Mailer?

mail - Creates the actual email itself. You can pass in headers as a hash to the mail method as a parameter. mail will create an email — either plain text or multipart — depending on what email templates you have defined. Action Mailer makes it very easy to add attachments.

Why should I use SendGrid instead of postmark?

I like SendGrid, because unlike PostMark, or many other similar services, you can sign up with a regular Gmail email account. It doesn’t require you to already have a domain with an email (which is great if you are still testing, don’t have MX servers set up yet, or if you are using a 3rd party service like Heroku ).

How do I access the mailer in actionmailer?

There are also some Action Mailer-specific helper methods available in ActionMailer::MailHelper. For example, these allow accessing the mailer instance from your view with mailer, and accessing the message as message: <%= stylesheet_link_tag mailer.name.underscore %> <h1><%= message.subject %></h1>


2 Answers

Using config/environments/[development.rb | production.rb] as tfe mentioned above sounds like its the way to go. Just put the ActionMailer configuration in either of those files and change it to suit the development|production environment.

You can also find your SendGrid credentials used by Heroku by issuing the following command:

heroku config --long

These credentials are used for all SendGrid authentication (SMTP Auth, Website login to view stats, etc., API access)

-- Joe

SendGrid

like image 96
joescharf Avatar answered Nov 05 '22 20:11

joescharf


Just set environment variables on your development environment for SENDGRID_USERNAME, SENDGRID_PASSWORD, and SENDGRID_DOMAIN. Then it will work.

You can get the correct values for these from your Heroku app. Open heroku console and get the values of ENV['SENDGRID_USERNAME'] and so on.

Or just use a different set of SMTP settings locally. Or use sendmail or something.

like image 43
tfe Avatar answered Nov 05 '22 21:11

tfe