Hi Jeremy and future readers,
I'm posting the code I wrote following Jeremy's advice, in case it would be helpful for others.
```ruby
# app.rb
require_relative 'helpers/mail_helpers'
class App < Roda
plugin :rodauth do
enable :login, :logout, :reset_password # and other features omitted for brevity...
# Email Base
send_email { |mail| MailHelpers.send_email_with_sendgird(mail) } if ENV['RACK_ENV'] == "production"
# and other configuration stuffs
# ...
end
end
# helpers/mail_helpers.rb
module MailHelpers
include SendGrid
# IMPORTANT : It seems that the local variable 'mail' must be named 'mail' in order
# for Sendgrid to process the request correctly
def self.send_mail_with_sendgrid(original_mail)
from = SendGrid::Email.new(email: original_mail.from[0])
to = SendGrid::Email.new(email:
original_mail.to[0])
subject = original_mail.subject
content = SendGrid::Content.new(type: 'text/html', value: original_mail.body.raw_source)
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
end
end
```
Cheers,
Laurent