<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mailtrap</title>
    <description>The latest articles on DEV Community by Mailtrap (@mailtrap).</description>
    <link>https://dev.to/mailtrap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F1386%2F13b59c85-c417-4a8a-8154-0f37fe20cec6.png</url>
      <title>DEV Community: Mailtrap</title>
      <link>https://dev.to/mailtrap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mailtrap"/>
    <language>en</language>
    <item>
      <title>Sending Emails with JavaScript</title>
      <dc:creator>Andriy Zapisotskyi</dc:creator>
      <pubDate>Tue, 16 Jun 2020 11:28:58 +0000</pubDate>
      <link>https://dev.to/mailtrap/sending-emails-with-javascript-4jdl</link>
      <guid>https://dev.to/mailtrap/sending-emails-with-javascript-4jdl</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The guide about &lt;a href="https://blog.mailtrap.io/javascript-send-email/"&gt;Sending emails with Javascript&lt;/a&gt; first appeared on Mailtrap blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JavaScript is a programming language that you can use for both front-end and back-end development. When the name JavaScript is used in the context of sending emails, Node.js is the first thing that comes to mind. We even blogged about how to send emails with Node.js. In this article, we want to change the perspective from the server-side to the client-side. Let’s figure out how you can use JS to send emails from the app that has no back-end.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ: Can I send emails with JS or not?
&lt;/h2&gt;

&lt;p&gt;You can’t send emails using JavaScript code alone due to lack of support for server sockets. For this, you need a server-side language that talks to the SMTP server. You can use JS in conjunction with a server script that will send emails from the browser based on your requests. This is the value we’re going to introduce below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you might want to send emails with JS
&lt;/h2&gt;

&lt;p&gt;Traditionally, the server-side of a regular app is responsible for sending emails. You will need to set up a server using back-end technology. The client-side sends a request to the server-side, which creates an email and sends it to the SMTP server. If you’re curious about what happens with an email after that, read our blog post, &lt;a href="https://blog.mailtrap.io/smtp-relay/"&gt;SMTP relay&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;So, why would anyone be willing to go another way and send emails right from the client-side using JavaScript? Such an approach is quite useful for building contact forms or other kinds of user interaction on web apps, which allows your app to send an email without refreshing the page the user is interacting with. Besides, you don’t have to mess around with coding a server. This is a strong argument if your web app uses email sending for contact forms only. Below, you will find a few options on how to make your app send emails from the client-side. &lt;/p&gt;

&lt;h2&gt;
  
  
  mailto: for sending form data
&lt;/h2&gt;

&lt;p&gt;Since you can't send an email directly with JS, you can tell the browser to open a default mail client to do so. Technically, the &lt;code&gt;mailto:&lt;/code&gt;` method does not send email directly from the browser, but it can do the job. Check out how the following code example works:&lt;/p&gt;

&lt;p&gt;FirstName:&lt;br&gt;
Email:&lt;/p&gt;

&lt;p&gt;When you launch it in the browser, you’ll see the following:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rjltU_r6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/4e7c444f-5ac2-44e7-b8eb-a8d288c4db02/" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rjltU_r6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/4e7c444f-5ac2-44e7-b8eb-a8d288c4db02/" alt="Screen Shot 2020-06-16 at 1.45.34 PM.png" width="880" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the data has been submitted, the browser opens a default mail client. In our case, this is Gmail.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wB6e5xlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/51cbde64-634e-4649-bea4-7c3b3adf0e36/" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wB6e5xlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/51cbde64-634e-4649-bea4-7c3b3adf0e36/" alt="Screen Shot 2020-06-16 at 1.45.46 PM.png" width="880" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&lt;/code&gt;&lt;code&gt;mailto:&lt;/code&gt;&lt;code&gt;&lt;/code&gt; method is a rather easy solution to implement but it has some specific drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can’t control the layout of the data since the data is submitted in the form sent by the browser.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&lt;/code&gt;&lt;code&gt;mailto:&lt;/code&gt;&lt;code&gt;&lt;/code&gt; doesn’t protect your email address from being harvested by spambots. Some time ago, this could be mitigated by constructing a link in JS. These days, more and more bots run JS and do not rely on HTML rendered by the server alone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SmtpJS.com - true email sending from JavaScript
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.smtpjs.com/"&gt;SmtpJS&lt;/a&gt; is a free library you can use for sending emails from JavaScript. All you need is an SMTP server and a few manipulations to get things done. We’ll use &lt;a href="https://mailtrap.io/"&gt;Mailtrap.io&lt;/a&gt; as the server because it is an actionable solution for email testing. Below is the flow you should follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Create an HTML file (for example, test.html) with the following script:&lt;/p&gt;

 &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a button that will trigger the JavaScript function&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write the JS function to send emails via SmtpJS.com.&lt;br&gt;
