> adonis install @adonisjs/mail
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
AdonisJs has first class support for sending email.
The Mail Provider supports a number of drivers including:
Smtp (smtp
)
Spark Post (sparkpost
)
Mailgun (mailgun
)
Amazon SES (ses
)
As the Mail Provider is not installed by default, we need to pull it from npm
:
> adonis install @adonisjs/mail
Next, register the provider inside the start/app.js
file:
const providers = [
'@adonisjs/mail/providers/MailProvider'
]
Mail configuration is saved inside the config/mail.js file, which is created by the adonis install command when installing the Mail Provider.
|
Let’s start with the basic example of sending email on user registration:
Route.post('user', 'UserController.store')
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) => {
message
.to(user.email)
.from('<from-email>')
.subject('Welcome to yardstick')
})
return 'Registered successfully'
}
}
module.exports = UserController
Finally, create the emails/welcome.edge
view file containing the HTML body:
<h2> Hello {{ username }} </h2>
<p>
Welcome to the yardstick club, here's your getting started guide
</p>
Below is the list of methods you can use to send emails.
Send email using one or many Edge views:
await Mail.send('view', data, (message) => {
message
.from('')
.to('')
})
The views
argument can be a single view or array of views per content type:
await Mail.send(['welcome', 'welcome.text'])
In the example above, the welcome
view is used for the HTML version of the email, while the welcome.text
view is used for the plain text version.
If you’re using Edge as your template engine, you can also use ‑text instead of .text as the plain text body template suffix.
|
Using template suffixes, you can also set the mail body for Apple watch:
await Mail.send(['welcome', 'welcome.text', 'welcome.watch'])
Use a raw string to send the mail (when the string is HTML the email HTML body will be set, otherwise just a plain text email will be sent):
await Mail.raw('plain text email', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})
await Mail.raw('<h1> HTML email </h1>', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})
Below is the list of methods you can use to build a mail message using the fluent message
API.
Set to
address:
message.to(user.email)
// with email and name both
message.to(user.email, user.name)
Set from
address:
message.from('team@yardstick.io')
// with email and name both
message.from('team@yardstick.io', 'Yardstick')
Add cc address to the email:
message.cc(user.email)
// with email and name both
message.cc(user.email, user.name)
Add bcc address to the email:
message.bcc(user.email)
// with email and name both
message.bcc(user.email, user.name)
You can call the above methods multiple times to define multiple addresses. |
Manually set the plain text body for the email:
message.text('Email plain text version')
Attach file(s) to the email:
message
.attach(Helpers.tmpPath('guides/getting-started.pdf'))
Set custom file name:
message
.attach(Helpers.tmpPath('guides/getting-started.pdf'), {
filename: 'Getting-Started.pdf'
})
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 an image into the HTML body using a content id:
message.embed(Helpers.publicPath('logo.png'), 'logo')
Then inside the template, you can say:
<img src="cid:logo" />
Ensure the cid is unique for each image in a given email.
|
Pass an object of values to the current driver:
message.driverExtras({ campaign_id: 20 })
The Mail Provider passes the object through to the driver, and it is up to the driver to consume these values.
The Mail Provider defines multiple connections inside the config/mail.js
file:
{
connection: 'smtp',
smtp: {},
sparkpost: {
driver: 'sparkpost',
apiKey: Env.get('SPARKPOST_API_KEY'),
extras: {}
}
}
Using the above configuration, you could switch to the sparkpost
connection via the connection
method like so:
await Mail
.connection('sparkpost')
.send('view', data, (message) => {
})
Below are configuration instructions relating to each specific driver.
The ses
driver requires the aws-sdk package.
Ensure to install it via npm
before using the ses
driver:
> npm i aws-sdk
The sparkpost
driver accepts an optional extras
configuration object:
{
extras: {
campaign_id: '',
options: {}
}
}
Check out SparkPost’s documentation to learn more about their available options.
You can also pass extras
at runtime using the driverExtras
method:
await Mail.send('view', data, (message) => {
message.driverExtras({
campaign_id: '',
options: {}
})
})
The mailgun
driver accepts an optional extras
configuration object:
{
extras: {
'o:tag': '',
'o:campaign': ''
}
}
Check out Mailgun’s documentation to learn more about their available options.
You can also pass extras
at runtime using the driverExtras
method:
await Mail.send('view', data, (message) => {
message.driverExtras({
'o:tag': '',
'o:campaign': ''
})
})