how to send emails with mailgun in nodejs
Here are the steps to send emails with Mailgun in Node.js:
-
Install the Mailgun SDK for Node.js by running the command
npm install mailgun-js. -
Require the Mailgun module in your Node.js application by adding the following code at the top of your file:
const mailgun = require("mailgun-js");
- Create an instance of the Mailgun class by passing in your API key and domain. You can find these credentials on your Mailgun account dashboard.
const api_key = "YOUR_API_KEY";
const domain = "YOUR_DOMAIN";
const mailgun = require("mailgun-js")({ apiKey: api_key, domain: domain });
- Create an email message by defining the sender, recipient, subject, and body of the email. You can also include attachments if needed.
const data = {
from: "Sender Name <sender@example.com>",
to: "Recipient Name <recipient@example.com>",
subject: "Test Email",
text: "This is a test email sent using Mailgun.",
attachment: [
{ data: fileBuffer, filename: "file.pdf" }
]
};
- Send the email by calling the
messages().send()method on the Mailgun instance and passing in the email data.
mailgun.messages().send(data, function(error, body) {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
- Test the email functionality by running your Node.js application and checking your email inbox for the test email.
原文地址: https://www.cveoy.top/t/topic/bcNM 著作权归作者所有。请勿转载和采集!