function sendEmail() {&lt;br&gt;
Email.send({&lt;br&gt;
Host : "smtp.mailtrap.io",&lt;br&gt;
Username : "",&lt;br&gt;
Password : "",&lt;br&gt;
To : '&lt;a href="mailto:recipient@example.com"&gt;recipient@example.com&lt;/a&gt;',&lt;br&gt;
From : "&lt;a href="mailto:sender@example.com"&gt;sender@example.com&lt;/a&gt;",&lt;br&gt;
Subject : "Test email",&lt;br&gt;
Body:&lt;br&gt;
}).then(&lt;br&gt;
message =&amp;gt; alert(message)&lt;br&gt;
);&lt;br&gt;
}&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run test.html in the browser and send your email&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xr1WUb_7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/fe45111d-088a-4773-bb01-d0869df0f888/" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xr1WUb_7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/fe45111d-088a-4773-bb01-d0869df0f888/" alt="Screen Shot 2020-06-16 at 1.53.51 PM.png" width="880" height="161"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The drawback with the code sample above is that your username and password are visible in the client-side script. This can be fixed if you utilize the encryption option provided by SmtpJS. Click the Encrypt your SMTP credentials button and fill in the required fields. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dtASO7_9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/ed2a5e52-cd59-44d1-879c-b4621174b76a/" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dtASO7_9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ucarecdn.com/ed2a5e52-cd59-44d1-879c-b4621174b76a/" alt="Screen Shot 2020-06-16 at 1.54.00 PM.png" width="722" height="760"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, press Generate security token and use it then in your JS function instead of SMTP server settings like the following:&lt;br&gt;
&lt;code&gt;&lt;/code&gt;&lt;code&gt;SecureToken : "&amp;lt;your generated token&amp;gt;"&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to more code samples for an HTML email and an email with attachments? Check the full article at &lt;a href="https://blog.mailtrap.io/javascript-send-email/"&gt;Mailtrap blog&lt;/a&gt;.
&lt;/h2&gt;

</description>
      <category>javascript</category>
      <category>email</category>
      <category>java</category>
    </item>
    <item>
      <title>Sending emails with Node.js</title>
      <dc:creator>Andriy Zapisotskyi</dc:creator>
      <pubDate>Mon, 15 Jun 2020 11:20:54 +0000</pubDate>
      <link>https://dev.to/mailtrap/node-js-emails-5g3m</link>
      <guid>https://dev.to/mailtrap/node-js-emails-5g3m</guid>
      <description>&lt;p&gt;Sending emails from Node.js is easy. We have gone over it in our previous blog post on &lt;a href="https://blog.mailtrap.io/sending-emails-with-nodemailer/"&gt;sending emails with Nodemailer&lt;/a&gt;. Last time we reviewed Nodemailer’s capabilities we focused on sending HTML emails via SMTP. In this post, we will examine how to send emails with Node.js using popular email servers like Gmail. Also, we will have a look at other transport options and packages to build and send emails from Node.js.  &lt;/p&gt;

&lt;p&gt;The "&lt;a href="https://blog.mailtrap.io/send-emails-with-nodejs/"&gt;Send emails in Node.js&lt;/a&gt;" blog post originally was published on Mailtrap's blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building and sending emails with Node.js without Nodemailer
&lt;/h2&gt;

&lt;p&gt;In some guides and tutorials, you might find a note that there are a variety of Node.js email packages but Nodemailer is the best one. It’s not true. In fact, you can barely find a decent alternative to Nodemailer (and I can hardly imagine why you might need it.)&lt;/p&gt;

