# Sending emails 📧

---
## SMPT
**Simple Mail Transfer Protocol (SMTP)** is a technology for sending outgoing emails across networks and is the most common transport method. It serves as a relay service to send email from one server to another.
---
When you send an email to a friend using an email client like Gmail, an outgoing (SMTP) server picks it up and connects with your friend's receiving server. The two servers communicate using guidelines defined by the SMTP protocol, determining who the recipient is and how they can receive the incoming mail. Email clients usually have an SMTP server associated with them to aid in email delivery.
---
### Advantages of using SMTP
The major advantage of SMTP is that it's widely adopted and easy to set up and integrate in a web application. Email service providers might have more features, but using them also means relying on a third-party intermediary to deliver your emails. With SMTP, you get fine-grained control over every aspect of your email sending.
---
### Drawbacks of using SMTP
The major drawback of SMTP is it can be insecure and easily hacked. The standard SMTP protocol is susceptible to DDoS attacks, phishing, and data breaches. If you decide to use your own email SMTP server, you will be responsible for long-term server maintenance, which requires a lot of ongoing effort to maintain securely.
---
Sending emails with SMTP is also much slower than using an API service. SMTP requires extensive back-and-forth between mail SMTP servers to deliver a message. Even then the email may fail to deliver without feedback if the server's IP address is blacklisted or a firewall has blocked a port. This back-and-forth also means multiple points of failure.
---
## How to send emails with Nodemailer and SMTP
Nodemailer is a Node.js module used for sending emails, and is the most popular Node.js email package. You can use Nodemailer to create HTML or plain-text emails, add attachments, and send your emails through different transport methods, including built-in SMTP support. It requires Node.js 6.0 or newer.
---
The first step is to create a Node.js application:
`mkdir email-nodeapp && cd email-nodeapp
npm init -y`
---
Next, install the Nodemailer module:
`npm install nodemailer`
---
Nodemailer’s `createTransport` function specifies which method you want to use for sending email. It takes the connection data and credential as an argument. In this case, since SMTP is the preferred transport, you will need to define an SMTP host, port, and credential password for accessing a host SMTP server.
---
To get a host URL, you need an SMTP server. For development purposes, I used *Mailtrap* to serve as a fake SMTP server.
A fake SMTP server lets you avoid cluttering your real account with multiple tests while still seeing how your test emails behave – do all the buttons work the way they’re supposed to, is the formatting still correct after sending, and so on.
---
In the Integrations dropdown on the dashboard, select Nodemailer and copy the credentials displayed.

---
Create an `email.js`

---
You need to insert the host, user, and password with the Mailtrap credentials you copied from the dashboard above. Now you can send an email using the `sendMail` method of Nodemailer's `createTransport` function.
---
To test that it works, go to your terminal and run:
`node email.js`

{"metaMigratedAt":"2023-06-16T12:42:37.240Z","metaMigratedFrom":"Content","title":"Sending emails 📧","breaks":true,"contributors":"[{\"id\":\"173c5ca6-dd9a-4147-90c5-ad6929e6d625\",\"add\":3624,\"del\":59}]"}