在 Node.js 中发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4113701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:43:43  来源:igfitidea点击:

Sending emails in Node.js?

node.jsemail-integration

提问by NycCompSci

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.

我最近开始编写我的第一个 node.js。但是,我发现我无法创建直接发送到我的电子邮件的与我联系的表单,因为我无法从节点中找到任何能够发送电子邮件的模块。

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

有谁知道 node.js 电子邮件库或示例联系表单脚本?

采纳答案by JimBastard

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

node-email-templates 是一个更好的选择:https: //github.com/niftylettuce/node-email-templates

it has support for windows as well

它也支持windows

回答by Eric Peterson

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.

Nodemailer 基本上是一个模块,它使您能够在使用 Node.js 编程时轻松发送电子邮件。在http://www.nodemailer.com/ 上有一些关于如何使用 Nodemailer 模块的很好的例子。此链接中包含有关如何安装和使用 Nodemailer 基本功能的完整说明。

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.

我个人在使用 npm 安装 Nodemailer 时遇到了麻烦,所以我只下载了源代码。有关于 npm 安装和下载源代码的说明。

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

这是一个使用起来非常简单的模块,我会向任何想要使用 Node.js 发送电子邮件的人推荐它。祝你好运!

回答by silvio

Check out emailjs

查看emailjs

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

在尝试让 nodemailer 处理大附件上浪费了大量时间之后,找到了 emailjs 并从此开心。

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..

它支持使用普通 File 对象发送文件,而不是像 nodemailer 需要的巨大缓冲区。意味着您可以将其链接到 fe,以将附件从 html 表单传递给邮件程序。它还支持排队..

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

总而言之,不知道为什么 nodejitsu ppl 选择 nodemailer 作为他们的版本的基础,emailjs 只是更先进。

回答by Vicky

Complete Code to send Email Using nodemailer Module

使用 nodemailer 模块发送电子邮件的完整代码

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();
});

回答by Dean Rather

@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.

@JimBastard 接受的答案似乎过时了,我看了看,该邮件程序库已经超过 7 个月没有被触及,列出了几个错误,并且不再在 npm 中注册。

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

nodemailer当然看起来是最好的选择,但是在这个线程的其他答案中提供的 url 都是 404'ing。

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

nodemailer 声称支持 gmail、hotmail 等的简单插件,并且还有非常漂亮的文档。

回答by Robin Orheden

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

您可以随时使用AlphaMail披露:我是其背后的开发人员之一)。

Just install with NPM:

只需使用NPM安装:

npm install alphamail

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

注册一个 AlphaMail 帐户。获取令牌,然后您就可以开始使用 AlphaMail 服务发送了。

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:

在 AlphaMail GUI(仪表板)中,您将能够使用您发送的数据编辑模板:

<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.

模板是用Comlang编写的,它是一种专门为电子邮件设计的简单模板语言。

回答by Otto Kek?l?inen

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

成熟,使用简单,如果简单还不够,还有很多功能:Nodemailer:https: //github.com/andris9/nodemailer(注意正确的网址!)

回答by aacharya_vaddey

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

Nodemailer 模块是在 node.js 中发送电子邮件的最简单方法。

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

试试这个示例表单:http: //www.tutorialindustry.com/nodejs-mail-tutorial-using-nodemailer-module

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

附加信息:http: //www.nodemailer.com/

回答by Some dude

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

您肯定想使用https://github.com/niftylettuce/node-email-templates,因为它支持 nodemailer/postmarkapp 并且内置了漂亮的异步电子邮件模板支持。

回答by Ezku

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

npm有几个包,但都没有达到 1.0。最佳选择npm list mail

[email protected]
[email protected]
[email protected]