Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.