I have a "When a new email arrives" trigger. When the body of the incoming email contains images, I need to replace the src="" attributes of the pictures in the body of the email with the ContentBytes base64 encoded data from Attachments (that is created automatically). I then send the Body of the email as a description in an incident management tool, so I need the HTML of the body to actually "contain" the pictures.
What would be the best way to approach this? I couldn't figure out how to use RegEx in Flow (apart from the paid Plumsail connectors), so I am trying to find a way to identify the whole <img ... /> tag based on the image name and then replace either the src="" attribute or the whole img tag.
Any help would be greatly appreciated. I am not new to Flow, but this is definitely one of the most challenging tasks I faced.
Hi @marianreha ,
Please refer to screenshot below to create the flow:
The expresssion in the compose as below:
replace(triggerBody()?['body'],first(split(last(split(triggerBody()?['body'],'src=')),'style=')),concat('"',items('Apply_to_each')?['contentBytes'],'" '))
The flow would run successfully as below:
Best regards,
Alice
Community Support Team _ Alice Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @v-alzhan-msft - thank you for your help!
Unfortunately this would only work if there would be only 1 picture in the email body. I need to figure out a way to make it work for multiple attachments (some of which are not pictures in body - for example word document).
I managed to make things work in Flow, but when I recreate it in Azure Logic Apps, it messes up the Body (like if the actions didn't wait until the previous one is fully finished, or I don't know..)
The way I constructed the flow: (SCREENSHOTS here: https://imgur.com/rZMi6hE)
1. create array variable containing data about attachments (name - identifier, base64 code of the attachment)
2. initialize 2 array variables for storing the body - "array" splits the body by "<img"
split(triggerbody()?['Body'],'<img ')
3. I then check if each attachment name is present in all rows of the original array - if YES, I replace the row (similarly as you suggested), if NOT, I keep the original row, everything stored in the "new-array" variable
Code in condition:
substring(items('Apply_to_each'),0,indexof(items('Apply_to_each'),'>'))
contains
substring(items('Apply_to_each_3'),0,indexof(items('Apply_to_each_3'),';'))
If yes:
replace(items('Apply_to_each'),substring(items('Apply_to_each'),0,indexof(items('Apply_to_each'),'>')),concat(' src="',substring(items('Apply_to_each_3'),add(indexof(items('Apply_to_each_3'),';'),1),sub(length(items('Apply_to_each_3')),add(indexof(items('Apply_to_each_3'),';'),1))),'" /'))
Then I store the "new-array" variable in the "array" and the "Apply to each 3" loops for another attachment.
At the end rows in the "array" are joined with "<img " to construct the HTML back.
Again, this way it works in MS Flow, but for some reason after recreating in Azure Logic Apps, it breaks the body of the email randomly.
Any idea why this happens would really help.