Sending emails in Node.js? [closed]
Asked Answered
H

11

292

I recently started programming my first node.js. However, I discovered that I am unable to create a contact me form that sends straight to my email since I can't find any modules from node that is able to send emails.

Does anyone know of a node.js email library or an sample contact form script?

Hiller answered 6/11, 2010 at 15:22 Comment(1)
Checkout: AWS-SES free 62000 emails for ec2 apps.Ozmo
E
142

node-email-templates is a much better option: https://github.com/niftylettuce/node-email-templates

it has support for windows as well

Escribe answered 8/11, 2010 at 3:48 Comment(9)
This really helps the docs are pretty self explanatory.Hiller
github.com/marak/node_mailer is deprecated. Should use this github.com/andris9/NodemailerBeale
thats because its nodemailer.com now i am using it in my project, works fine, nodejitsu had no problems sending mail through gmail's smtp servers.Imperishable
I've spent a few hours over the past two days trying to get node-email-templates up and running. I want to just use it as an object that I initialize and then later use to send. Can't get it to work. Giving up.Indurate
Tutorial proved valuable (Nodejs Gamil OAuth2) masashi-k.blogspot.com.au/2013/06/…Handed
FYI the nodemailer . org site is spam - the correct URL is nodemailer.com @EduardoCostaViolative
it may be worth noting that this also adds about a million dependencies to your projectTraylor
here is a tutorial on nodejs send email using nodemailer and MySQL. Hope it is helpful for someone. programmerblog.net/nodejs-send-email-tutorialParathion
That's an impressive amount of time and consideration. The more you get, the more you got, the more you get some more.Gerlachovka
S
162

Nodemailer is basically a module that gives you the ability to easily send emails when programming in Node.js. There are some great examples of how to use the Nodemailer module at http://www.nodemailer.com/. The full instructions about how to install and use the basic functionality of Nodemailer is included in this link.

I personally had trouble installing Nodemailer using npm, so I just downloaded the source. There are instructions for both the npm install and downloading the source.

This is a very simple module to use and I would recommend it to anyone wanting to send emails using Node.js. Good luck!

Scholasticate answered 28/1, 2011 at 18:54 Comment(5)
nodemailer worked at first!Ingrowing
tested it with both SMTP and Amazon SES, works for me, integrated it into the ConnectedSets frameworkSolution
simple, no hard work for setting its up. just copied the example code, changed to my info (read about SSL secure) and it just worked. great for simple email sender!Polyclitus
Its a commercial project now, so be warned.Avitzur
This answer should include an exampleExcessive
E
142

node-email-templates is a much better option: https://github.com/niftylettuce/node-email-templates

it has support for windows as well

Escribe answered 8/11, 2010 at 3:48 Comment(9)
This really helps the docs are pretty self explanatory.Hiller
github.com/marak/node_mailer is deprecated. Should use this github.com/andris9/NodemailerBeale
thats because its nodemailer.com now i am using it in my project, works fine, nodejitsu had no problems sending mail through gmail's smtp servers.Imperishable
I've spent a few hours over the past two days trying to get node-email-templates up and running. I want to just use it as an object that I initialize and then later use to send. Can't get it to work. Giving up.Indurate
Tutorial proved valuable (Nodejs Gamil OAuth2) masashi-k.blogspot.com.au/2013/06/…Handed
FYI the nodemailer . org site is spam - the correct URL is nodemailer.com @EduardoCostaViolative
it may be worth noting that this also adds about a million dependencies to your projectTraylor
here is a tutorial on nodejs send email using nodemailer and MySQL. Hope it is helpful for someone. programmerblog.net/nodejs-send-email-tutorialParathion
That's an impressive amount of time and consideration. The more you get, the more you got, the more you get some more.Gerlachovka
W
65

Check out emailjs

After wasting lots of time on trying to make nodemailer work with large attachments, found emailjs and happy ever since.

It supports sending files by using normal File objects, and not huge Buffers as nodemailer requires. Means that you can link it to, f.e., formidable to pass the attachments from an html form to the mailer. It also supports queueing..

All in all, no idea why nodejitsu ppl chose nodemailer to base their version on, emailjs is just much more advanced.

Warsle answered 20/8, 2011 at 17:7 Comment(3)
After some prolonged use, had to fork it to fix up a problem: unless inside the mail-sending process, the thing would not process socket events, and thus stalled after some time being idle, as the server would close the connection, but there was nothing to process the event and reset the internal variables. You can find the forked version here:https://github.com/silvioster/emailjs. This version has been working stable for quite a long time now with no stalls.Warsle
update to previous comment, the original author fixed that bug and some others too, so fork is not needed any more. the newest version should be on https://github.com/eleith/emailjsWarsle
nodemailer supports attachment streams also, not only "huge Buffers"Conductive
A
57

Complete Code to send Email Using nodemailer Module

var mailer = require("nodemailer");

// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "gmail_password"
    }
});

