Skip to content

Instantly share code, notes, and snippets.

@jpbalarini
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbalarini/22ef5d8c3e91d54433ff to your computer and use it in GitHub Desktop.
Save jpbalarini/22ef5d8c3e91d54433ff to your computer and use it in GitHub Desktop.
Setting up email with Sendgrid

Generate the user mailer class

rails generate mailer UserMailer

Define the method for sending emails

class UserMailer < ActionMailer::Base
  include SendGrid
  sendgrid_category :use_subject_lines
  sendgrid_enable   :ganalytics, :opentrack

  def send_email(message, email)
    @message = message
    mail to: email, subject: 'Your subject here'
  end
end

Generate the view for the method in views/user_mailer/send_email.html.haml

%p
  = @message

Set up each config/environment/ENVIRONMENT.rb file with the following

config.action_mailer.default_url_options = { host: ENV['ENVIRONMENT_URL'] }
config.action_mailer.raise_delivery_errors = true

Set up config/application.rb

  ActionMailer::Base.smtp_settings = {
    address: 'smtp.sendgrid.net',
    port: 25,
    domain: 'www.YOURDOMAIN.com',
    authentication: :plain,
    user_name: ENV['SENGRID_USERNAME'],
    password: ENV['SENGRID_PASSWORD']
  }
  config.action_mailer.default_options = {
    from: 'no-reply@YOURDOMAIN.com'
  }

Send your email where needed

UserMailer.send_email(message, email).deliver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment