gpt4 book ai didi

node.js - mailgun 的域名示例适用于 Nodejs?

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:10 24 4
gpt4 key购买 nike

嘿,我正在使用 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 控制台面板中显示的方式粘贴:enter image description here

有人可以给我粘贴一个域名的示例吗?

这是我收到的错误:

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 根目录,而应该设置为您设置为通过 Mailgun 发送的域。

使用代码示例进行编辑:

我从主配置文件 (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);
});
}

然后从我的 Controller 中,我可以导入我的 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);

这是一个存储库,其中包含一些我需要修复的内容,但 Mailgun 集成可以工作:https://github.com/joshuaslate/mern-starter/tree/master/server/config

关于node.js - mailgun 的域名示例适用于 Nodejs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com