【问题标题】:Sending emails in Node.js? [closed]在 Node.js 中发送电子邮件? [关闭]
【发布时间】:2011-05-06 01:11:23
【问题描述】:

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

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

【问题讨论】:

  • 结帐:AWS-SES 为 ec2 应用程序免费发送 62000 封电子邮件。

标签: node.js email


【解决方案1】:

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

var mailer = require("nodemailer");

// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "gmail_id@gmail.com",
        pass: "gmail_password"
    }
});

var mail = {
    from: "Yashwant Chavan <from@gmail.com>",
    to: "to@gmail.com",
    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();
});

【讨论】:

  • 我发现Nodemailer waynode-email-templates 更易于使用。也许是因为我只是想发送基本的纯文本/基本 html 电子邮件,但我发现 node-email-templates 需要更多设置,而 Nodemailer 只需 2 分钟即可启动并运行。
  • 除了 gmail 吗?我们如何配置自己的 smtp 服务器?
  • 使用 webpack 编译后出现此错误:TypeError: Cannot create property 'mailer' on string 'SMTP' at new Mail。我去过thisthis page 之类的帖子,但仍然迷路。
  • @user1063287 去掉“SMTP”参数即可
  • 请注意,您不能将发件人地址更改为您用作身份验证的电子邮件地址以外的任何其他地址。 Gmail 将发件人地址强制发送到该地址,以便确认发件人的权限。
【解决方案2】:

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

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

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

【讨论】:

  • nodemailer 开始工作了!
  • 使用 SMTP 和 Amazon SES 对其进行了测试,对我有用,将其集成到 ConnectedSets 框架中
  • 简单,设置起来不费吹灰之力。只需复制示例代码,更改为我的信息(阅读 SSL 安全),它就可以工作了。非常适合简单的电子邮件发件人!
  • 现在是商业项目,请注意。
  • 这个答案应该包括一个例子
【解决方案3】:

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

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

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

【讨论】:

【解决方案4】:

campaign 是一个在 Node 中发送电子邮件的综合解决方案,它带有一个非常简单的 API。

你这样实例化它。

var client = require('campaign')({
  from: 'you@gmail.com'
});

要发送电子邮件,您可以使用Mandrill,这是免费且很棒的。只需设置您的 API 密钥,如下所示:

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

(如果您想使用其他提供商发送电子邮件,请查看文档)

然后,当你想发送电子邮件时,你可以这样做:

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

GitHub 存储库有 pretty extensive documentation

【讨论】:

    【解决方案5】:

    你总是可以使用AlphaMail披露:我是它背后的开发者之一)。

    只需使用NPM 安装:

    npm install alphamail
    

    注册一个 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", "from@example.com"))
        .setReceiver(new alphamail.EmailContact("John Doe", "to@example.org"))
        .setBodyObject(person) // Any serializable object
    );
    

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

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

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

    【讨论】:

    • Alphamail 现在正式死了。非常不幸 - 我正是因为这篇文章才开始使用它。祝开发者在下一次创业中一切顺利。
    【解决方案6】:

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

    它也支持windows

    【讨论】:

    • 这确实有助于文档非常自我解释。
    • 那是因为它的nodemailer.com 现在我在我的项目中使用它,工作正常,nodejitsu 通过 gmail 的 smtp 服务器发送邮件没有问题。
    • 过去两天我花了几个小时试图让node-email-templates 启动并运行。我只想将它用作我初始化然后用于发送的对象。无法让它工作。放弃。
    • 教程被证明很有价值(Nodejs Gamil OAuth2)masashi-k.blogspot.com.au/2013/06/…
    【解决方案7】:

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

    【讨论】:

      【解决方案8】:

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

      【讨论】:

        【解决方案9】:

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

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

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

        【讨论】:

          【解决方案10】:

          npm 有几个包,但还没有达到 1.0。来自npm list mail的最佳选择:

          email@0.2.2
          mail@0.1.1
          mailer@0.3.0
          

          【讨论】:

            【解决方案11】:

            查看emailjs

            在尝试使 nodemailer 处理大型附件浪费了大量时间之后,发现 emailjs 并从此快乐。

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

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

            【讨论】:

            • 经过长时间使用,不得不 fork 解决一个问题:除非在邮件发送进程中,否则 Thing 不会处理套接字事件,因此在闲置一段时间后停止,因为服务器将关闭连接,但没有任何东西可以处理事件并重置内部变量。你可以在这里找到分叉的版本:https://github.com/silvioster/emailjs。这个版本已经稳定运行了很长时间了,没有卡顿。
            • 更新到之前的评论,原作者修复了该错误和其他一些错误,因此不再需要 fork。最新版本应该在https://github.com/eleith/emailjs
            • nodemailer 也支持附件流,不仅是“巨大的缓冲区”
            猜你喜欢
            • 2012-05-23
            • 2013-08-13
            • 2013-04-25
            • 2012-03-04
            • 2013-10-30
            • 2012-08-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多