Search code examples
node.jsimapemail

can't set "To" and "Subject" headers in imap append


I am building app where i send email to client with nodemailer and after that i want to add copy of that email to SENT mailbox on email client. The emails are added properly but without subject and header. I am using imap-simple package and I was following examples in docs, not sure why is that happening

I am connecting with imap to home.pl mailbox

imaps
      .connect(imapConfig)
      .then((connection) => {
        const message = `Content-Type: text/html;charset=utf-8
        From:e-recepta@przychodnia-primadent.pl
        To: ${email}
        Subject:Wniosek o e-recepte wysłany
        \r\n
        ${email}
      `;

        connection.append(message, {
          mailbox: "SENT",
          flags: "\\Sent",
        });
        return null;
      })
      .catch((err) => {
        console.log(err);
      });

Solution

  • The spaces before From, To and Subject aren't legal. You can use spaces almost anywere, these three are legal:

    From: fnord@example.com
    From : fnord@example.com
    From:      fnord@example.com
    

    But spaces before the From ungood. When something other than a header field name follows a CR, most parsers consider the header to end and the mail body to start. (Which is perhaps not quite comme il faut, I don't remember that part of the formal grammar. But someone who sends a buggy message cannot complain if parsers choose a different buggy interpretation than intended.)