Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a nodejs equivalent to PHP's mail() function

Tags:

php

node.js

email

I come from the world of PHP and I'm accustomed to using mail() to send quick diagnostic emails on occasion. Is there a module or method in the standar library of NodeJS that's roughly the equivalent of this?

like image 855
Casey Flynn Avatar asked Feb 03 '13 23:02

Casey Flynn


People also ask

Can you send emails with node js?

Whether you're building the next Gmail, cutting-edge email marketing software, or just want to program notifications into your Node. js app, it's easy to send emails using readily-made tools and services. Node. js is one of the most popular (if not the most popular) server-side runtime environment for web applications.

Which module is used to send emails in node JS?

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.


2 Answers

Sure:

const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({sendmail: true}, {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'test',
});
transporter.sendMail({text: 'hello'});

Also see Configure sendmail inside a docker container

like image 147
Igor Sukharev Avatar answered Oct 19 '22 04:10

Igor Sukharev


Nodemailer is a popular, stable, and flexible solution:

  • http://www.nodemailer.com/
  • https://github.com/andris9/Nodemailer

Full usage looks something like this (the top bit is just setup - so you would only have to do that once per app):

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "[email protected], [email protected]", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}

// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    // if you don't want to use this transport object anymore, uncomment following line
    //smtpTransport.close(); // shut down the connection pool, no more messages
});
like image 24
hunterloftis Avatar answered Oct 19 '22 04:10

hunterloftis