Email - mcordell/grape_token_auth GitHub Wiki

#Email

In order to send emails from GrapeTokenAuth (e.g. for password resets), email will have to be properly configured. Out of the box, GrapeTokenAuth supports simple SMTP mail for the mail gem, however the mailer and messages can also be configured and customized to your liking.

Simple SMTP setup

GrapeTokenAuth thinly wraps the mail gem's SMTP class which is used to send simple SMTP messages. The SMTP settings can be set during configuration as so:

GrapeTokenAuth.config do |config|
  config.smtp_configuration = {
    :address              => "smtp.me.com",
    :port                 => 587,
    :domain               => 'your.host.name',
    :user_name            => 'some username',
    :password             => 'some password',
    :authentication       => 'plain',
    :enable_starttls_auto => true
  }
end

Customizing the Messages

GrapeTokenAuth defines two standard messages, the password reset email and the confirmation message. The easiest way to override the message is to subclass the message class as so:

class MyResetEmail < GrapeTokenAuth::PasswordResetEmail
  TEXT_TEMPLATE = File.expand_path('../path_to_my_template.text.erb', __FILE__)
end

Here, the ERB template of the text message is being overridden. GrapeTokenAuth can be told to use this custom class during configuration as so:

GrapeTokenAuth.setup! do |config|
  config.messages = GrapeTokenAuth::Mail::DEFAULT_MESSAGES.merge(reset_password_instructions: MyResetEmail)
end

Overriding the Mailer

While sending messages with SMTP is nice, often we want to use other mail services. Creating a new mailer is straight forward, a mailer should define a class method send! which accepts a message object and a hash of options.

Typically, it is a good practice to utilize the html_body and text_body methods of the message object. An example of creating a mailer can be found here. Once the mailer is defined, GrapeTokenAuth can be instructed to use the mailer during setup:

GrapeTokenAuth.setup! do |config|
  config.mailer = MailgunMailer
end