&lt;p&gt;On Github, you can find several Node.js packages related to emails but they won’t offer you a wide functionality. With Nodemailer, you can create HTML emails with attachments and send them via SMTP, SES (wrapper for sending emails via AWS SES), or &lt;em&gt;sendmail&lt;/em&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The most similar package is &lt;a href="https://github.com/eleith/emailjs"&gt;Emaijs&lt;/a&gt;. Its features include: &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;sending emails via SMTP servers (both SSL and TLS) with authentication&lt;/li&gt;
&lt;li&gt;HTML support and MIME attachments (also, attachments can be added as strings, streams, or file paths)&lt;/li&gt;
&lt;li&gt;asynchronous sending of queued emails &lt;/li&gt;
&lt;li&gt;UTF-8 encoding in headers and body.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the main difference is that in Emailjs you will use MIME type to work with attachments, while in Nodemailer you use strings. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Another quite popular package is &lt;a href="https://github.com/niftylettuce/email-templates"&gt;email-templates&lt;/a&gt;. As you can see from the name, this package is designed for creating various custom templates for Node.js. It features support for automatic inline CSS, stylesheets, embedded images, and fonts. Also, it has an email preview option. The email templates package was made by the creator of the &lt;a href="https://lad.js.org/"&gt;Lad&lt;/a&gt; framework. So it’s recommended to use it with Lad. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One more package worth mentioning here is &lt;a href="https://github.com/eladnava/mailgen"&gt;Mailgen&lt;/a&gt;. It is aimed at creating HTML templates for transactional emails. There is a note on Github, that with Mailgen you can “Programmatically create beautiful e-mails using plain old JavaScript.” The package includes several open-source themes as well as supports custom elements like tables, action buttons, etc. It is your choice how to send an email created with Mailgen, but they recommend checking out Nodemailer for this purpose. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;| &lt;strong&gt;Feature&lt;/strong&gt; | &lt;strong&gt;Nodemailer&lt;/strong&gt; | &lt;strong&gt;Emailjs&lt;/strong&gt; | &lt;strong&gt;Email templates&lt;/strong&gt; | &lt;strong&gt;Mailgen&lt;/strong&gt; |&lt;br&gt;
| Building HTML emails | yes | Yes +MIME | Yes + CSS and customization | yes+CSS+themes |&lt;br&gt;
| Email sending | SMTP, SES, &lt;em&gt;sendmail&lt;/em&gt; | SMTP | Lad | no |&lt;br&gt;
| Github rating (stars) (as on June 25, 2019) | 11,258 | 1,748 | 2,489 | 2,223 |&lt;br&gt;
| Last commit (as on June 25, 2019) | May 26, 2019 | Sept 4, 2018 | June 23, 2019 | Jan 3, 2019 |&lt;/p&gt;

&lt;p&gt;As you can see from the above table, Nodemailer is the most popular package, which offers functionality for both email creation and email sending. It’s not limited to one sending method. But it won’t be easy to create a special email template. This is why it might be a good idea to use Nodemailer in combination with another package.&lt;/p&gt;

&lt;p&gt;To find all related packages and plugins, &lt;a href="https://www.npmjs.com/search?q=nodemailer"&gt;search for nodemailer&lt;/a&gt; in npm.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sending HTML emails with dynamic content
&lt;/h2&gt;

&lt;p&gt;In our previous blog post, we reviewed &lt;a href="https://blog.mailtrap.io/sending-emails-with-nodemailer/"&gt;several examples of sending HTML emails&lt;/a&gt; with Nodemailer, embedding images, and attaching files. In most cases, for transactional emails like registration confirmation or resetting passwords, you need to use dynamic content. It will be easier and more efficient to do it with one of the template modules. &lt;/p&gt;

&lt;p&gt;Let’s experiment with the &lt;a href="https://www.npmjs.com/package/email-templates"&gt;email-templates&lt;/a&gt; package. It has several interesting features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support for different template engines (&lt;a href="https://pugjs.org/api/getting-started.html"&gt;Pug&lt;/a&gt; is a default one)&lt;/li&gt;
&lt;li&gt;Email preview (by default) in the development environment&lt;/li&gt;
&lt;li&gt;Direct email sending. So, you don’t need extra packages like Nodemailer for email sending. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of all, let’s create our templates, for a frequently occurring scenario: new user registration. In this example, we are working with the default option (for more details and samples of using Pug, refer to &lt;a href="https://github.com/pugjs/pug"&gt;Github&lt;/a&gt;.) &lt;/p&gt;

&lt;p&gt;Install the template engine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/"&gt;npm&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install email-templates pug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://yarnpkg.com/"&gt;yarn&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add email-templates pug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should create two files: subject and HTML body.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;subject.pug:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;= `Hi ${firstName} ${lastName}, happy to see you at My App!`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;html.pug:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h1 Hello #{firstName} #{lastName}
    p.
Welcome to My App! Now your test emails will be safe. We just need to make sure your account is real. 
Please, click the button below and start using your account. 
a(href='https://example.com/confirmation') Confirm!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now make sure that your directory has the following structure:&lt;/p&gt;

&lt;p&gt;├── app.js&lt;/p&gt;

&lt;p&gt;├── emails&lt;/p&gt;

&lt;p&gt;│   └── welcome (the template name)&lt;/p&gt;

&lt;p&gt;│       ├── html.pug&lt;/p&gt;

&lt;p&gt;│       ├── subject.pug&lt;/p&gt;

