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.