【问题标题】:How to listen to all the inbound emails for an Email Server如何收听电子邮件服务器的所有入站电子邮件
【发布时间】:2021-11-03 07:36:18
【问题描述】:

我需要创建一个可能有 100 万封电子邮件的电子邮件服务器。 我想收听所有的电子邮件帐户,基本上是收听收到的电子邮件(每天大约 20k)

我有以下 node.js 脚本,它只收听一封电子邮件。 但我想收听所有所有电子邮件帐户的所有新电子邮件。 我怎样才能做到这一点?欢迎任何想法。 或者是否有任何其他提供该功能的电子邮件服务器。

我的目标是能够在服务器上有新电子邮件时触发事件并保存该电子邮件的附件。

var MailListener = require("mail-listener4");
 
var mailListener = new MailListener({
  username: 'something@example.com',
  password: 'password',
  host: 'mail.example.com',
  port: 993, // imap port
  tls: true,
  connTimeout: 10000, // Default by node-imap
  authTimeout: 5000, // Default by node-imap,
  debug: console.log, // Or your custom function with only one incoming argument. Default: null
  tlsOptions: { rejectUnauthorized: false },
  mailbox: 'INBOX', // mailbox to monitor
  searchFilter: ["ALL"], // the search filter being used after an IDLE notification has been retrieved
  markSeen: true, // all fetched email willbe marked as seen and not fetched next time
  fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
  mailParserOptions: {stream: true}, // options to be passed to mailParser lib.
  attachments: true, // download attachments as they are encountered to the project directory
  attachmentOptions: { directory: "attachments/" } ,
  searchFilter: ["UNSEEN"], 
  markSeen: true, // specify a download directory for attachments
});
 
mailListener.start(); 
 
mailListener.on("server:connected", function(){
  console.log("imapConnected");
});
 
mailListener.on("mailbox", function(mailbox){
  console.log("Total number of mails: ", mailbox.messages.total); // this field in mailbox gives the total number of emails
});
 
mailListener.on("server:disconnected", function(){
  console.log("imapDisconnected");
});
 
mailListener.on("error", function(err){
  console.log(err);
});
 
mailListener.on("mail", function(mail, seqno, attributes){
});
 
mailListener.on("attachment", function(attachment){
  console.log(attachment.path);
});
 

如果需要,我可以详细说明我的要求。

【问题讨论】:

  • 如果您要托管服务器,最好将脚本插入 SMTP 服务器。大多数 unix 风格的服务器都支持在邮件路径中放置“过滤器”或类似的东西,这将允许您在通过该服务器的每封传入电子邮件上触发某些内容。试图通过 IMAP 获取它们是一场失败的战斗。
  • 您是否需要创建一个完整的邮件服务器来满足您的要求,或者您是否正在使用某些提供商(gmail、hotmail、yahoo、yandex)并且只需要在邮件到达时收到一种通知您的用户收件箱?
  • 我需要创建整个电子邮件服务器,因为我必须为每个订阅者分配一个电子邮件帐户,然后每当我们需要存储的任何电子邮件中有新电子邮件时。

标签: node.js email smtp imap


【解决方案1】:

假设您正在处理理想的条件,并且您不必担心性能、基础设施、特权或任何您可以通过基本上为该服务器中的每个帐户创建一个连接并监听您可以检查的连接来做到这一点这个问题可以帮助您了解如何做到这一点How can i handle multiple imap connections in node.js? 基本上,如果您可以访问邮件服务器使用的帐户库,例如 LDAP 服务器或类似的东西,您可以创建一个连接数组可以单独收听:

  var Connection = []; 

  function connectImap(username, password, address, port, tls) {

   if (typeof Connection[username]             != typeof undefined &&
       typeof Connection[username].state       == typeof ''        &&
       Connection[username].state              == 'authenticated'  &&
       Connection[username]._config.user       == username         &&
       Connection[username]._config.password   == password) {

       console.log('IMAP-CLIENT-USE-AUTHENTICATED-CONNECTION ' + username);

   } else {

         port = port || 993;
         tls = tls || true;

      Connection[username] = new Imap({
            user        : username,
            password    : password,
            host        : address,
            port        : port,
            authTimeout : 10000,
            connTimeout : 10000,
            keepalive   : true,
            tls         : tls
           });

           console.log('IMAP-CLIENT-CONNECTED : ' + username);
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多