&lt;p&gt;│       └── text.pug&lt;/p&gt;

&lt;p&gt;Pay attention to the text part of your message: if you don’t include it, it will be generated automatically. But if you add it, it will be rendered automatically. This means that the content of the text and HTML parts may differ. &lt;/p&gt;

&lt;p&gt;Now we can write some code to gather all the elements together and add transport. As usual, we will use &lt;a href="https://mailtrap.io/"&gt;Mailtrap&lt;/a&gt;, to be able to test and check everything. In the same way, you can use any other SMTP server like Gmail, for example. Just be careful if experimenting with real email addresses!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Email = require('email-templates');
const email = new Email({
 message: {
   from: 'hi@example.com'
 },
 send: true,
 transport: {
   host: 'smtp.mailtrap.io',
   port: 2525,
   ssl: false,
   tls: true,
   auth: {
     user: '1a2b3c4d5e6f7g', // your Mailtrap username
     pass: '1a2b3c4d5e6f7g' //your Mailtrap password
   }
 }
});

const people = [
 {firstName: 'Diana', lastName: 'One'},
 {firstName: 'Alex', lastName: 'Another'}
];

people.forEach((person) =&amp;gt; {
 email
   .send({
     template: 'welcome',
     message: {
       to: 'test@example.com'
     },
     locals: person
   })
   .then(console.log)
   .catch(console.error);
}).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, the preview of your email will be opened in your browser. It might be helpful if you are working on your template and don’t need to actually send the message. If you need to test how the variables work, and you compose a message to dozens or even hundreds of recipients, be careful with this option. To switch it off, specify &lt;em&gt;options.open&lt;/em&gt; as &lt;em&gt;false.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is why we use Mailtrap: we will see how the message looks for each recipient, explore both HTML and text versions, and will be able to perform additional checks. With Pug and email-templates, you can build a complex template using CSS, inlined images, tables, etc. Here is an example of how it should look in the Mailtrap virtual inbox:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--psi1O4Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/TwdtkXu-XSEkbWE4ojpb2O2pBEXItu3oJtChPpaGvXnx9F3ZW4wCru1oTDe05yL4dUOWCdwWyw-eXzxQsfH3l58Rsna_07PXopB1ZCohOtKBsjqQDMCcGocd0ceO_AW2_s57STi1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--psi1O4Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/TwdtkXu-XSEkbWE4ojpb2O2pBEXItu3oJtChPpaGvXnx9F3ZW4wCru1oTDe05yL4dUOWCdwWyw-eXzxQsfH3l58Rsna_07PXopB1ZCohOtKBsjqQDMCcGocd0ceO_AW2_s57STi1" alt="" width="880" height="400"&gt;&lt;/a&gt;* HTML*&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JEFR9ISD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/-pqsBEo-Mr4NVbcJQetksXWMSX2QhL-Cvy_pDqyf6CscXMTGxPK1s08HfW3WTulsHR5UO45ygUdA-XmePkso_dwiRhO3PHkGNB0Kk033vAr6shEDwK6wX0jBK7G63eLddvHu8VAg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JEFR9ISD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/-pqsBEo-Mr4NVbcJQetksXWMSX2QhL-Cvy_pDqyf6CscXMTGxPK1s08HfW3WTulsHR5UO45ygUdA-XmePkso_dwiRhO3PHkGNB0Kk033vAr6shEDwK6wX0jBK7G63eLddvHu8VAg" alt="" width="880" height="399"&gt;&lt;/a&gt;* Text*&lt;/p&gt;

&lt;p&gt;Enjoyed the reading? Full guide in &lt;a href="https://blog.mailtrap.io/send-emails-with-nodejs/"&gt;Node.js and Emails&lt;/a&gt; is available at Mailtrap blog.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>startup</category>
      <category>emails</category>
    </item>
    <item>
      <title>PHP built-in mail function vs PHP mailing packages</title>
      <dc:creator>Andriy Zapisotskyi</dc:creator>
      <pubDate>Wed, 01 Apr 2020 20:05:24 +0000</pubDate>
      <link>https://dev.to/mailtrap/php-built-in-mail-function-vs-php-mailing-packages-38nc</link>
      <guid>https://dev.to/mailtrap/php-built-in-mail-function-vs-php-mailing-packages-38nc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The article on &lt;a href="https://blog.mailtrap.io/php-email-sending/"&gt;Sending Emails in PHP&lt;/a&gt; was originally published at Mailtrap's blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  PHP built-in mail function ()
&lt;/h2&gt;

&lt;p&gt;There are two basic ways of sending emails with PHP: built-in mail function and external mail packages.  &lt;/p&gt;

