Hey I'm using mailgun to try to send emails so I'm using this script:
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);
});
})
I think I have an error in the domain name but I'm pasting it exactly as it appears in mailgun console panel in their website:
Can anyone paste me an example of how the domain name should look?
This is the error I'm getting:
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
This worked for me:
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);
});
My working example sets the Mailgun domain to "sandbox-blahblahblah.mailgun.org"
Take a look at this documentation: https://www.npmjs.com/package/mailgun-js - I do not think you should be setting the domain to their API root, but rather to the domain you have set to send from through Mailgun.
Edit with code example:
I export the following from my main config file (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'
In my Mailgun config file (mailgun.js), I have the following:
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);
});
}
Then from my controller, I can import my Mailgun config:
const mailgun = require('../config/mailgun');
And I can send an email:
const message = {
subject: 'Subject here',
text: 'Some text'
}
// Otherwise, send user email via Mailgun
mailgun.sendEmail(user.email, message);
Here is a repo with some things I need to fix, but the Mailgun integration works: https://github.com/joshuaslate/mern-starter/tree/master/server/config