[英]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.