Nodejs邮件发送插件node-mailgun的使用

Nodejs邮件发送插件node-mailgun的使用
node-mailgun 是一个用于 Node.js 应用程序中发送邮件的库,它基于 Mailgun API。在开始之前,请确保你已经在 Mailgun 上注册并创建了一个域名,获取了 API 密钥和域名密钥。

以下是如何使用 node-mailgun 发送邮件的基本步骤:

1. 安装 node-mailgun

首先,你需要安装 node-mailgun 库。你可以通过 npm 来安装:

npm install mailgun-js

2. 初始化 node-mailgun

接下来,你需要初始化 mailgun-js,并提供你的 API 密钥、私钥和域名:

const api_key = 'your_api_key';
const domain = 'your_domain';
const mailgun = require('mailgun-js')({ apiKey: api_key, domain: domain });

3. 发送邮件

一旦初始化完成,你就可以使用 mailgun.messages() 方法来发送邮件。这里是一个基本的例子:

const data = {
    from: 'sender@example.com',
    to: 'recipient@example.com',
    subject: 'Hello',
    text: 'Testing some Mailgun awesomness!'
};

mailgun.messages().send(data, function (error, body) {
    console.log(body);
});

4. 异步发送邮件

如果你希望以异步方式发送邮件,可以使用 Promise 或 async/await:

async function sendEmail() {
    try {
        const data = {
            from: 'sender@example.com',
            to: 'recipient@example.com',
            subject: 'Hello',
            text: 'Testing some Mailgun awesomness!'
        };

        const message = await new Promise((resolve, reject) => {
            mailgun.messages().send(data, (error, body) => {
                if (error) {
                    reject(error);
                } else {
                    resolve(body);
                }
            });
        });

        console.log(message);
    } catch (error) {
        console.error(error);
    }
}

sendEmail();

5. 高级配置

你还可以添加更多的选项,如 CC、BCC、HTML 内容等:

const data = {
    from: 'sender@example.com',
    to: ['recipient1@example.com', 'recipient2@example.com'],
    cc: 'cc@example.com',
    bcc: 'bcc@example.com',
    subject: 'Hello',
    text: 'This is a test email.',
    html: '<h1>This is a test email.</h1>'
};

mailgun.messages().send(data, function (error, body) {
    console.log(body);
});

以上就是如何使用 node-mailgun 发送邮件的基本指南。记得替换示例中的电子邮件地址和内容为你自己的信息。


3 回复

当然,要使用Node.js中的node-mailgun插件来发送邮件,你需要先安装它。打开你的终端,运行:

npm install node-mailgun --save

接下来,在你的项目中引入并配置它。假设你已经有了Mailgun的API密钥和域名:

const Mailgun = require('node-mailgun').Mailgun;
const mg = new Mailgun('your-api-key');
const domain = mg.domain('your-domain.mailgun.org');

domain.sendMessage({
  from: 'Sender <sender@your-domain.mailgun.org>',
  to: 'Receiver <receiver@example.com>',
  subject: 'Hello',
  text: 'Testing some Mailgun awesomness!'
}, (err, body) => {
  if (err) console.log(err);
  else console.log(body);
});

别忘了替换your-api-keyyour-domain.mailgun.org为你的实际信息。这样,你就可以开始用Node.js发送邮件了!祝你发送愉快!


node-mailgun 是一个用于Node.js应用中发送邮件的库,它是基于Mailgun API的。这里将介绍如何安装和基本使用 node-mailgun

1. 安装

首先,你需要安装 node-mailgun。可以通过npm来安装:

npm install mailgun-js

2. 基本使用

接下来,我们需要配置 mailgun-js 并编写代码来发送邮件。首先,你需要一个Mailgun账户,并且需要知道你的API密钥、域名和从哪个邮箱地址发送邮件。

假设你已经有了这些信息:

  • API Key: your-api-key
  • Domain: your-domain.com
  • 发送邮箱地址: no-reply@your-domain.com

下面是发送邮件的基本示例代码:

const mailgun = require('mailgun-js')({
    apiKey: 'your-api-key',
    domain: 'your-domain.com'
});

// 邮件内容
const data = {
    from: 'no-reply@your-domain.com',
    to: 'recipient@example.com',
    subject: 'Hello from Mailgun!',
    text: 'This is a test email sent via Mailgun using node-mailgun.'
};

// 发送邮件
mailgun.messages().send(data, function (error, body) {
    if (error) {
        console.error('Failed to send email:', error);
    } else {
        console.log('Email sent successfully:', body);
    }
});

这段代码做了以下几件事:

  1. 引入并初始化 mailgun-js 模块。
  2. 准备要发送的邮件数据(发件人、收件人、主题和文本内容)。
  3. 调用 mailgun.messages().send() 方法来发送邮件。
  4. 提供了一个回调函数来处理发送的结果。

3. 注意事项

  • 确保你的API密钥和域名是正确的。
  • 如果你在本地测试,请确保你的环境能够访问外部网络,因为发送邮件需要连接到Mailgun服务器。
  • 在生产环境中,你可能需要处理更多的错误情况和日志记录。

以上就是如何使用 node-mailgun 发送邮件的基本步骤。希望这对你有所帮助!

node-mailgun 是一个用于通过 Mailgun API 发送电子邮件的 Node.js 模块。首先,你需要安装它:

npm install node-mailgun

然后,你可以这样使用:

const mailgun = require('node-mailgun')(YOUR_API_KEY, YOUR_DOMAIN_NAME);

mailgun.messages().send({
  from: 'Sender Name <sender@example.com>',
  to: 'Receiver Name <receiver@example.com>',
  subject: 'Hello',
  text: 'Hello, this is a test email sent by node-mailgun.'
}, function (err, body) {
  if (!err) {
    console.log(body);
  } else {
    console.error(err);
  }
});

确保替换 YOUR_API_KEYYOUR_DOMAIN_NAME 为你的实际信息。

回到顶部