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: '[email protected]'
}
Send your email where needed
UserMailer.send_email(message, email).deliver