Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

I want to send emails with sendgrids API from a google cloud platform instance. They have a short tutorial on how to do this but it has the email message, who its from, who to send to, and other info all in app.rb which isnt the normal way messages are sent in rails apps.

I looked at the sendgrid ruby docs and they also dont have very good information. All the info in in one place and they dont state what files to put them in or even mention any smtp settings to use.

Here is what I have so far

development.rb

config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
 :address              => "smtp.sendgrid.net",
 :port                 => 465,
 :domain               => "mydomain.com",
 :user_name            => ENV['sendgrid_username'],
 :password             => ENV['sendgrid_password'],
 :authentication       => :plain,
 :ssl                  => true,
 :enable_starttls_auto => true
}

gemfile

gem "sendgrid-ruby"

The email views are in app/views, the mailer methods are in app/mailer the same as normal rails 4 apps have it setup.

So I guess here are my main questions:

  1. Where do I call the environment variables holding the sendgrid api key?
  2. Where do I tell Action Mailer to use sendgrid or SendGrid::Mail as I've seen in a couple of places?
  3. Do I even need the sendgrid gem?
  4. Are the smtp settings correct for sending over SSL with sendgrid?

I'm new to sending emails like this in a rails app and would really appreciate the help.

like image 498
Rob Avatar asked Jul 04 '16 12:07

Rob


People also ask

Does SendGrid API use SMTP?

SendGrid provides an SMTP service that allows you to deliver your email via our servers instead of your own client or server. This means you can count on SendGrid's delivery at scale for your SMTP needs.

Does SendGrid store email body?

Data retention — SendGrid service If you're using the Twilio SendGrid service, we only hold email message bodies for as long as it takes to send them.


1 Answers

Put apikey as the name and the actual apikey as password:

config.action_mailer.smtp_settings = {
  address:        "smtp.sendgrid.net",
  port:           587,
  domain:         "yourwebsite.com",
  authentication: :plain,
  user_name:      'apikey',
  password:       Rails.application.secrets.sendgrid_api_key
}

Source

like image 90
horozco15 Avatar answered Oct 07 '22 15:10

horozco15