Skip to content
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

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from

Conversation

masoodkhoshgard
Copy link
Contributor

@masoodkhoshgard masoodkhoshgard commented May 26, 2024

Description

#19930

Gmail will not render base64 images embedded inside HTML emails but with Google Apps Script
on Gmail:
Screenshot 2024-05-26 125259

on personal mail server:
Screenshot 2024-05-26 125348

Checklist

  • I fully tested it as developer / designer and created unit / integration tests

Copy link

codecov bot commented May 26, 2024

Codecov Report

Attention: Patch coverage is 5.47945% with 69 lines in your changes missing coverage. Please review.

Project coverage is 52.09%. Comparing base (5857f65) to head (58331fc).
Report is 158 commits behind head on dev.

Current head 58331fc differs from pull request most recent head 089cc52

Please upload reports for the commit 089cc52 to get more accurate results.

Files Patch % Lines
....Abp.Emailing/Volo/Abp/Emailing/EmailSenderBase.cs 5.47% 69 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@maliming
Copy link
Member

Thank you for your code.

The NormalizeMailAsync is designed to override and meet your cases.

We don't need to add the Gmail-relevant code to the framework.

@masoodkhoshgard
Copy link
Contributor Author

Thank you for your code.

The NormalizeMailAsync is designed to override and meet your cases.

We don't need to add the Gmail-relevant code to the framework.

Thanks @maliming
But keep in mind that Gmail is a public service and this problem happens a lot and that it is likely to occur for other mail services as well.

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,([^""]+)""[^>]*>");
Copy link
Member

@ebicoglu ebicoglu Jun 5, 2024

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,([^""]+)."|'

image

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
}

Copy link
Contributor Author

@masoodkhoshgard masoodkhoshgard Jun 5, 2024

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);
Copy link
Member

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?

Copy link
Contributor Author

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.

@ebicoglu ebicoglu requested a review from maliming June 5, 2024 11:15
@maliming maliming added this to the 8.3-preview milestone Jul 4, 2024
@maliming
Copy link
Member

maliming commented Jul 4, 2024

I have formatted the code. The code differences are now simpler.

@ebicoglu You can review it again. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants