Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Sun May 21 2023

How to send emails in NodeJs with a Yahoo mail - nodemailer

In this article we will show how to send emails in NodeJs using nodemailer. We will also try to set up a Yahoo mail with third-party app access that we will use to send the mails with.

Problem with creating third-party app access on Yahoo

It seems that there is a known issue with setting up access on Yahoo for third-party apps, sometimes it says something like "This feature is currently not available". I got it to work on my email that is 1 year old, but not on a newly created. If you have issues, we have another guide to get it to work with gmail instead, that guide is a little bit longer, but at least it works. Find the guide here.

Set up Yahoo mail for third-party app access

Let's try to set up our Yahoo email to accept third-party app access. Navigate to the security page and click on Generate app password and follow the steps.

yahoo-settings

Create our mail service in NodeJs

First, we download nodemailer from with npm.

npm i nodemailer

Create a file called mailService.js in your project and add following code.

const nodeMailer = require("nodemailer"); const EMAIL = "email@yahoo.com"; const THIRD_PARTY_APP_PASSWORD = "ghdkgjdkgjdkvnfjd"; class MailService { _transporter; constructor() { this._transporter = nodeMailer.createTransport({ host: "smtp.mail.yahoo.com", port: 465, service: "yahoo", auth: { user: EMAIL, pass: THIRD_PARTY_APP_PASSWORD, }, }); } async sendMail(recipient, subject, text) { const mailOptions = { from: EMAIL, to: recipient, subject: subject, text: text, }; try { await this._transporter.sendMail(mailOptions); return { status: "SUCCESS" }; } catch (err) { return { error: err }; } } } module.exports = MailService;

There we go. So let's go through the code and see what we are doing here.

  • EMAIL constant is our sending email
  • THIRD_PARTY_APP_PASSWORD is our generated password from Yahoo
  • In our constructor we are setting up our transporter. We are telling it to use Yahoo's smtp server and 465 as port. We are also providng our credentials to authenticate towards Yahoo.
  • sendMail() function will send the message with our provided attributes such as recipient, subject and text.

Consume our service

Let's send some emails with our service. In another file, consume it like this:

const MailService = require("./mailService"); const service = new MailService(); const someFunction = async () => { const response = await service.sendMail( "myFriend@gmail.com", "My first email", "This mail was sent from my cool NodeJs service" ); if (response.error) { console.log("Something went wrong", response.error); return; } else if (response.status === "SUCCESS") { console.log("Message sent"); } };

Outro

In this guide, we created a service in NodeJs to send emails using Yahoo. As stated in the beginning, people are facing issues with Yahoo when it comes to creating app passwords for third-party apps. If you want to use Gmail instead, we have a guide for that as well. It requires more steps to get it to work, but when it is setup correct, it works really well. Read the guide here.

Hope you enjoyed this article, and thanks for reading!

signatureSun May 21 2023
See all our articles