xirukitepe
xirukitepe

Reputation: 1615

Logs outgoing emails in Rails

I have 2 models, User and Email

I have an after_create callback after user creation that sends a confirmation email to the email address of the user.

user.rb

after_create :create_confirmation_email

private

    def creation_confirmation_email
      UserMailer.account_create_confirmation(self).deliver
    end

The creation_confirmation_email above connects to the method in mailer/user_mailer.rb

user_mailer.rb

def account_create_confirmation(user)
    I18n.locale = 'pt'
    @user = user

    mail(from: '[email protected]', to: @user.email, subject: 'Bem-vindo ao Miigo') do |format|
      format.text
      format.html
    end
  end

My Email model has the following attributes:

reply_to: string
first_name: string
last_name :string
subject: string

Now what I want is an action that logs the confirmation email information after every delivery.

Can you suggest a workaround or callback for that?

any workarounds will be appreciated

Upvotes: 0

Views: 377

Answers (1)

Mike Campbell
Mike Campbell

Reputation: 7978

You could always write a callback in your ActionMailer class, this guide explains all that.

If you're using Rails 3, IIRC you can add callback functionality to ActionMailer classes by including AbstractController::Callbacks.

Something like:

class MyMailer < ActionMailer::Base
  include AbstractController::Callbacks

  after_filter :audit_email

  def account_create_confirmation(user)
    @subject = 'Bem-vindo ao Miigo'
    # ...
  end

  def audit_email
    EmailAudit.create(subject: @subject, ...)
  end
end

Upvotes: 1

Related Questions