You are viewing the legacy version of AdonisJS. Visit for newer docs. This version will receive security patches until the end of 2021.


AdonisJs has first class support for sending emails. The mail provider comes with a handful of drivers to send emails, which includes.

  • Smtp ( smtp )

  • Spark Post ( sparkpost )

  • Mailgun ( mailgun )

  • Amazon SES ( ses )


The mail provider is not part of the default project structure and must be installed as defined in following steps.

adonis install @adonisjs/mail

Next, register the provider inside start/app.js file.

const providers = [

The configuration is saved inside config/mail.js file. Make sure to update the settings as per your needs.

Basic Example

Let’s start with the basic example of sending email on user registration.'user', '')
const Mail = use('Mail')

class UserController {

  async store ({ request }) {
    const data = request.only(['email', 'username', 'password'])
    const user = await User.create(data)

    await Mail.send('emails.welcome', user.toJSON(), (message) => {
        .subject('Welcome to yardstick')

    return 'Registered successfully'

module.exports = UserController

Finally, we need to create emails/welcome.edge view, which is the HTML body.

<h2> Hello {{ username }} </h2>
  Welcome to the yardstick club, here's your getting started guide

Mail API

Below is the list of methods you can use to send emails.

send(views, data, callback)

Send email using one or multiple Edge views.

await Mail.send('view', data, (message) => {

The views can be a single view or an array of views for a specific content type.

await Mail.send(['welcome', 'welcome.text'])

The view ending with .text is used as the plain text body of the email. In the same way, you can set the mail body for Apple watch.

await Mail.send(['welcome', 'welcome.text', ''])

raw(body, callback)

Use raw string to send the mail. The body value is considered as the plain text email.

await Mail.raw('... everyone who got this message.', (message) => {
  message.subject('Hello ...')

Message API

Below is the list of methods you can use to build message using fluent message API.

to(address, [name])

Set to address.

// with email and name both,

from(address, [name])

Set from address.


// with email and name both
message.from('', 'Yardstick')

cc(address, [name])

Add cc address on the email

// with email and name both,

bcc(address, [name])

Add bcc address on the email.


// with email and name both

Also, you can call the above methods for multiple times to defined multiple addresses.

replyTo(address, [name])

Set replyTo email address



Set email message id



Set email subject

message.subject('Welcome to yardstick')


Manually set the plain text body for the email.

message.text('Email plain text version')

attach(filePath, options)

Attach file(s) with email.


Set custom file name

  .attach(Helpers.tmpPath('guides/getting-started.pdf'), {
    filename: 'Getting-Started.pdf'

attachData(data, filename, options)

Attach raw data as a String, Buffer or Stream.

message.attachData('hello', 'hello.txt')

// buffer
message.attachData(new Buffer('hello'), 'hello.txt')

// stream
message.attachData(fs.createReadStream('hello.txt'), 'hello.txt')

embed(filePath, cid, options)

Embed image into HTML body using content id. Make sure the cid is unique for each image for a given email.

message.embed(Helpers.publicPath('logo.png'), 'logo')

Later inside the template, you can say

<img src="cid:logo" />


Pass an object of values to the current driver. The mail provider passes the object to the driver, and it is up to the driver to consume these values.

message.driverExtras({ campaign_id: 20 })

Switching connections

Mail provider allows defining multiple connections inside config/mail.js file. Connections can be for same or a different driver.

  connection: 'smtp',

  smtp: {},

  sparkpost: {
    driver: 'sparkpost',
    apiKey: Env.get('SPARKPOST_API_KEY'),
    extras: {}

Using the above config, you can switch to sparkpost connection using the connection method.

await Mail
  .send('view', data, (message) => {


Below sections are instructions related to specific drivers.


The ses driver relies on aws-sdk. Make sure to install the dependencies from npm, since it is installed by default.

npm i aws-sdk


You can pass following extra options when using sparkpost as the driver.

Via config

  extras: {
    campaign_id: '',
    options: {}

Check out sparkpost options attributes docs to learn more about available options.

Also, you can pass extras at runtime using the driverExtras method.

await Mail.send('view', data, (message) => {
    campaign_id: '',
    options: {}


You can pass following extra options when using mailgun as the driver.

Via config

  extras: {
    'o:tag': '',
    'o:campaign': '',

Check out mailgun docs to learn more about available options.

Also, you can pass extras at runtime using the driverExtras method.

await Mail.send('view', data, (message) => {
    'o:tag': '',
    'o:campaign': ''