当前位置:首页 > Nodejs > 正文内容

域名邮箱邮件转发到QQ邮箱

4个月前 (12-26)Nodejs

不知道从什么时候开始,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"

  }

}


扫描二维码推送至手机访问。

版权声明:本文由小祥子的博客发布,如需转载请注明出处。

本文地址:http://www.xiaoxiangzi.com/post/1708.html

返回列表

上一篇:NodeJS搭建Socket服务器

没有最新的文章了...

相关文章

NodeJS搭建Socket服务器

之前想做一个越狱插件让手机与服务器实时通讯,只有Socket长连接才可以实现,最开始想到的是Delphi和QT5来做一个挂在服务端的软件,因为我用的Mac系统,所以开发起来很不方便。后来发现NodeJ...