Skip to content

Instantly share code, notes, and snippets.

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'

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

  = @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: '',
    port: 25,
    domain: '',
    authentication: :plain,
    user_name: ENV['SENGRID_USERNAME'],
    password: ENV['SENGRID_PASSWORD']
  config.action_mailer.default_options = {
    from: ''

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.