Skip to content

TransportBuilder unable to send emails of Content-Type "text/plain" as of 2.3.3 #26471

Closed
@twoonesixdigital

Description

@twoonesixdigital

Preconditions (*)

  1. Magento Community Edition 2.3.3
  2. One or more Magento email templates configured as type "text" via an email_templates.xml file.

Steps to reproduce (*)

1.Attempt to send a template based email of type "text" using Magento\Framework\Mail\Template\TransportBuilder that is of Content-Type "text/plain" using the TransportBuilder class's sendMessage method.

Expected result (*)

  1. An email is dispatched whose "Content-Type" header is set to "text/plain"

Actual result (*)

  1. The email is sent with a "Content-Type" header set to "text/html"

Additional Information (*)

It appears that as of Magento 2.3.3, it is no longer possible to send "text/plain" emails using the TransportBuilder. This is because, as of 2.3.3, the TransportBuilder's prepareMessage method does not pass the determined template type to the mimePartInterfaceFactory's create method.

Current method definition as of 2.3.3:

protected function prepareMessage()
    {
        $template = $this->getTemplate();
        $content = $template->processTemplate();
        switch ($template->getType()) {
            case TemplateTypesInterface::TYPE_TEXT:
                $part['type'] = MimeInterface::TYPE_TEXT;
                break;

            case TemplateTypesInterface::TYPE_HTML:
                $part['type'] = MimeInterface::TYPE_HTML;
                break;

            default:
                throw new LocalizedException(
                    new Phrase('Unknown template type')
                );
        }
        $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]);
        $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(
            ['parts' => [$mimePart]]
        );

        $this->messageData['subject'] = html_entity_decode(
            (string)$template->getSubject(),
            ENT_QUOTES
        );
        $this->message = $this->emailMessageInterfaceFactory->create($this->messageData);

        return $this;
    }

Proposed change that would resolve issue (only the relevant snippet):

$mimePart = $this->mimePartInterfaceFactory->create(
        	[
        		'content' => $content,
        		'type' => $part['type']
        	]
        );

(Pull request incoming)

Metadata

Metadata

Labels

Issue: Format is validGate 1 Passed. Automatic verification of issue format passedReported on 2.3.3Indicates original Magento version for the Issue report.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions