如何使用 Mailer To Go 在 Ruby、Python、Node.js 和 Go 中发送电子邮件
今天通过 Mailer To Go 发送事务性邮件的方式是 SMTP:将您框架的内置邮件程序指向我们的提交端点,几分钟内即可开始发送,支持任何语言。本指南包含 Ruby、Python、Node.js 和 Go 的工作示例。(JSON REST API 正在开发中 — 请参阅末尾的说明。)
这里的一切都是 数据平面 — 实际发送邮件。控制平面(投递/退回事件、入站接收和配置)是一个独立的界面,稍后会有单独的指南。
获取您的凭证
从您的 仪表板,获取您的 SMTP 用户名和密码,并将它们设置为环境变量。在 Heroku 和 Build.io 上,配置插件时这些会自动设置为配置变量。
export MAILERTOGO_SMTP_HOST=smtp.mailertogo.com # 确切的主机显示在您的仪表板中
export MAILERTOGO_SMTP_USER=... # 来自仪表板
export MAILERTOGO_SMTP_PASSWORD=... # 来自仪表板
从您已验证的域上的地址发送(请参阅 域名设置和身份验证);Mailer To Go 拒绝未验证的 From: 域。
通过 SMTP 发送
来自您仪表板的 SMTP 主机,端口 587(STARTTLS) 或 465(SSL)。因为它是标准 SMTP,您语言的内置邮件程序已经支持它。
Ruby(mail gem):
require "mail"
Mail.defaults do
delivery_method :smtp,
address: ENV["MAILERTOGO_SMTP_HOST"], port: 587,
user_name: ENV["MAILERTOGO_SMTP_USER"],
password: ENV["MAILERTOGO_SMTP_PASSWORD"],
authentication: :plain, enable_starttls_auto: true
end
Mail.deliver do
from "Acme <hello@yourdomain.com>"
to "user@example.com"
subject "Hello from Ruby"
body "Your email body here."
end
Python(标准库 smtplib):
import os, smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = "Acme <hello@yourdomain.com>"
msg["To"] = "user@example.com"
msg["Subject"] = "Hello from Python"
msg.set_content("Your email body here.")
with smtplib.SMTP(os.environ["MAILERTOGO_SMTP_HOST"], 587) as s:
s.starttls()
s.login(os.environ["MAILERTOGO_SMTP_USER"], os.environ["MAILERTOGO_SMTP_PASSWORD"])
s.send_message(msg)
Node.js(Nodemailer):
const nodemailer = require("nodemailer");
const transport = nodemailer.createTransport({
host: process.env.MAILERTOGO_SMTP_HOST,
port: 587,
auth: { user: process.env.MAILERTOGO_SMTP_USER, pass: process.env.MAILERTOGO_SMTP_PASSWORD },
});
await transport.sendMail({
from: "Acme <hello@yourdomain.com>",
to: "user@example.com",
subject: "Hello from Node",
text: "Your email body here.",
});
Go(标准库 net/smtp):
package main
import (
"net/smtp"
"os"
)
func main() {
host := os.Getenv("MAILERTOGO_SMTP_HOST")
auth := smtp.PlainAuth("", os.Getenv("MAILERTOGO_SMTP_USER"), os.Getenv("MAILERTOGO_SMTP_PASSWORD"), host)
msg := []byte("To: user@example.com\r\n" +
"From: Acme <hello@yourdomain.com>\r\n" +
"Subject: Hello from Go\r\n\r\n" +
"Your email body here.\r\n")
if err := smtp.SendMail(host+":587", auth, "hello@yourdomain.com", []string{"user@example.com"}, msg); err != nil {
panic(err)
}
}
发送 HTML 而不是纯文本?设置消息的 HTML 部分(Ruby mail gem 中的 html_part、Python 中的 add_alternative(..., subtype="html")、Nodemailer 中的 html 字段或 Go 中的 Content-Type: text/html 标头) — 其他一切保持不变。
生产提示
- 将凭证保存在环境变量中,从不在源代码中。在 Heroku/Build.io 上,它们已经是配置变量。
- 从您已验证的域发送,最好是 像
mtg.yourdomain.com这样的子域。 - 使用端口 587 和 STARTTLS(或 465 和 SSL) — 始终传输中加密。
- 从后台任务发送,不是在 Web 请求中内联发送,并使用退避重试瞬间失败。硬退回会自动添加到您的禁止列表。
REST API 正在开发中
对于喜欢发出 HTTP 调用而不是保持 SMTP 连接的团队,JSON 发送 API 即将推出。计划的形式:
POST https://api.mailertogo.com/api/v1/emails
Authorization: Bearer <your API key>
Content-Type: application/json
{
"from": "hello@yourdomain.com",
"to": "user@example.com", // 或数组,最多 50 个收件人
"subject": "Hello",
"html": "...", "text": "...", // 提供其中之一或两者
"reply_to": "support@yourdomain.com",
"tags": [ { "name": "campaign", "value": "welcome" } ]
}
它将返回排队消息的 id。此端点尚未上线 — 今天通过 SMTP 发送,REST API 发布时我们会更新此帖。(更广泛的控制平面 API — 事件、入站接收、配置 — 单独追踪。)
在特定平台上部署?请参阅 从 Heroku 发送事务性邮件。从另一个提供商迁移?请参阅 SendGrid、Mailgun 和 Amazon SES 的指南。