1

I want to send Mails via the Postmark API from my express server. I get a post-request on my express server with the following json payload:

{
    "mail": {
        "from": "frommail@address.com",
        "to": "tomail@adrress.com",
        "subject": "Test mit Datei",
        "textBody": "Test mit Datei",
        "replyTo": "replyto@mail.com",
        "files": [
            {
                "name": "filename.pdf",
                "url": "https://sharelink.de"
            }
        ]
    }
}  

I need to iterate through the files array and download every file and convert it to base64, and then safe the result with the name and Content Type in a object which I can send over the Postmark API. As it is in the moment the mail is sent but without the files. I think this happens because the sendEmail function is not waiting for the encode2base64 function to be finished. And therefor its sending an empty array. But I dont know how to setup the await syntax in this scenario.

Here is my code so far:

    // Execution
    const from = req.body.mail.from;
    const to = req.body.mail.to;
    const subject = req.body.mail.subject;
    const htmlBody = req.body.mail.htmlBody;
    const textBody = req.body.mail.textBody;
    const replyTo = req.body.mail.replyTo;
    const files = req.body.mail.files;
    sendEmail(from, to, subject, htmlBody, textBody, replyTo, files)
    .catch((err)=>{
                    res.send({"error": err.message});
     })
    .then((response)=>{
                console.log("prom response ",  response.data );
     res.send( response.data );
     })
    //==========================================
    //===== SEND EMAIL WITH POSTMARK API =======
    //==========================================
    /*  FOR THE API THE ATTACHMENTS MUST BE IN THE FOLLOWING FORMAT
    "Attachments": [{
                        Name: text(filename),
                        Content: 'BASE64 ENCODED FILE',
                        ContentType: "application/octet-stream"
                    }]
            */
    async function sendEmail( from, to, subject, htmlBody, textBody, replyTo, files ){
      var attachments = [];
      if ( files ) {
        console.log("============ EMAIL WITH FILES ==============")
        
        for (i in files ){
          var fileName = files[i].name;
          var shareLink = files[i].url;
          console.log("fileName", fileName );
          console.log("url: ", shareLink );
          encodeFile2Base64( shareLink )
          .catch((err)=>{
            console.log(err)
            })
          .then((response)=>{
              const encoded = new Buffer.from(response.data).toString('base64');
              attachments.push({
                "Name": fileName,
                "Content": encoded,
                "ContentType": "application/octet-stream"
              })
              
            })
    
      }}
      var config = {
        method: 'post',
        url: 'https://api.postmarkapp.com/email',
        headers: {'Content-Type': 'application/json','Accept':'application/json','X- Postmark-Server-Token': process.env.POSTMARK_KEY},
        data:{
                "From": from,
                "To": to,
                "Subject": subject,
                "HtmlBody": htmlBody,
                "TextBody": textBody,
                "ReplyTo": replyTo,
          "Attachments": attachments
            }
      };
      const promSendMail = axios(config);
      const resMailSent = await promSendMail;
      return resMailSent
    }
    //==========================================
    
    //==========================================
    //======== ENCODE TO BASE64 ================
    //==========================================
    async function encodeFile2Base64(url){
      var config = {
        method: 'get',
        url : url,
        headers : { responseType: 'arraybuffer' }
      };
      const prom = axios( config );
      const data = await prom;
      return data
    }

Do I need to wrap the whole loop into an async function? Or should I first create the attachments object and then make the API Call to Postmark? But I would need to wait anyway.

1 Answers1

1

If you wish to use async / await you would need to wrap the loop in an async function.

The sendEmail function must return a Promise, and ideally this should reject on a send failure.

You can test this conceptually with some mock sending code so you get the logic / rationale.

You can use a for...of, for...in , for loop. Just don't use Array.forEach, this won't play nicely with await!

For example:

// Send successfully exept for email #2 (simulate an error)
function sendEmail(from, to, subject, body) {
    return new Promise((resolve, reject) => setTimeout(from === 'from2' ? reject: resolve, 1000, from === 'from2' ? "send failure " : "send success "));
}


let emails = Array.from( {length: 5}, (v,k) => ({ from: 'from' + k, to: "to" + k, subject: "subject" + k, body: "body" + k}));

async function sendEmails(emails, stopOnError) {
    for(let i = 0; i < emails.length; i++) {
        try { 
            console.log(`Sending email ${i+1} of ${emails.length}...`);
            let result = await sendEmail(emails[i].from, emails[i].to, emails[i].subject, emails[i].body);
            console.log("result:", result);
        } catch (error) {
            console.error(`sendEmails: error occurred at email #${i}:`, error);
            if (stopOnError) throw error
        }
    }
}

// Don't stop if we get an error, change to true to abandon sending on error
sendEmails(emails, false);
Terry Lennox
  • 17,423
  • 2
  • 18
  • 28
  • 1
    Thanks for the example code . At the moment my biggest Problem is the part where I build the attachment object. I need this array of objects with the filename and base64 encoded content. – SuperSpamTube Feb 17 '21 at 14:27
  • 1
    Found this for the axios in loop problem https://stackoverflow.com/questions/56532652/axios-get-then-in-a-for-loop – SuperSpamTube Feb 17 '21 at 14:45
  • 1
    That will work alright, you create an array of promises, then do Promise.all(), – Terry Lennox Feb 17 '21 at 14:51