nodemailer
In order to send messages, those three would be following.
nodemailer 설치하고 transporter 객체 생성하기
//install Nodemailer from npm
npm install nodemailer
//generate SMTP service account from etherea.email
const nodemailer = require("../lib/nodemailer");
nodemailer.createTestAccount((err, account) => {
if(err){
console.err('Failed to create a testing account');
console.err(err);
return process.exit(1);
}
console.log('Credentials obtained, sending message...');
//create a SMTP transporter object (to send emails you need a transporter object)
let transporter = nodemailer.createTransport(transport[, defaults]);
//defining transport와 default
let transporter = nodemailer.createTransport({
host: account.smtp.host;
port: account.smtp.port,
secure: account.smtp.port,
auth: {
user: account.user,
pass: account.pass
},
logger: true,
transactionLog: true
},
{ //default message fields
//sender info
from: 'Nodemailer <example@nodemailer.com>',
});
메시지 옵션 작성하고 이메일 전송하기
//Once you have a transporter object you can send mail with it
const message = {
to: 'Nodemailer <example@nodemailer.com>',
subject: 'Nodemailer is unicode friendly ✔' + Date.now(),
text: 'Hello to myself',
html: '<p><b>Hello</b> to myself</p>',
amp: `<!doctype html>
<html ⚡4email>
<head>
<meta charset="utf-8">
<style amp4email-boilerplate>body{visibility:hidden}</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
</head>
<body>
<p><b>Hello</b> to myself</p>
</body>
</html>`,
}
transporter.sendMail(data[, callback]);
transporter.sendMail(message, (error, info)=>{
if(error){
console.log('Error occurred');
console.log(error.message);
return process.exit(1);
}
console.log('Message sent successfully!');
console.log(nodemailer.getTestMessageUrl(info));
//only needed when using pooled connections
transporter.close();
});
});
필수
전송할 메일 내용 옵션
메시지가 전송에 성공하거나 실패했을때 동작하는 함수"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo@example.com>', // sender address
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
reference