-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Email Attachments in Magento 2.4.8 #39869
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
Comments
Hi @fmaniconereply. Thank you for your report.
Join Magento Community Engineering Slack and ask your questions in #github channel. |
A really messy solution is to do this to add an attachment:
|
Hi @engcom-November. Thank you for working on this issue.
|
I would note that the MimePartInterface / MimeMessageInterface classes used to handle this properly but it no longer handles multi-part MimeParts. In if ($type === MimeInterface::TYPE_HTML) {
$this->mimePart = new TextPart($content, $charset, 'html', $encoding);
} elseif ($type === MimeInterface::TYPE_TEXT) {
$this->mimePart = new TextPart($content, $charset, 'plain', $encoding);
} else {
$this->mimePart = new DataPart($content, $fileName, $type, $encoding);
} However, when this is converted to an actual Symfony message, only the first TextPart is used and the rest are discarded. In public function __construct(array $parts)
{
$headers = null;
$body = null;
foreach ($parts as $part) {
$mimePart = $part->getMimePart();
if ($mimePart instanceof TextPart) {
$headers = $mimePart->getHeaders();
$body = $mimePart;
break;
}
}
$this->mimeMessage = new Message($headers, $body);
} As you can see, any TextPart types are inserted into the body and the remaining parts are discarded. Technically this does include DataPart attachments but it'll only process the first part. To properly merge all of the parts we need to do something a bit more like the Symfony Mailer class: public function __construct(array $parts)
{
$dataParts = [];
$headers = new Headers();
$html = null;
$text = null;
/** @var MimePartInterface $part */
foreach ($parts as $part) {
$headers = new Headers(...$headers->all(), ...$mimePart->getHeaders()->all());
$mimePart = $part->getMimePart();
if ($mimePart instanceof TextPart) {
if ($mimePart->getMediaSubtype() == 'html') {
$html = $mimePart;
} elseif ($mimePart->getMediaSubtype() == 'plain') {
$text = $mimePart;
} else {
$dataParts[] = $mimePart;
}
}
}
if ($html && $text) {
$body = new AlternativePart($text, $html);
} else if ($html) {
$body = $html;
} else if ($text) {
$body = $text;
}
if ($dataParts) {
$body = new MixedPart($body, ...$dataParts);
}
$this->mimeMessage = new Message($headers, $body);
} |
I'm actually not sure that we need to include the Symfony Part headers in the Message $headers... unless we need to add additional headers to the email I'm fairly sure the Part headers will be included automatically. Just passing in an empty Headers() may be fine. |
A slightly less hacky way is to simply make use of the Symfony MixedPart: $symfonyMessage = $message->getSymfonyMessage();
$attachment = new DataPart($attachmentContent, 'invoice_' . $order->getIncrementId() . '.pdf', 'application/octet-stream', 'base64');
$symfonyMessage->setBody(new MixedPart($symfonyMessage->getBody(), $attachment)); |
Some of the other implementations in public function isMultiPart(): bool
{
$body = $this->mimeMessage->getBody();
return $body instanceof AlternativePart && $body->countParts() > 1;
} This probably needs to check for Same for the |
Hi @engcom-Hotel. Thank you for working on this issue.
|
Hello @fmaniconereply, Thanks for the report and collaboration! Yes its true there is no straight way for attaching file in the mails in Magento core. Hence confirming this issue. Thanks |
✅ Jira issue https://jira.corp.adobe.com/browse/AC-14581 is successfully created for this GitHub issue. |
✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue. |
We did have a PR at one time that introduced this email attachment feature request, but for some lame reason it got rejected by internal Adobe devs. Maybe we can try again if they are willing, maybe @engcom-Hotel can first double check internally if Adobe devs are open for this feature this time? |
@hostep Not sure if this had anything to do with the switch to Symfony (which was most definitely BIC) but the above MimeMessage changes would be necessary for that PR to function as well. |
Preconditions and environment
Could you explain the rationale behind having the EmailMessage class extend the Message class, rather than a class directly related to email sending, such as Mail? Additionally, since the Message class does not provide methods like attach to handle attachments, how does Magento handle email attachments in this architecture?
Steps to reproduce
Create a script for send email
use method "attach" to add attachment
Expected result
It is not possible to add an attachment.
Actual result
It is not possible to add an attachment.
Additional information
No response
Release note
No response
Triage and priority
The text was updated successfully, but these errors were encountered: