メールの送信#
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransport({
service: 'QQ',
auth: {
user: '[email protected]',
pass: 'xxx'
}
});
const send = (text: string, link: string) => {
let mailOptions = {
from: '発信者',
to: ["受信者"],
subject: '副題',
html: ""
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.error(err);
} else {
console.log('メールの送信が完了しました')
}
});
}
メールの受信#
const Imap = require("imap");
const { MailParser } = require("mailparser");
var imap = new Imap({
user: "[email protected]",
password: "xx",
host: "imap.qq.com",
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
});
const receive = async () => {
const lastMessage = await new Promise((resolve) => {
let lastMessageText = "";
function openInbox(cb) {
imap.openBox("INBOX", true, cb);
}
imap.once("ready", function () {
openInbox(function (err, box) {
if (err) throw err;
// 今日のすべてのメールを取得します。(時分秒まで正確には取得できません)
imap.search(["ALL", ["SINCE", Date.now()]], function (err, results) {
if (err) throw err;
var f = imap.fetch(results, { bodies: "" });
f.on("message", function (msg, seqno) {
var mailparser = new MailParser();
msg.on("body", function (stream) {
stream.pipe(mailparser);
mailparser.on("data", function (data) {
if (data.type === "text" && data.text) // テキストを受信します(画像も可能です)
lastMessageText = data.text;
});
});
});
f.once("end", function () {
imap.end();
});
});
});
});
imap.once("error", function (err) {
console.log(err);
});
imap.once("end", function () {
resolve(lastMessageText);
});
imap.connect();
});
return lastMessage;
};