1
votes

Been using node-imap lately and trying to implement sending emails. Here is the code on feathers/node:

  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);
      });

    });
  }

this code is running fine and sending email as it should, but after it does its job i cant find those emails in 'Sent' box... anyone experienced with node-imap, what am i missin? Cheers.

EDIT:: Just realised It does save it for some email providers (Gmail, Hotmail) but for some others it doesnt. So I guess i'm not missing anything... but how could I save it manually for other providers who doesnt do it automatically.

1

1 Answers

1
votes

Well seems problem was the email provider cause this code works just fine for Gmail or Hotmail... so what i did is manually append email to 'Sent' box after its already sent for that certain email provider... heres the code if any1 needs:

        // 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);
        });

Dependency: mimemessage module which is used for generating mime message before storing it into 'Sent' box cause mime message type is required for imap.append()