简体   繁体   中英

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: 在此处输入图片说明

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

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

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);
});

As of near 2020 I post You working example of mailgun's api use for EU mailgun's servers. You need to specify host for mailgun-js constructor. I am also using express here.

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.");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM