域名邮箱邮件转发到QQ邮箱
不知道从什么时候开始,QQ的域名邮箱关闭了,所以当你需要一个或者多个域名邮箱的时候,就需要收费去购买企业邮箱。如果你恰巧有一台Linux服务器,又不想买企业邮箱,那么这篇文章就很适合你。
思路如下:
在Linux上搭建postfix服务,域名A记录mail.xxx.com到服务器IP,MX记录为mail.xxx.com,
在postfix配置main.cf里写一条
virtual_alias_maps = hash:/etc/postfix/virtual
virtual文件里写入
@xxx.com admin@xxx.com
在终端里执行useradd admin
这样就有一个admin@xxx.com的邮件地址,并且任意*@xxx.com的邮件都会转发到admin@xxx.com
在目录/home/admin/Maildir/new下就会出现收到的邮件
此时我们只需要监听这个目录,并且将邮件通过SMTP转发到自己常用的邮箱就行。
const fs = require('fs'); const path = require('path'); const { simpleParser } = require('mailparser'); const nodemailer = require('nodemailer'); const chokidar = require('chokidar'); // 创建 SMTP transporter const transporter = nodemailer.createTransport({ host: 'smtp.qq.com', // SMTP 服务器地址 port: 465, // SMTP 服务器端口 secure: true, // true for 465, false for other ports auth: { user: '', // 你的邮箱用户名 pass: '' // 你的邮箱密码或授权码 } }); const watcher = chokidar.watch('/home/admin/Maildir/new/'); const targetDirectory = '/home/admin/Maildir/cur/'; watcher.on('add', (pathString) => { fs.readFile(pathString, 'utf8', (err, data) => { if (err) { console.error('Error reading mail file:', err); return; } // 使用 mailparser 解析邮件 simpleParser(data, (err, mail) => { if (err) { console.error('Error parsing mail:', err); return; } const mailOptions = { from: '', // 发件人邮箱地址 to: '', // 收件人邮箱地址 subject: mail.subject, // 邮件主题 text: mail.text, html: '' }; if (mail.html) { mailOptions.html = mail.html; } // 发送邮件 transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.error('Error sending mail:', error); } console.log('Message sent: %s', info.messageId); }); // 移动文件 const fileName = path.basename(pathString); const targetFilePath = path.join(targetDirectory, fileName); // 移动文件 fs.rename(pathString, targetFilePath, (err) => { if (err) { console.error('Error moving file:', err); } else { console.log('File moved successfully'); } }); }); }); });
package.json
{
"dependencies": {
"chokidar": "^3.5.3",
"mailparser": "^3.6.5",
"nodemailer": "^6.9.7"
}
}