Skip to main content
Version: Next

Create emailing backends

Marten lets you easily create custom emailing backends that you can then use as part of your application when it comes to sending emails.

Basic backend definition

Defining an emailing backend is as simple as creating a class that inherits from the Marten::Emailing::Backend::Base abstract class and that implements a unique #deliver method. This method takes a single email argument (instance of Marten::Emailing::Email), corresponding to the email to deliver.

For example:

class CustomEmailingBackend < Marten::Emailing::Backend::Base
def deliver(email : Email)
# Deliver the email!
end
end

Handling attachments

If your backend supports attachments, you can iterate over #attachments in the #deliver implementation:

class CustomEmailingBackend < Marten::Emailing::Backend::Base
def deliver(email : Email)
email.attachments.each do |attachment|
# attachment.filename
# attachment.mime_type
# attachment.content
end
end
end

Each attachment exposes:

  • a filename
  • a mime_type
  • a content byte slice
  • a size in bytes

Enabling the use of custom emailing backends

Custom emailing backends can be used by assigning an instance of the corresponding class to the emailing.backend setting.

For example:

config.emailing.backend = CustomEmailingBackend.new