Skip to content

Instantly share code, notes, and snippets.

@boicualexandru
Created July 23, 2020 07:49
Show Gist options
  • Save boicualexandru/67bd232e83b27aa367ca4a1d65295c58 to your computer and use it in GitHub Desktop.
Save boicualexandru/67bd232e83b27aa367ca4a1d65295c58 to your computer and use it in GitHub Desktop.
Replace html image urls with the cid of the attachments
var templateDecoded = Encoding.UTF8.GetString(Convert.FromBase64String(campaign.Template));
templateDecoded = ReplaceHtmlImagesWithCIds(templateDecoded);
private string[] GetImageNamesFromHtml(string html)
{
string[] htmlImgSrcNames = Regex.Matches(html, @"<img.+?src=[\""'](.*?[\\/])?(.+?)[\""'].*?>", RegexOptions.IgnoreCase).Select(x => x.Groups[2].Value).ToArray();
string[] htmlImgUrlNames = Regex.Matches(html, @"url\([\""']?(.*?[\\/])(.+?)[\""']?\)", RegexOptions.IgnoreCase).Select(x => x.Groups[1].Value).ToArray();
string[] htmlImageNames = htmlImgSrcNames.Concat(htmlImgUrlNames).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray();
return htmlImageNames;
}
private string ReplaceHtmlImagesWithCIds(string html)
{
string[] htmlImageNames = GetImageNamesFromHtml(html);
string result = html;
foreach (var imageName in htmlImageNames)
{
result = result.Replace(imageName, $"cid:{imageName}");
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment