nodemailer-mailgun-transport中文文档|nodemailer-mailgun-transport js中文教程|解析
nodemailer-mailgun-transport
这是什么?
nodemailer 是一个了不起的节点模块,可以在您的任何 nodejs 应用程序中发送电子邮件。这是运输的插件,与nodemailer去用发送电子邮件Mailgun的迷死人!噗噗。
它是如何工作的?
基于这个 mailgun-js 模块和nodemailer 模块,Mailgun Transport 诞生了。这是一个传输层,这意味着它允许您使用 nodemailer 发送电子邮件,使用 Mailgun API 而不是 SMTP 协议!
Nodemailer 允许您编写一次代码,然后交换传输,以便您可以在不同的提供商上使用不同的帐户。最重要的是,它是一种在您的节点应用程序上快速发送电子邮件的超级可靠方式。
当 SMTP 在您的服务器上被阻止或者您只是更喜欢 web api 的可靠性时,nodemailer 的 Mailgun 传输非常适合使用!
支持项目
我知道这是一个很小的模块,但很多人在生产中使用它(对我们所有人来说都是高 5)——如果你碰巧使用了这个模块,请点击星号按钮——这对所有贡献者来说意义重大
快速入门 – 示例
创建一个新文件,安装依赖项[1]并查看下面的骨架代码,让您快速入门!
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
// This is your API key that you retrieve from www.mailgun.com/cp (free up to 10K monthly emails)
const auth = {
auth: {
api_key: 'key-1234123412341234',
domain: 'one of your domain names listed at your https://app.mailgun.com/app/sending/domains'
}
}
const nodemailerMailgun = nodemailer.createTransport(mg(auth));
nodemailerMailgun.sendMail({
from: 'myemail@example.com',
to: 'recipient@domain.com', // An array if you have multiple recipients.
cc:'second@domain.com',
bcc:'secretagent@company.gov',
subject: 'Hey you, awesome!',
'h:Reply-To': 'reply2this@company.com',
//You can use "html:" to send HTML email content. It's magic!
html: '<b>Wow Big powerful letters</b>',
//You can use "text:" to send plain-text content. It's oldschool!
text: 'Mailgun rocks, pow pow!'
}, (err, info) => {
if (err) {
console.log(`Error: ${err}`);
}
else {
console.log(`Response: ${info}`);
}
});
缓冲支持
例子:
const mailOptions = {
...
attachments: [
{
filename: 'text2.txt',
content: new Buffer('hello world!','utf-8')
},
以编码字符串作为附件内容:
const mailOptions = {
...
attachments: [
{
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
使用编码字符串作为内联附件:
// Replace `filename` with `cid`
const mailOptions = {
...
attachments: [
{
cid: 'logo.png',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
<!-- Reference the `cid` in your email template file -->
<img src="cid:logo.png" alt="logo" />
地址对象
“from”、“to”、“cc”和“bcc”字段支持地址对象或地址对象数组。每个“名称”和“地址”都被转换为 "name <address>"
格式。“名称”是可选的,“地址”是必需的。跳过对象中的缺失地址或空地址。
例子:
from: {name: 'Sales', address: 'sales@example.com'},
to: [{name:'Mary', address:'mary@differentexample.com'}, {address:'john@anotherexample.com'}]
转换为:
from: 'Sales <sales@example.com>',
to: 'Mary <mary@differentexample.com>,john@anotherexample.com'
现在使用 Consolidate.js 模板,并且还与 mailgun 自己的模板兼容
这个包有两个模板选项——一个是允许mailgun的模板引擎处理模板,另一个是使用consolidate.js支持的任何模板引擎在你自己的代码库中使用模板。
要使用 mailgun 的模板引擎(并允许模板独立于代码库进行迭代),只需将模板名称传递给template
键,并将模板变量作为字符串化的 JSON 传递给h:X-Mailgun-Variables
键。举个例子:
nodemailerMailgun.sendMail({
from: 'myemail@example.com',
to: 'recipient@domain.com', // An array if you have multiple recipients.
subject: 'Hey you, awesome!',
template: 'boss_door',
'h:X-Mailgun-Variables': JSON.stringify({key:'boss'})
}, (err, info) => {
if (err) {
console.log(`Error: ${err}`);
}
else {
console.log(`Response: ${info}`);
}
});
要在本地使用 consolidate.js 模板,请为template
键提供一个对象,而不是包含一个name
键、一个engine
键和可选的context
对象。例如,您可以使用 Handlebars 模板为您的消息生成 HTML,如下所示:
const handlebars = require('handlebars');
const contextObject = {
variable1: 'value1',
variable2: 'value2'
};
nodemailerMailgun.sendMail({
from: 'myemail@example.com',
to: 'recipient@domain.com', // An array if you have multiple recipients.
subject: 'Hey you, awesome!',
template: {
name: 'email.hbs',
engine: 'handlebars',
context: contextObject
}
}, (err, info) => {
if (err) {
console.log(`Error: ${err}`);
}
else {
console.log(`Response: ${info}`);
}
});
您可以使用Consolidate.js支持的任何模板引擎。只需要脚本中的引擎模块,并将引擎名称的字符串传递给template
对象。有关支持的引擎,请参阅 Consolidate.js 文档。
邮枪地区
您可以为 mailgun 域使用两个不同的区域环境。对于美国地区,您应该使用 api 端点api.mailgun.net
,但对于欧盟地区api.eu.mailgun.net
您可以将其作为“主机”传递给传输选项对象:
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const options = {
auth: {
api_key: 'key-1234123412341234',
domain: 'one of your domain names listed at your https://mailgun.com/app/domains'
},
host: 'api.eu.mailgun.net' // e.g. for EU region
}
const nodemailerMailgun = nodemailer.createTransport(mg(auth));
[1]快速安装依赖
npm install nodemailer
npm install nodemailer-mailgun-transport
旧版 Node.js 版本
7.6 之前的 Node.js 版本通过nodemailer-mailgun-transport/es5
.
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport/es5');