&lt;p&gt;PHP’s built-in mail function () is very simple but at the same time, it provides a limited functionality for sending emails. You won’t be able to add attachments to your email, and building a beautiful HTML template with embedded images will be a tricky task as well. &lt;/p&gt;

&lt;p&gt;The other side of the PHP mail function () is that the email is sent from your web server, which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. The best way to overcome this problem is sending messages via an SMTP server, however, this functionality is limited as well. PHP mail() does not usually allow you to use the external SMTP server and it does not support SMTP authentication.&lt;/p&gt;

&lt;p&gt;In other words, I don’t recommend using the PHP built-in mail function() and advise you to avoid the headaches it may cause by installing an external mailer package.&lt;/p&gt;

&lt;p&gt;If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic code and its main parameters. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax and parameters&lt;/strong&gt;&lt;br&gt;
The PHP mail syntax is pretty simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
mail($to_email_address,$subject,$message,[$headers],[$parameters]);
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It uses the following parameters: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“$to” = your message recipient(s). The email address format may be &lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt; or User &lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt;. If there are several recipients, separate them with comma(-s).
&lt;/li&gt;
&lt;li&gt;“$subject” = your message’s subject
“$message” = the body of your message. Lines should be separated with a CRLF (\r\n). Each line should not exceed 70 characters.&lt;/li&gt;
&lt;li&gt;“[$headers]” = additional recipients of your message, which can be included in CC or BCC.  Headers are optional, except for the “from” header: it must be specified, otherwise, you will receive an error message like Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details and additional parameters, refer to the &lt;a href="http://php.net/manual/en/function.mail.php"&gt;PHP documentation&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sending HTML email using PHP mail() function
&lt;/h2&gt;

&lt;p&gt;The body of the message can be written in HTML. However, as I’ve mentioned above, it should be simple. In the PHP mail function(), the HTML part will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Message
$message = '
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Review Request Reminder&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;p&amp;gt;Here are the cases requiring your review in December:&amp;lt;/p&amp;gt;
  &amp;lt;table&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;th&amp;gt;Case title&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Category&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Status&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Due date&amp;lt;/th&amp;gt;
    &amp;lt;/tr&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;td&amp;gt;Case 1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Development&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;pending&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Dec-20&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;td&amp;gt;Case 1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;DevOps&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;pending&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Dec-21&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
  &amp;lt;/table&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s important to remember that to send HTML mail, you need to set the Content-type header...&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  Continue reading the full guide on &lt;a href="https://blog.mailtrap.io/php-email-sending/"&gt;sending emails in PHP&lt;/a&gt; originally on Mailtrap blog.
&lt;/h2&gt;
&lt;/blockquote&gt;

</description>
      <category>php</category>
      <category>email</category>
    </item>
    <item>
      <title>Mails and Node JS</title>
      <dc:creator>Andriy Zapisotskyi</dc:creator>
      <pubDate>Wed, 01 Apr 2020 17:52:51 +0000</pubDate>
      <link>https://dev.to/mailtrap/mails-and-node-js-24h</link>
      <guid>https://dev.to/mailtrap/mails-and-node-js-24h</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The "&lt;a href="https://blog.mailtrap.io/send-emails-with-nodejs/"&gt;send emails in Node.js&lt;/a&gt;" blog post originally was published on Mailtrap's blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sending emails from Node.js is easy. We have gone over it in our previous blog post on &lt;a href="https://blog.mailtrap.io/sending-emails-with-nodemailer/"&gt;sending emails with Nodemailer&lt;/a&gt;. Last time we reviewed Nodemailer’s capabilities we focused on sending HTML emails via SMTP. In this post, we will examine how to send emails with Node.js using popular email servers like Gmail. Also, we will have a look at other transport options and packages to build and send emails from Node.js.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Building and sending emails with Node.js without Nodemailer
&lt;/h2&gt;

&lt;p&gt;In some guides and tutorials, you might find a note that there are a variety of Node.js email packages but Nodemailer is the best one. It’s not true. In fact, you can barely find a decent alternative to Nodemailer (and I can hardly imagine why you might need it.)&lt;/p&gt;

