Created
July 23, 2020 07:49
-
-
Save boicualexandru/67bd232e83b27aa367ca4a1d65295c58 to your computer and use it in GitHub Desktop.
Replace html image urls with the cid of the attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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