Sending Mails with Adonis Js and Mailgun

Elvis Onobo
2 min readSep 10, 2020

--

Almost every application being built today requires some communication, and if you are building an application right now, odds are you will need to implement some sort of communication with the user over e-mail at some point.

Adonis js is a Node js framework that makes it easy for developers to build applications incredibly fast and with relative ease. With Adonis js, you can easily send e-mails to users of your application using Mailgun e-mail service. How would you do that?

First you will need to add the Adonis Mail Provider by running the following:

adonis install @adonisjs/mail

After that, you need to register the Mail provider in the start/app.js file like so:

const providers = [
'@adonisjs/mail/providers/MailProvider'
]

Go over to the config/mail.js file where you would see something like this:

smtp: {driver: "smtp",pool: true,port: Env.get("SMTP_PORT", 2525),host: Env.get("SMTP_HOST"),secure: false,auth: {user: Env.get("MAIL_USERNAME"),pass: Env.get("MAIL_PASSWORD"),},maxConnections: 5,maxMessages: 100,rateLimit: 10,},

To be able to use this service you need a Mailgun account so head over here to start. After registering, click the ‘Sending’ drop-down and select ‘Overview’,

Under the Overview tab, select SMTP, your SMTP data will appear below. It looks like this:

Grab your SMTP credentials:SMTP hostname: smtp.mailgun.org
Port: 587 (recommended)
Username: postmaster@*************.mailgun.org
Default password: ******************

With your SMTP credentials you can now properly format your Mail Provider like so:

smtp: {driver: "smtp",pool: true,port: Env.get("SMTP_PORT", 587),host: Env.get("SMTP_HOST", "smtp.mailgun.org"),secure: false,auth: {user: Env.get("MAIL_USERNAME"),pass: Env.get("MAIL_PASSWORD"),},maxConnections: 5,maxMessages: 100,rateLimit: 10,},

You can also see that we are fetching the MAIL_USERNAME and MAIL_PASSWORD from .env so add it to your .env file just like below:

MAIL_USERNAME=postmaster@***********************.mailgun.orgMAIL_PASSWORD=*********************

Also, ensure that your Mail Provider connection is SMTP like this:

connection: Env.get("MAIL_CONNECTION", "smtp"),

With this, you can now send your E-mail using the Adonis Mail Provider

await Mail.send("emails.welcome", data, (message) => {message.to(user.email, user.name).from("noreply@my-website.com", "My Website").subject("Welcome to My Website");});

Don’t forget to use your Mail Provider in your controller, otherwise you would have yourself to blame, lol.

const Mail = use("Mail");

Now build something awesome!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Elvis Onobo
Elvis Onobo

Written by Elvis Onobo

Backend Engineer, making the world better one code block at a time

No responses yet

Write a response