&lt;p&gt;On Github, you can find several Node.js packages related to emails but they won’t offer you a wide functionality. With Nodemailer, you can create HTML emails with attachments and send them via SMTP, SES (wrapper for sending emails via AWS SES), or &lt;em&gt;sendmail&lt;/em&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The most similar package is &lt;a href="https://github.com/eleith/emailjs"&gt;Emaijs&lt;/a&gt;. Its features include: &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;sending emails via SMTP servers (both SSL and TLS) with authentication&lt;/li&gt;
&lt;li&gt;HTML support and MIME attachments (also, attachments can be added as strings, streams, or file paths)&lt;/li&gt;
&lt;li&gt;asynchronous sending of queued emails &lt;/li&gt;
&lt;li&gt;UTF-8 encoding in headers and body.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the main difference is that in Emailjs you will use MIME type to work with attachments, while in Nodemailer you use strings. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Another quite popular package is &lt;a href="https://github.com/niftylettuce/email-templates"&gt;email-templates&lt;/a&gt;. As you can see from the name, this package is designed for creating various custom templates for Node.js. It features support for automatic inline CSS, stylesheets, embedded images, and fonts. Also, it has an email preview option. The email templates package was made by the creator of the &lt;a href="https://lad.js.org/"&gt;Lad&lt;/a&gt; framework. So it’s recommended to use it with Lad. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One more package worth mentioning here is &lt;a href="https://github.com/eladnava/mailgen"&gt;Mailgen&lt;/a&gt;. It is aimed at creating HTML templates for transactional emails. There is a note on Github, that with Mailgen you can “Programmatically create beautiful e-mails using plain old JavaScript.” The package includes several open-source themes as well as supports custom elements like tables, action buttons, etc. It is your choice how to send an email created with Mailgen, but they recommend checking out Nodemailer for this purpose. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;| &lt;strong&gt;Feature&lt;/strong&gt; | &lt;strong&gt;Nodemailer&lt;/strong&gt; | &lt;strong&gt;Emailjs&lt;/strong&gt; | &lt;strong&gt;Email templates&lt;/strong&gt; | &lt;strong&gt;Mailgen&lt;/strong&gt; |&lt;br&gt;
| Building HTML emails | yes | Yes +MIME | Yes + CSS and customization | yes+CSS+themes |&lt;br&gt;
| Email sending | SMTP, SES, &lt;em&gt;sendmail&lt;/em&gt; | SMTP | Lad | no |&lt;br&gt;
| Github rating (stars) (as on June 25, 2019) | 11,258 | 1,748 | 2,489 | 2,223 |&lt;br&gt;
| Last commit (as on June 25, 2019) | May 26, 2019 | Sept 4, 2018 | June 23, 2019 | Jan 3, 2019 |&lt;/p&gt;

&lt;p&gt;As you can see from the above table, Nodemailer is the most popular package, which offers functionality for both email creation and email sending. It’s not limited to one sending method. But it won’t be easy to create a special email template. This is why it might be a good idea to use Nodemailer in combination with another package.&lt;/p&gt;

&lt;p&gt;To find all related packages and plugins, &lt;a href="https://www.npmjs.com/search?q=nodemailer"&gt;search for nodemailer&lt;/a&gt; in npm.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sending HTML emails with dynamic content
&lt;/h2&gt;

&lt;p&gt;In our previous blog post, we reviewed &lt;a href="https://blog.mailtrap.io/sending-emails-with-nodemailer/"&gt;several examples of sending HTML emails&lt;/a&gt; with Nodemailer, embedding images, and attaching files. In most cases, for transactional emails like registration confirmation or resetting passwords, you need to use dynamic content. It will be easier and more efficient to do it with one of the template modules. &lt;/p&gt;

&lt;p&gt;Let’s experiment with the &lt;a href="https://www.npmjs.com/package/email-templates"&gt;email-templates&lt;/a&gt; package. It has several interesting features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support for different template engines (&lt;a href="https://pugjs.org/api/getting-started.html"&gt;Pug&lt;/a&gt; is a default one)&lt;/li&gt;
&lt;li&gt;Email preview (by default) in the development environment&lt;/li&gt;
&lt;li&gt;Direct email sending. So, you don’t need extra packages like Nodemailer for email sending. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of all, let’s create our templates, for a frequently occurring scenario: new user registration. In this example, we are working with the default option (for more details and samples of using Pug, refer to &lt;a href="https://github.com/pugjs/pug"&gt;Github&lt;/a&gt;.) &lt;/p&gt;

&lt;p&gt;Install the template engine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/"&gt;npm&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install email-templates pug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://yarnpkg.com/"&gt;yarn&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add email-templates pug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should create two files: subject and HTML body.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;subject.pug:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;= `Hi ${firstName} ${lastName}, happy to see you at My App!`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;html.pug:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h1 Hello #{firstName} #{lastName}
    p.
Welcome to My App! Now your test emails will be safe. We just need to make sure your account is real. 
Please, click the button below and start using your account. 
a(href='https://example.com/confirmation') Confirm!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now make sure that your directory has the following structure:&lt;/p&gt;

