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

3 views
Skip to first unread message

Rob via StackOverflow

unread,
Jul 4, 2016, 8:34:07 AM7/4/16
to google-appengin...@googlegroups.com

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.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/38184833/how-do-i-send-emails-using-sendgrids-api-to-send-emails-with-rails-4-i-can-seem

Marko Manojlovic via StackOverflow

unread,
Jul 4, 2016, 2:39:03 PM7/4/16
to google-appengin...@googlegroups.com
  1. Use Figaro gem: https://github.com/laserlemon/figaro It will generate an application.yml file for you, where you can store your sendgrid_username and sendgrid_password.

  2. Have you generated your mailer? For example, generate mailer user_notifier where you can define your a default-from email, and some methods like this:

r

# send a signup email to the user, pass in the user object that contains the 
user email address
default :from => 'he...@yourdomain.com'
def send_signup_email(user)
    @user = user
    mail( :to => @user.email,
        :subject => 'Thanks for signing up!'
    )
end
  1. No, you don't need it.
  2. https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/38184833/how-do-i-send-emails-using-sendgrids-api-to-send-emails-with-rails-4-i-can-seem/38190548#38190548
Reply all
Reply to author
Forward
0 new messages