iachat/lib/mail/resend_provider.rb
Gabriel Jablonski 5b7fc9ae77 feat: add Resend email delivery method and configuration (#11)
* feat: add Resend email delivery method and configuration

* chore: simplify ResendProvider initialization by removing settings parameter

* test: update ResendProvider initialization in tests by removing unnecessary settings parameter

* chore: improvements
2025-04-03 23:28:38 -03:00

30 lines
910 B
Ruby

module Mail # rubocop:disable Style/ClassAndModuleChildren
class ResendProvider
class DeliveryError < StandardError; end
def initialize(_settings); end # rubocop:disable Style/RedundantInitialize
def deliver!(mail)
Resend::Emails.send(
from: mail.smtp_envelope_from,
to: mail.smtp_envelope_to,
subject: mail.subject,
html: mail.decoded,
text: sanitize_html(mail.decoded)
)
rescue Resend::Error => e
raise DeliveryError, "Failed to send email: #{e.message}"
rescue StandardError => e
raise DeliveryError, "An error occurred while sending email: #{e.message}"
end
private
def sanitize_html(html)
sanitized = ActionView::Base.full_sanitizer.sanitize(html)
# NOTE: Remove more than two consecutive newlines
sanitized.lines.map(&:strip).join("\n").gsub(/\n{3,}/, "\n\n").strip
end
end
end