thelittlemaster
thelittlemaster

Reputation: 403

nodemailer erratic behaviour when deployed to azure

I am developing a backend server in nodeJs and at some point I need to generate a pdf and send it by email to the user. As technologies I am using GraphQl, Html-PDF and nodeJs.

I was using before puppeteer, but since the backend end server lies in Azure, it became a really headache to configure, so the reason why I changed to html-pdf.

now my code:

This is part of a resolver.

  let transporter = nodemailer.createTransport({
            service: "Outlook365",
            host: "smtp.office365.com",
            port: 587,
            secure: false,
            auth: {
              user: "[email protected]", 
              pass: "example", 
            },
          });
          
        var i;
        await pdf.create(html, options).toBuffer((err, buffer) => {
          i = buffer
          transporter.sendMail({
          from: '"example" [email protected]', 
          to: `${a1.dataValues.email}`, 
          subject: "Result Email ✔", 
          text:
            " Willkommen beim. Ihre Ergebnisse sind hier.Ihr/ Welcome to the Service. Your results are here.  ", 
          html: `<p>Willkommen beim . <br /> <br /> Ihre Ergebnisse sind hier. <br /> <br /> <br /> <br />      Welcome. <br /> <br /> Your results are here.<br /> <br /> </p>`, 
          attachments: [
            {
              filename: `${a1.dataValues.kitID}.pdf`,
              content: i,
            },
          ],
        })}
        )

this works locally but does not work when deployed to Azure. I have been using nodemailer in other parts of the server and usually inside callbacks it usually does not work, but I have tried as well to do like this:

 let transporter = nodemailer.createTransport({
            service: "Outlook365",
            host: "smtp.office365.com",
            port: 587,
            secure: false, 
            auth: {
              user: "[email protected]",
              pass: "example", 
            },
          });
          
        var i;
        await pdf.create(html, options).toBuffer((err, buffer) => {
          i = buffer
)}
          transporter.sendMail({
          from: '"example" [email protected]', 
          to: `${a1.dataValues.email}`,
          subject: "Result Email ✔", 
          text:
            " Willkommen beim. Ihre Ergebnisse sind hier       Welcome . Your results are here.",y
          html: `<p>Willkommen beim. <br /> <br /> Ihre Ergebnisse sind hier.  <br /> <br /> <br /> <br />      Welcome . <br /> <br /> Your results are here.<br /> <br /> </p>`, // html body
          attachments: [
            {
              filename: `${a1.dataValues.kitID}.pdf`,
              content: i,
            },
          ],
        }
        )

EDIT: Solved the problem of Azure sending the email, although the buffer comes "empty"

Upvotes: 1

Views: 91

Answers (1)

user13037493
user13037493

Reputation: 56

the first version should work. But you need to place your file on a buffer instead of only on a constant.

try changing

 {
              filename: `${a1.dataValues.kitID}.pdf`,
              content: i,
            },

to

 {
              filename: `${a1.dataValues.kitID}.pdf`,
              content: new Buffer(buffer),
            },

Upvotes: 1

Related Questions