【问题标题】:Node-imap sending emails but not saving it to SentNode-imap 发送电子邮件但未将其保存到已发送
【发布时间】:2018-04-19 06:30:41
【问题描述】:

最近一直在使用node-imap,并尝试实现发送电子邮件。这是羽毛/节点上的代码:

  create(data, params) {

    return new Promise((resolve, reject) => {


      // create reusable transporter object using the default SMTP transport
      let transporter = nodemailer.createTransport({
        host: this.host,
        port: this.smtpPort,
        secure: false, // true for 465, false for other ports,
        tls: {
          rejectUnauthorized: false
        },
        auth: {
          user: this.emailUsername, // generated ethereal user
          pass: this.emailPassword // generated ethereal password
        }
      });

      // setup email data with unicode symbols
      let mailOptions = {
        from: this.emailUsername, // sender address
        to: data.to, // list of receivers
        subject: data.subject, // Subject line
        text: data.body, // plain text body
        html: data.body // html body
      };

      // send mail with defined transport object
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          reject(error);
          //console.log(error);
        }
        //console.log('Message sent: %s', info.messageId);
        return resolve(info);
      });

    });
  }

此代码运行良好并按应有的方式发送电子邮件,但在完成其工作后,我无法在“已发送”框中找到这些电子邮件...任何有 node-imap 经验的人,我错过了什么?干杯。

编辑:: 刚刚意识到它确实为某些电子邮件提供商(Gmail、Hotmail)保存了它,但对于其他一些电子邮件提供商却没有。所以我想我没有遗漏任何东西......但是我怎样才能为其他不自动执行的提供商手动保存它。

【问题讨论】:

    标签: imap node-imap


    【解决方案1】:

    看来问题在于电子邮件提供商,因为此代码适用于 Gmail 或 Hotmail...所以我所做的是在已为该特定电子邮件提供商发送电子邮件后手动将电子邮件附加到“已发送”框...继承人如果需要的话,代码:

            // send mail with defined transport object
            transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                reject(error);
                //console.log(error);
              }
    
              if (checkEmail(userEmail) == 'linksoft') {
                imap.once('ready', function () {
                  imap.openBox('inbox.Sent', false, (err, box) => {
                    if (err) throw err;
    
                    let msg, htmlEntity, plainEntity;
                    msg = mimemessage.factory({
                      contentType: 'multipart/alternate',
                      body: []
                    });
                    htmlEntity = mimemessage.factory({
                      contentType: 'text/html;charset=utf-8',
                      body: mailOptions.html
                    });
                    plainEntity = mimemessage.factory({
                      body: mailOptions.text
                    });
                    msg.header('Message-ID', '<1234qwerty>');
                    msg.header('From', mailOptions.from);
                    msg.header('To', mailOptions.to);
                    msg.header('Subject', mailOptions.subject);
                    msg.header('Date', new Date()));
                    //msg.body.push(htmlEntity);
                    msg.body.push(plainEntity);
    
                    imap.append(msg.toString());
    
                  })
                });
    
                imap.once('error', function (err) {
                  reject(err);
                });
    
                imap.once('end', function () {
                  resolve(response);
                });
    
                imap.connect();
              }
              //console.log('Message sent: %s', info.messageId);
              return resolve(info);
            });
    

    依赖:mimemessage module 用于在将 mime 消息存储到“已发送”框之前生成 mime 消息,因为 imap.append() 需要 mime 消息类型

    【讨论】:

      猜你喜欢
      • 2014-03-18
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多