[英]Example of the domain name for mailgun be for nodejs?
嘿,我正在使用 mailgun 尝试发送电子邮件,所以我正在使用以下脚本:
var Mailgun = require('mailgun-js');
//get requests
expressApp
.get("/", function routeHandler(req, res) {
res.sendFile(path.join(__dirname, "../client/index.html"));
var api_key = 'key-00000000000000000000';
var domain = "https://api.mailgun.net/v3/mydomain.com"; //I think the error must be here
var mailgun = new Mailgun({apiKey: api_key, domain: domain});
var data = {
from: "me@mydomain.com", //I tried also with me@samples.mailgun.org which was in the example
to: 'myemail@gmail.com',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!'
};
mailgun.messages().send(data, function (err, body) {
if (err) {
console.log("error ", err);
}
console.log(body);
});
})
我想我的域名有错误,但我正在粘贴它,就像它在他们网站的 mailgun 控制台面板中显示的那样:
任何人都可以粘贴给我一个域名应该是什么样子的例子吗?
这是我得到的错误:
error { [Error: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.</p><p>If you entered the URL manually please check your spelling and try again.</p>
] statusCode: 404 }
undefined
我的工作示例将 Mailgun 域设置为"sandbox-blahblahblah.mailgun.org"
看看这个文档: https : //www.npmjs.com/package/mailgun-js - 我认为你不应该将域设置为他们的 API 根,而是你设置为通过发送的域邮枪。
使用代码示例进行编辑:
我从我的主配置文件 (main.js) 中导出以下内容:
// Configuring Mailgun API for sending transactional email
'mailgun_priv_key': 'key-XXXXXXXXXXXXXX',
// Configuring Mailgun domain for sending transactional email
'mailgun_domain': 'sandboxXXXXXXXXXXXXXX.mailgun.org'
在我的 Mailgun 配置文件 (mailgun.js) 中,我有以下内容:
const config = require('./main');
const mailgun = require('mailgun-js')({ apiKey: config.mailgun_priv_key,
domain: config.mailgun_domain });
// Create and export function to send emails through Mailgun API
exports.sendEmail = function(recipient, message) {
const data = {
from: 'My Name <email@mydomain.com>',
to: recipient,
subject: message.subject,
text: message.text
};
mailgun.messages().send(data, function(error, body) {
console.log(body);
});
}
然后从我的控制器,我可以导入我的 Mailgun 配置:
const mailgun = require('../config/mailgun');
我可以发送电子邮件:
const message = {
subject: 'Subject here',
text: 'Some text'
}
// Otherwise, send user email via Mailgun
mailgun.sendEmail(user.email, message);
这是一个包含我需要修复的一些内容的 repo,但 Mailgun 集成有效: https : //github.com/joshuaslate/mern-starter/tree/master/server/config
这对我有用:
var api_key = 'key-################';
var domain = 'mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var data = {
from: 'Excited User <me@mydomain.com>',
to: 'recepeint@gmail.com',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!'
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});
到 2020 年左右,我发布了您将 mailgun 的 api 用于欧盟 mailgun 服务器的工作示例。 您需要为 mailgun-js 构造函数指定主机。 我也在这里使用快递。
let mailgun = require("mailgun-js")({
apiKey: "key-xxxxxxxxxxx",
domain: "YOUR-DOMAIN.com", // or MG.YOUR-DOMAIN.com NOT https://api.eu.mailgun.net/v3/YOUR-DOMAIN.com
host: "api.eu.mailgun.net"
});
router.get("/sendMail", (req, res) => {
const data = {
from: "no-reply@YOUR-DOMAIN.com",
to: "bob@example.com",
subject: "Hello",
html: "<b>Testing some Mailgun awesomeness!</b>"
};
mailgun.messages().send(data, (error, body) => {
if(!error) res.send("Hurray! Email sent.");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.