Jeka Jeka - 6 months ago 89
Node.js Question

Example of the domain name for mailgun be for nodejs?

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: enter image description here

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

Answer

This worked for me:

var api_key = 'key-################';
var domain = 'mydomain.com.mailgun.org';
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);
});