&lt;p&gt;├── app.js&lt;/p&gt;

&lt;p&gt;├── emails&lt;/p&gt;

&lt;p&gt;│   └── welcome (the template name)&lt;/p&gt;

&lt;p&gt;│       ├── html.pug&lt;/p&gt;

&lt;p&gt;│       ├── subject.pug&lt;/p&gt;

&lt;p&gt;│       └── text.pug&lt;/p&gt;

&lt;p&gt;Pay attention to the text part of your message: if you don’t include it, it will be generated automatically. But if you add it, it will be rendered automatically. This means that the content of the text and HTML parts may differ. &lt;/p&gt;

&lt;p&gt;Now we can write some code to gather all the elements together and add transport. As usual, we will use &lt;a href="https://mailtrap.io/"&gt;Mailtrap&lt;/a&gt;, to be able to test and check everything. In the same way, you can use any other SMTP server like Gmail, for example. Just be careful if experimenting with real email addresses!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Email = require('email-templates');
const email = new Email({
 message: {
   from: 'hi@example.com'
 },
 send: true,
 transport: {
   host: 'smtp.mailtrap.io',
   port: 2525,
   ssl: false,
   tls: true,
   auth: {
     user: '1a2b3c4d5e6f7g', // your Mailtrap username
     pass: '1a2b3c4d5e6f7g' //your Mailtrap password
   }
 }
});

const people = [
 {firstName: 'Diana', lastName: 'One'},
 {firstName: 'Alex', lastName: 'Another'}
];

people.forEach((person) =&amp;gt; {
 email
   .send({
     template: 'welcome',
     message: {
       to: 'test@example.com'
     },
     locals: person
   })
   .then(console.log)
   .catch(console.error);
}).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, the preview of your email will be opened in your browser. It might be helpful if you are working on your template and don’t need to actually send the message. If you need to test how the variables work, and you compose a message to dozens or even hundreds of recipients, be careful with this option. To switch it off, specify &lt;em&gt;options.open&lt;/em&gt; as &lt;em&gt;false.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is why we use Mailtrap: we will see how the message looks for each recipient, explore both HTML and text versions, and will be able to perform additional checks. With Pug and email-templates, you can build a complex template using CSS, inlined images, tables, etc. Here is an example of how it should look in the Mailtrap virtual inbox:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--psi1O4Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/TwdtkXu-XSEkbWE4ojpb2O2pBEXItu3oJtChPpaGvXnx9F3ZW4wCru1oTDe05yL4dUOWCdwWyw-eXzxQsfH3l58Rsna_07PXopB1ZCohOtKBsjqQDMCcGocd0ceO_AW2_s57STi1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--psi1O4Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/TwdtkXu-XSEkbWE4ojpb2O2pBEXItu3oJtChPpaGvXnx9F3ZW4wCru1oTDe05yL4dUOWCdwWyw-eXzxQsfH3l58Rsna_07PXopB1ZCohOtKBsjqQDMCcGocd0ceO_AW2_s57STi1" alt="" width="880" height="400"&gt;&lt;/a&gt;* HTML*&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JEFR9ISD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/-pqsBEo-Mr4NVbcJQetksXWMSX2QhL-Cvy_pDqyf6CscXMTGxPK1s08HfW3WTulsHR5UO45ygUdA-XmePkso_dwiRhO3PHkGNB0Kk033vAr6shEDwK6wX0jBK7G63eLddvHu8VAg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JEFR9ISD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/-pqsBEo-Mr4NVbcJQetksXWMSX2QhL-Cvy_pDqyf6CscXMTGxPK1s08HfW3WTulsHR5UO45ygUdA-XmePkso_dwiRhO3PHkGNB0Kk033vAr6shEDwK6wX0jBK7G63eLddvHu8VAg" alt="" width="880" height="399"&gt;&lt;/a&gt;* Text*&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending Emails with Nodemailer and SMTP
&lt;/h2&gt;

&lt;p&gt;If configuring a new message in Nodemailer, we always should start with creating a transport method. The most popular one is the SMTP server, which can be easily set up for the majority of email clients or sending providers (like Sendgrid, Outlook, Gmail, etc.) SMTP configuration will be very simple and similar. For more detailed instructions on how to use Nodemailer, refer to the “&lt;a href="https://blog.mailtrap.io/sending-emails-with-nodemailer/"&gt;Sending emails with Nodemailer explained&lt;/a&gt;” blog post.&lt;/p&gt;

&lt;p&gt;Here we will demonstrate how to send emails with Gmail as it requires some tricks related to authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending emails with Gmail 
&lt;/h2&gt;

