I am working on Ruby on Rails applications. We choose the AWS SES service to send emails to our users. We are using the aws-sdk-ses gem for this purpose.
And I'm faced with a problem with previewing emails.
My user_mailer.rb has the next code:
# frozen_string_literal: true
class UserMailer < ApplicationMailer
layout 'mailer'
def invite
invitation = params[:invitation]
email = invitation.email
subject = 'Invitation email'
# The HTML body of the email
htmlbody = render_to_string(
template: 'user_mailer/invite',
layout: true,
locals: {
email: receiver_email,
}
)
send_email(
receiver: receiver_email,
sender: 'default@email.com',
subject: subject,
body: htmlbody
)
end
def send_email(receiver:, sender:, subject:, body:)
AWS_SES_CLIENT.send_email(
{
destination: { to_addresses: [receiver] },
message: {
body: {
html: { charset: 'UTF-8',
data: body }
},
subject: {
charset: 'UTF-8',
data: subject
}
},
source: sender
}
)
rescue Aws::SES::Errors::ServiceError => e
Rails.logger.info "ERROR! Email for #{receiver} with subject #{subject} wasn't sent with error message: #{e}"
end
end
And my spec/mailers/preview/user_mailer_preview.rb has the next code:
# frozen_string_literal: true
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview
def invite
UserMailer.with(invitation: UserInvitation.first).invite
end
end
But when I go to the http://localhost:3000/rails/mailers/user_mailer I see the message
You are trying to preview an email that does not have any content. This is probably because the mail method has not been called in user_mailer#invite.
I suggest that standard Rails previewer track the calling the mail method, but I'm used custom send_email method to send email. So it doesn't work. I'm trying to find the solution on the AWS SDK guide but I didn't. I suppose it needs to reconfigure action_mailer in some way.