mailgun的域名示例是针对nodejs的吗?

时间:2016-06-02 15:13:57

标签: node.js express mean-stack mailgun

嘿我使用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

3 个答案:

答案 0 :(得分:2)

我的工作示例将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);
    });
  }

然后从我的控制器,我可以导入我的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

答案 1 :(得分:1)

这对我有用:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}

class Classifier classifier a m c | classifier -> m c where
  classify :: (Monad m) => classifier -> [a] -> m [c]
  cluster :: classifier -> [Partitioner] -> c -> Cluster Partitioner a

instance Classifier (MappingClassifierM FileSummary IO Partitioner) FileSummary IO (P
  classify = classifyM
  cluster _ x (key, val) = Cluster (key : x) val

instance Classifier (BinaryClassifierM FileSummary IO) FileSummary IO [FileSummary] w
  classify = classifyBinary
  cluster _ clusterKey = Cluster (Content : clusterKey)

dig :: (Classifier classifier FileSummary IO c) => classifier -> Conduit (Cluster Par
dig classifier =
  await >>= \case
    Nothing -> return ()
    Just ( Cluster clusterKey clusterValue ) -> do
      categories <- liftIO $ classify classifier clusterValue

      when (length clusterValue == length categories) $
        yield $ Cluster clusterKey clusterValue

      when (length clusterValue /= length categories) $
        mapM_ (yield . (cluster classifier clusterKey)) categories

      dig classifier

答案 2 :(得分:0)

截至2020年左右,我将向您发布用于EU mailgun服务器的mailgun api使用的工作示例。您需要为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.");
    });
});
相关问题