var mail = {
    from: "Yashwant Chavan <[email protected]>",
    to: "[email protected]",
    subject: "Send Email Using Node.js",
    text: "Node.js New world for me",
    html: "<b>Node.js New world for me</b>"
}

smtpTransport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    smtpTransport.close();
});
Almeida answered 22/3, 2014 at 2:41 Comment(5)
I found Nodemailer way easier to use than node-email-templates. Maybe it's because I was just trying to send basic plaintext/basic html email, but I found node-email-templates needed way more setting up, whereas Nodemailer was up and running in literally 2 minutes.Increate
Other than gmail ? How can we configure for our own smtp server ?Detoxify
I get this error after compiling with webpack: TypeError: Cannot create property 'mailer' on string 'SMTP' at new Mail. I've gone to posts like this and then this page but still lost.Irrupt
@Irrupt Just remove the "SMTP" parameterOverhasty
Just so you're aware, you can't change the from address to anything other than your own email address you used as the authentication. Gmail forces the from address to that one so it can confirm the authority of the sender.Vitek
L
25

@JimBastard's accepted answer appears to be dated, I had a look and that mailer lib hasn't been touched in over 7 months, has several bugs listed, and is no longer registered in npm.

nodemailer certainly looks like the best option, however the url provided in other answers on this thread are all 404'ing.

nodemailer claims to support easy plugins into gmail, hotmail, etc. and also has really beautiful documentation.

Landri answered 15/5, 2012 at 14:22 Comment(0)
T
9

You could always use AlphaMail (disclosure: I'm one of the developers behind it).

Just install with NPM:

npm install alphamail

Sign up for a AlphaMail account. Get a token, and then you can start sending with the AlphaMail service.

var alphamail = require('alphamail');

var emailService = new alphamail.EmailService()
    .setServiceUrl('http://api.amail.io/v1/')
    .setApiToken('YOUR-ACCOUNT-API-TOKEN-HERE');

var person = {
    id: 1234,
    userName: "jdoe75",
    name: {
        first: "John",
        last: "Doe"
    },
    dateOfBirth: 1975
};

emailService.queue(new alphamail.EmailMessagePayload()
    .setProjectId(12345) // ID of your AlphaMail project (determines template, options, etc)
    .setSender(new alphamail.EmailContact("Sender Company Name", "[email protected]"))
    .setReceiver(new alphamail.EmailContact("John Doe", "[email protected]"))
    .setBodyObject(person) // Any serializable object
);

And in the AlphaMail GUI (Dashboard) you'll be able to edit the template with the data you sent:

<html>
    <body>
        <b>Name:</b> <# payload.name.last " " payload.name.first #><br>
        <b>Date of Birth:</b> <# payload.dateOfBirth #><br>

        <# if (payload.id != null) { #>
            <a href="http://company.com/sign-up">Sign Up Free!</a>
        <# } else { #>
            <a href="http://company.com/login?username=<# urlencode(payload.userName) #>">Sign In</a>
        <# } #>
    </body>
</html>

The templates are written in Comlang, it's a simple template language specifically designed for emails.

Thimbleful answered 27/9, 2012 at 14:33 Comment(1)
Alphamail is now officially dead. Very unfortunate - I started using it exactly because of this post. Wishing the developers all the best in their next venture.Lundquist
C
8

Mature, simple to use and has lots of features if simple isn't enought: Nodemailer: https://github.com/andris9/nodemailer (note correct url!)

Cyclometer answered 2/6, 2012 at 12:9 Comment(0)
D
5

Nodemailer Module is the simplest way to send emails in node.js.

Try this sample example form: http://www.tutorialindustry.com/nodejs-mail-tutorial-using-nodemailer-module

Additional Info: http://www.nodemailer.com/

Duplicate answered 14/2, 2014 at 14:59 Comment(1)
tutorialindustry.com/… is not working!Bristle
S
3

npm has a few packages, but none have reached 1.0 yet. Best picks from npm list mail:

[email protected]
[email protected]
[email protected]
Sickle answered 6/11, 2010 at 15:28 Comment(0)
F
3

You definitely want to use https://github.com/niftylettuce/node-email-templates since it supports nodemailer/postmarkapp and has beautiful async email template support built-in.

Feverfew answered 1/7, 2012 at 8:40 Comment(0)
B
2

campaign is a comprehensive solution for sending emails in Node, and it comes with a very simple API.

You instance it like this.

var client = require('campaign')({
  from: '[email protected]'
});

To send emails, you can use Mandrill, which is free and awesome. Just set your API key, like this:

process.env.MANDRILL_APIKEY = '<your api key>';

(if you want to send emails using another provider, check the docs)

Then, when you want to send an email, you can do it like this:

client.sendString('<p>{{something}}</p>', {
  to: ['[email protected]', '[email protected]'],
  subject: 'Some Subject',
  preview': 'The first line',
  something: 'this is what replaces that thing in the template'
}, done);

The GitHub repo has pretty extensive documentation.

Brenna answered 2/1, 2014 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.