&lt;p&gt;To be able to use Gmail to send messages via your app, you should start with several account configurations.&lt;/p&gt;

&lt;p&gt;If you use a &lt;strong&gt;plain password&lt;/strong&gt; , then you should allow access for less secure apps. &lt;/p&gt;

&lt;p&gt;If you are using &lt;strong&gt;2-Step Verification&lt;/strong&gt; , you should sign in with &lt;a href="https://support.google.com/accounts/answer/185833?hl=en"&gt;App Passwords.&lt;/a&gt; To create your password:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;strong&gt;Security&lt;/strong&gt; section of your &lt;a href="https://myaccount.google.com/"&gt;Gmail account&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;App Passwords&lt;/strong&gt; in the &lt;strong&gt;Signing into Google&lt;/strong&gt; block.&lt;/li&gt;
&lt;li&gt;Select the app and device from the list and press &lt;strong&gt;Generate&lt;/strong&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please note that you can use it for your personal account only. It’s not available for accounts that are a part of an organization. &lt;/p&gt;

&lt;p&gt;What else you should remember when setting the Gmail SMTP:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gmail will automatically set the authenticated username as the &lt;em&gt;From&lt;/em&gt; email address. To change it, you should “Add another address you own”. You will find it in your Gmail account -&amp;gt; Settings-&amp;gt; Accounts. For more details, refer to this &lt;a href="https://support.google.com/mail/answer/22370?hl=en"&gt;Google Help Center article&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;Gmail has its own &lt;a href="https://support.google.com/a/answer/166852?hl=en"&gt;email limits&lt;/a&gt;. For free (trial) accounts, it’s only 500 emails per day. If you reach the limit, your account might be suspended. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, when you made all necessary configurations, let’s set up the Gmail SMTP as a transport in the Node.js app. &lt;/p&gt;

&lt;p&gt;Gmail SMTP hostname is &lt;code&gt;smtp.gmail.com&lt;/code&gt;, the port should be 465 for SSL connection or 587 for TLS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: 'yourusername@gmail.com',
        pass: 'yourpassword'
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To avoid authentication issues, it is recommended to use oAuth2. Nodemailer requires an &lt;em&gt;Access Token&lt;/em&gt; to perform authentication. Read the instructions on the &lt;a href="https://nodemailer.com/usage/using-gmail/"&gt;Nodemailer documentation&lt;/a&gt; to proceed with this method. &lt;/p&gt;

&lt;p&gt;Once you have retrieved client ID and client Secret, refresh token and enable Gmail API at API console. It is recommended to use &lt;code&gt;bunyan&lt;/code&gt; logger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bunyan = require('bunyan');
const nodemailer = require('../lib/nodemailer');

let logger = bunyan.createLogger({
    name: 'nodemailer'
});
logger.level('trace');
// Create a SMTP transporter object
let transporter = nodemailer.createTransport(
    {
        service: 'Gmail',
        auth: {
            type: 'OAuth2',
            user: 'mail',
            clientId: 'clientid',
            clientSecret: 'clientsecret',
            refreshToken: 'refreshtoken',
            accessToken: 'accesstoken',
            expires: 12345
        },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise, to get an access token, you can use &lt;a href="https://www.npmjs.com/package/xoauth2"&gt;xoauth2&lt;/a&gt; package. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sending emails without SMTP 
&lt;/h2&gt;

&lt;p&gt;If you stick to one of the popular email sending providers like &lt;a href="https://blog.mailtrap.io/sendgrid-vs-mandrill-vs-mailgun/"&gt;Sendgrid, Mandrill, Mailgun&lt;/a&gt;, or Postmark, you can integrate your Node.js app with their API directly.&lt;/p&gt;

&lt;p&gt;For AWS SES, there is a wrapper around Nodemailer, &lt;a href="https://nodemailer.com/transports/ses/"&gt;node-ses&lt;/a&gt;. You can use &lt;code&gt;aws-sdk&lt;/code&gt; directly, but &lt;code&gt;node-ses&lt;/code&gt; provides a simpler way to send complex email templates with images and attachments. &lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;If you are adding the email sending functionality to your Node.js app, most likely you will use Nodemailer. It is the simplest and most popular sending option compatible with other packages. &lt;/p&gt;

&lt;p&gt;Besides, there are still options for how to send emails in Node.js without Nodemailer and without the SMTP server as well. Choose the option which best suits your current environment and needs. Just don’t forget to inspect and debug your test emails before delivering them to your customers.&lt;/p&gt;

</description>
      <category>node</category>
      <category>emails</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
