-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send Base64 Images in Email with Google Apps #19932
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #19932 +/- ##
=======================================
Coverage 52.09% 52.09%
=======================================
Files 3107 3105 -2
Lines 99366 99310 -56
Branches 7514 7515 +1
=======================================
- Hits 51763 51738 -25
+ Misses 46044 46016 -28
+ Partials 1559 1556 -3 ☔ View full report in Codecov by Sentry. |
Thank you for your code. The We don't need to add the Gmail-relevant code to the framework. |
Thanks @maliming |
var htmlBody = mail.Body; | ||
|
||
// Find all base64 image tags in the html message. | ||
var base64ImageTags = Regex.Matches(htmlBody, @"<img src=""data:image/(png|jpeg|gif);base64,([^""]+)""[^>]*>"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex you provided doesn't cover all variants of a base64 image element.
Here's a better regex that finds all possible src
with the base64 format:
src\s*=\s*("|')\s*data\s*:\s*image/\s*(png|jpeg|gif);\s*base64,([^""]+)."|'
syntax:
bool foundMatch = false;
try {
foundMatch = Regex.IsMatch("input-text-here...", @"src\s*=\s*(""|')\s*data\s*:\s*image/\s*(png|jpeg|gif);\s*base64,([^""""]+).""|'", RegexOptions.Singleline);
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ebicoglu
I think it would be better to use the following regex pattern:
src\s*=\s*(""|')\s*data\s*:\s*(?<mediaType>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*?)(""|')
Because it covers all tags that use base64, such as fonts, video, audio and more, not just limited to images.
img.ContentType.MediaType = $"image/{format}"; | ||
img.TransferEncoding = TransferEncoding.Base64; | ||
img.ContentType.Name = img.ContentId; | ||
img.ContentLink = new Uri("cid:" + img.ContentId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is cid
a universal HTML format? or only Gmail understand this tag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, "cid" is not a universal HTML format. CID stands for "Content-ID" and is used specifically in the context of email and HTML email messages.
In email, the "cid" is used to reference embedded content, such as images or other media, that is included within the email message itself. The CID acts as a unique identifier for that embedded content.
For example, in an HTML email, an image might be referenced using a CID like this:
<img src="cid:image001.jpg">
This tells the email client to look for an image with the content ID "image001.jpg" that is embedded within the email message.
However, this "cid" reference is not a standard HTML format that can be used outside of the email context. In regular web pages, images and other resources are typically referenced using standard URL paths, not CIDs.
So in summary, "cid" is specific to email and not a universal HTML format for referencing content on the web. Regular HTML uses other methods like [src="image.jpg"] to link to external resources.
I have formatted the code. The code differences are now simpler. @ebicoglu You can review it again. Thanks. |
We have decided not to add the Thanks for your contribution. |
Description
#19930
Gmail will not render base64 images embedded inside HTML emails but with Google Apps Script
on Gmail:
on personal mail server:
Checklist