From 72dbf4cbc0d3dfd4f214ef15f6b05e71a0fefe30 Mon Sep 17 00:00:00 2001 From: Sander Hartman Date: Tue, 7 Jan 2020 14:23:09 +0100 Subject: [PATCH 1/4] Add branch for Magento 2.3.3 --- Mail/Template/TransportBuilder.php | 458 +++++++++++++++++++++++++++-- README.md | 17 +- composer.json | 8 +- etc/di.xml | 4 + etc/module.xml | 4 +- 5 files changed, 457 insertions(+), 34 deletions(-) create mode 100644 etc/di.xml diff --git a/Mail/Template/TransportBuilder.php b/Mail/Template/TransportBuilder.php index a303a5e..b3149bc 100644 --- a/Mail/Template/TransportBuilder.php +++ b/Mail/Template/TransportBuilder.php @@ -1,36 +1,458 @@ message->createAttachment($body, $mimeType, $disposition, $encoding, $filename); + parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory, $emailMessageInterfaceFactory, $mimeMessageInterfaceFactory, $mimePartInterfaceFactory, $addressConverter); + $this->templateFactory = $templateFactory; + $this->objectManager = $objectManager; + $this->_senderResolver = $senderResolver; + $this->mailTransportFactory = $mailTransportFactory; + $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager + ->get(EmailMessageInterfaceFactory::class); + $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager + ->get(MimeMessageInterfaceFactory::class); + $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager + ->get(MimePartInterfaceFactory::class); + $this->addressConverter = $addressConverter ?: $this->objectManager + ->get(AddressConverter::class); + $this->partFactory = $objectManager->get(PartFactory::class); + } + + /** + * Add cc address + * + * @param array|string $address + * @param string $name + * + * @return $this + */ + public function addCc($address, $name = '') + { + $this->addAddressByType('cc', $address, $name); + + return $this; + } + + /** + * Add to address + * + * @param array|string $address + * @param string $name + * + * @return $this + * @throws InvalidArgumentException + */ + public function addTo($address, $name = '') + { + $this->addAddressByType('to', $address, $name); + + return $this; + } + + /** + * Add bcc address + * + * @param array|string $address + * + * @return $this + * @throws InvalidArgumentException + */ + public function addBcc($address) + { + $this->addAddressByType('bcc', $address); + + return $this; + } + + /** + * Set Reply-To Header + * + * @param string $email + * @param string|null $name + * + * @return $this + * @throws InvalidArgumentException + */ + public function setReplyTo($email, $name = null) + { + $this->addAddressByType('replyTo', $email, $name); return $this; } -} \ No newline at end of file + /** + * Set mail from address + * + * @param string|array $from + * + * @return $this + * @throws InvalidArgumentException + * @see setFromByScope() + * + * @deprecated 102.0.1 This function sets the from address but does not provide + * a way of setting the correct from addresses based on the scope. + */ + public function setFrom($from) + { + return $this->setFromByScope($from); + } + + /** + * Set mail from address by scopeId + * + * @param string|array $from + * @param string|int $scopeId + * + * @return $this + * @throws InvalidArgumentException + * @throws MailException + * @since 102.0.1 + */ + public function setFromByScope($from, $scopeId = null) + { + $result = $this->_senderResolver->resolve($from, $scopeId); + $this->addAddressByType('from', $result['email'], $result['name']); + + return $this; + } + + /** + * Set template identifier + * + * @param string $templateIdentifier + * + * @return $this + */ + public function setTemplateIdentifier($templateIdentifier) + { + $this->templateIdentifier = $templateIdentifier; + + return $this; + } + + /** + * Set template model + * + * @param string $templateModel + * + * @return $this + */ + public function setTemplateModel($templateModel) + { + $this->templateModel = $templateModel; + return $this; + } + + /** + * Set template vars + * + * @param array $templateVars + * + * @return $this + */ + public function setTemplateVars($templateVars) + { + $this->templateVars = $templateVars; + + return $this; + } + + /** + * Set template options + * + * @param array $templateOptions + * @return $this + */ + public function setTemplateOptions($templateOptions) + { + $this->templateOptions = $templateOptions; + + return $this; + } + + /** + * Get mail transport + * + * @return TransportInterface + * @throws LocalizedException + */ + public function getTransport() + { + try { + $this->prepareMessage(); + $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); + } finally { + $this->reset(); + } + + return $mailTransport; + } + + /** + * Reset object state + * + * @return $this + */ + protected function reset() + { + $this->messageData = []; + $this->templateIdentifier = null; + $this->templateVars = null; + $this->templateOptions = null; + return $this; + } + + /** + * Get template + * + * @return TemplateInterface + */ + protected function getTemplate() + { + return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) + ->setVars($this->templateVars) + ->setOptions($this->templateOptions); + } + + /** + * Prepare message. + * + * @return $this + * @throws LocalizedException if template type is unknown + */ + 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]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; + $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( + ['parts' => $parts] + ); + + $this->messageData['subject'] = html_entity_decode( + (string)$template->getSubject(), + ENT_QUOTES + ); + $this->message = $this->emailMessageInterfaceFactory->create($this->messageData); + + return $this; + } + + /** + * Handles possible incoming types of email (string or array) + * + * @param string $addressType + * @param string|array $email + * @param string|null $name + * + * @return void + * @throws InvalidArgumentException + */ + private function addAddressByType(string $addressType, $email, ?string $name = null): void + { + if (is_string($email)) { + $this->messageData[$addressType][] = $this->addressConverter->convert($email, $name); + return; + } + $convertedAddressArray = $this->addressConverter->convertMany($email); + if (isset($this->messageData[$addressType])) { + $this->messageData[$addressType] = array_merge( + $this->messageData[$addressType], + $convertedAddressArray + ); + } + } + + /** + * @param string|null $content + * @param string|null $fileName + * @param string|null $fileType + * @return TransportBuilder + */ + public function addAttachment(?string $content, ?string $fileName, ?string $fileType) + { + $attachmentPart = $this->partFactory->create(); + $attachmentPart->setContent($content) + ->setType($fileType) + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; + + return $this; + } +} diff --git a/README.md b/README.md index bd19cdb..a204f06 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ class Index extends Action public function execute() { // Any buffer - $body = ''; + $content = ''; $transport = $this->transportBuilder->setTemplateIdentifier('example_identifier') ->setTemplateOptions([ @@ -52,12 +52,7 @@ class Index extends Action 'email' => 'info@example.com', ]) ->addTo('example@example.com') - ->addAttachment($body, - Zend_Mime::TYPE_OCTETSTREAM, - Zend_Mime::DISPOSITION_ATTACHMENT, - Zend_Mime::ENCODING_BASE64, - 'document.pdf' - ); + ->addAttachment($content, 'document.pdf', 'application/pdf'); $transport = $transport->getTransport(); $transport->sendMessage(); @@ -70,11 +65,9 @@ class Index extends Action ## Api `public function addAttachment( - $body, - $mimeType = Zend_Mime::TYPE_OCTETSTREAM, - $disposition = Zend_Mime::DISPOSITION_ATTACHMENT, - $encoding = Zend_Mime::ENCODING_BASE64, - $filename = null + $content, + $fileName = '', + $fileType = '' )` For reference also check [the code](Mail/Template/TransportBuilder.php) diff --git a/composer.json b/composer.json index 3a3cbfa..4dd6486 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,16 @@ "type": "magento2-module", "authors": [ { - "email": "dwayne@weprovide.com", - "name": "Dwayne Hanekamp" + "email": "sander@weprovide.com", + "name": "Sander Hartman" } ], "license": "MIT", "minimum-stability": "stable", + "require": { + "php": "~7.1.3|~7.2.0", + "magento/framework": "~102.0.3" + }, "autoload": { "psr-4": { "WeProvide\\MailAttachment\\": "" diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..92d41c8 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index 59c5687..0cd87be 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,4 @@ - - \ No newline at end of file + + From e535d9f472d0ad156958f9fe9e6c10dbd21dd525 Mon Sep 17 00:00:00 2001 From: Sander Hartman Date: Tue, 7 Jan 2020 16:38:20 +0100 Subject: [PATCH 2/4] Remove unneeded properties --- .DS_Store | Bin 0 -> 6148 bytes Mail/Template/TransportBuilder.php | 390 +++++++++++++++++++++++++++-- README.md | 17 +- composer.json | 8 +- etc/di.xml | 4 + etc/module.xml | 4 +- 6 files changed, 389 insertions(+), 34 deletions(-) create mode 100644 .DS_Store create mode 100644 etc/di.xml diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fb3fed3abda71085e5f5a631e060806ef6cb50e0 GIT binary patch literal 6148 zcmeH~v2MaZ42FNV8IZbU?05?g&^I_CI(Fg(D6k+!VyM{f(fRH3(!h)&^josOvoBVX zf5^oEY;!$*0V9AF-HEk_nHlo|KlsHN)9LRx9WIy2i?p{6cuF5J+s|!53P=GdAO)m= z6j+b~d5o_<7W7Pd6e%DDmZ5-u9}3-BldUs89Sku7kORwMT*oXy7B7%B**aOFSxyg@ ztrla5*Q1>*d0kDm&fX5o;luLI=2HyKdONHzp;-+mNC7D@Q{cVlqo4oZ^hfjmtVO95 zkOD8JfDPN-cEgv-v-R%vyuQz>uN$3=%NhRt1TgWV_>&&S{o)ItemplateFactory = $templateFactory; + $this->objectManager = $objectManager; + $this->_senderResolver = $senderResolver; + $this->mailTransportFactory = $mailTransportFactory; + $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager + ->get(EmailMessageInterfaceFactory::class); + $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager + ->get(MimeMessageInterfaceFactory::class); + $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager + ->get(MimePartInterfaceFactory::class); + $this->addressConverter = $addressConverter ?: $this->objectManager + ->get(AddressConverter::class); + $this->partFactory = $objectManager->get(PartFactory::class); + } + + /** + * Add cc address + * + * @param array|string $address + * @param string $name * * @return $this */ - public function addAttachment( - $body, - $mimeType = Zend_Mime::TYPE_OCTETSTREAM, - $disposition = Zend_Mime::DISPOSITION_ATTACHMENT, - $encoding = Zend_Mime::ENCODING_BASE64, - $filename = null - ) { - $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename); + public function addCc($address, $name = '') + { + $this->addAddressByType('cc', $address, $name); + + return $this; + } + + /** + * Add to address + * + * @param array|string $address + * @param string $name + * + * @return $this + * @throws InvalidArgumentException + */ + public function addTo($address, $name = '') + { + $this->addAddressByType('to', $address, $name); + + return $this; + } + + /** + * Add bcc address + * + * @param array|string $address + * + * @return $this + * @throws InvalidArgumentException + */ + public function addBcc($address) + { + $this->addAddressByType('bcc', $address); + + return $this; + } + + /** + * Set Reply-To Header + * + * @param string $email + * @param string|null $name + * + * @return $this + * @throws InvalidArgumentException + */ + public function setReplyTo($email, $name = null) + { + $this->addAddressByType('replyTo', $email, $name); + + return $this; + } + + /** + * Set mail from address + * + * @param string|array $from + * + * @return $this + * @throws InvalidArgumentException + * @see setFromByScope() + * + * @deprecated 102.0.1 This function sets the from address but does not provide + * a way of setting the correct from addresses based on the scope. + */ + public function setFrom($from) + { + return $this->setFromByScope($from); + } + + /** + * Set mail from address by scopeId + * + * @param string|array $from + * @param string|int $scopeId + * + * @return $this + * @throws InvalidArgumentException + * @throws MailException + * @since 102.0.1 + */ + public function setFromByScope($from, $scopeId = null) + { + $result = $this->_senderResolver->resolve($from, $scopeId); + $this->addAddressByType('from', $result['email'], $result['name']); return $this; } -} \ No newline at end of file + /** + * Set template identifier + * + * @param string $templateIdentifier + * + * @return $this + */ + public function setTemplateIdentifier($templateIdentifier) + { + $this->templateIdentifier = $templateIdentifier; + + return $this; + } + + /** + * Set template model + * + * @param string $templateModel + * + * @return $this + */ + public function setTemplateModel($templateModel) + { + $this->templateModel = $templateModel; + return $this; + } + + /** + * Set template vars + * + * @param array $templateVars + * + * @return $this + */ + public function setTemplateVars($templateVars) + { + $this->templateVars = $templateVars; + + return $this; + } + + /** + * Set template options + * + * @param array $templateOptions + * @return $this + */ + public function setTemplateOptions($templateOptions) + { + $this->templateOptions = $templateOptions; + + return $this; + } + + /** + * Get mail transport + * + * @return TransportInterface + * @throws LocalizedException + */ + public function getTransport() + { + try { + $this->prepareMessage(); + $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); + } finally { + $this->reset(); + } + + return $mailTransport; + } + + /** + * Reset object state + * + * @return $this + */ + protected function reset() + { + $this->messageData = []; + $this->templateIdentifier = null; + $this->templateVars = null; + $this->templateOptions = null; + return $this; + } + + /** + * Get template + * + * @return TemplateInterface + */ + protected function getTemplate() + { + return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) + ->setVars($this->templateVars) + ->setOptions($this->templateOptions); + } + + /** + * Prepare message. + * + * @return $this + * @throws LocalizedException if template type is unknown + */ + 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]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; + $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( + ['parts' => $parts] + ); + + $this->messageData['subject'] = html_entity_decode( + (string)$template->getSubject(), + ENT_QUOTES + ); + $this->message = $this->emailMessageInterfaceFactory->create($this->messageData); + + return $this; + } + + /** + * Handles possible incoming types of email (string or array) + * + * @param string $addressType + * @param string|array $email + * @param string|null $name + * + * @return void + * @throws InvalidArgumentException + */ + private function addAddressByType(string $addressType, $email, ?string $name = null): void + { + if (is_string($email)) { + $this->messageData[$addressType][] = $this->addressConverter->convert($email, $name); + return; + } + $convertedAddressArray = $this->addressConverter->convertMany($email); + if (isset($this->messageData[$addressType])) { + $this->messageData[$addressType] = array_merge( + $this->messageData[$addressType], + $convertedAddressArray + ); + } + } + + /** + * @param string|null $content + * @param string|null $fileName + * @param string|null $fileType + * @return TransportBuilder + */ + public function addAttachment(?string $content, ?string $fileName, ?string $fileType) + { + $attachmentPart = $this->partFactory->create(); + $attachmentPart->setContent($content) + ->setType($fileType) + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; + + return $this; + } +} diff --git a/README.md b/README.md index bd19cdb..a204f06 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ class Index extends Action public function execute() { // Any buffer - $body = ''; + $content = ''; $transport = $this->transportBuilder->setTemplateIdentifier('example_identifier') ->setTemplateOptions([ @@ -52,12 +52,7 @@ class Index extends Action 'email' => 'info@example.com', ]) ->addTo('example@example.com') - ->addAttachment($body, - Zend_Mime::TYPE_OCTETSTREAM, - Zend_Mime::DISPOSITION_ATTACHMENT, - Zend_Mime::ENCODING_BASE64, - 'document.pdf' - ); + ->addAttachment($content, 'document.pdf', 'application/pdf'); $transport = $transport->getTransport(); $transport->sendMessage(); @@ -70,11 +65,9 @@ class Index extends Action ## Api `public function addAttachment( - $body, - $mimeType = Zend_Mime::TYPE_OCTETSTREAM, - $disposition = Zend_Mime::DISPOSITION_ATTACHMENT, - $encoding = Zend_Mime::ENCODING_BASE64, - $filename = null + $content, + $fileName = '', + $fileType = '' )` For reference also check [the code](Mail/Template/TransportBuilder.php) diff --git a/composer.json b/composer.json index 3a3cbfa..4dd6486 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,16 @@ "type": "magento2-module", "authors": [ { - "email": "dwayne@weprovide.com", - "name": "Dwayne Hanekamp" + "email": "sander@weprovide.com", + "name": "Sander Hartman" } ], "license": "MIT", "minimum-stability": "stable", + "require": { + "php": "~7.1.3|~7.2.0", + "magento/framework": "~102.0.3" + }, "autoload": { "psr-4": { "WeProvide\\MailAttachment\\": "" diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..92d41c8 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index 59c5687..0cd87be 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,4 @@ - - \ No newline at end of file + + From 4f6185e6c5d55e48dbf810d41ba525938384f54c Mon Sep 17 00:00:00 2001 From: Sander Hartman Date: Wed, 8 Jan 2020 08:20:31 +0100 Subject: [PATCH 3/4] Remove unneeded properties --- Mail/Template/TransportBuilder.php | 333 ++++------------------------- 1 file changed, 44 insertions(+), 289 deletions(-) diff --git a/Mail/Template/TransportBuilder.php b/Mail/Template/TransportBuilder.php index 8e298db..0c5e11c 100644 --- a/Mail/Template/TransportBuilder.php +++ b/Mail/Template/TransportBuilder.php @@ -1,31 +1,25 @@ templateFactory = $templateFactory; - $this->objectManager = $objectManager; - $this->_senderResolver = $senderResolver; - $this->mailTransportFactory = $mailTransportFactory; - $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager + parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory, $emailMessageInterfaceFactory, $mimeMessageInterfaceFactory, $mimePartInterfaceFactory, $addressConverter); + $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $objectManager ->get(EmailMessageInterfaceFactory::class); - $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager + $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $objectManager ->get(MimeMessageInterfaceFactory::class); - $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager + $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $objectManager ->get(MimePartInterfaceFactory::class); - $this->addressConverter = $addressConverter ?: $this->objectManager + $this->addressConverter = $addressConverter ?: $objectManager ->get(AddressConverter::class); - } - - /** - * Add cc address - * - * @param array|string $address - * @param string $name - * - * @return $this - */ - public function addCc($address, $name = '') - { - $this->addAddressByType('cc', $address, $name); - - return $this; - } - - /** - * Add to address - * - * @param array|string $address - * @param string $name - * - * @return $this - * @throws InvalidArgumentException - */ - public function addTo($address, $name = '') - { - $this->addAddressByType('to', $address, $name); - - return $this; - } - - /** - * Add bcc address - * - * @param array|string $address - * - * @return $this - * @throws InvalidArgumentException - */ - public function addBcc($address) - { - $this->addAddressByType('bcc', $address); - - return $this; - } - - /** - * Set Reply-To Header - * - * @param string $email - * @param string|null $name - * - * @return $this - * @throws InvalidArgumentException - */ - public function setReplyTo($email, $name = null) - { - $this->addAddressByType('replyTo', $email, $name); - - return $this; - } - - /** - * Set mail from address - * - * @param string|array $from - * - * @return $this - * @throws InvalidArgumentException - * @see setFromByScope() - * - * @deprecated 102.0.1 This function sets the from address but does not provide - * a way of setting the correct from addresses based on the scope. - */ - public function setFrom($from) - { - return $this->setFromByScope($from); - } - - /** - * Set mail from address by scopeId - * - * @param string|array $from - * @param string|int $scopeId - * - * @return $this - * @throws InvalidArgumentException - * @throws MailException - * @since 102.0.1 - */ - public function setFromByScope($from, $scopeId = null) - { - $result = $this->_senderResolver->resolve($from, $scopeId); - $this->addAddressByType('from', $result['email'], $result['name']); - - return $this; - } - - /** - * Set template identifier - * - * @param string $templateIdentifier - * - * @return $this - */ - public function setTemplateIdentifier($templateIdentifier) - { - $this->templateIdentifier = $templateIdentifier; - - return $this; - } - - /** - * Set template model - * - * @param string $templateModel - * - * @return $this - */ - public function setTemplateModel($templateModel) - { - $this->templateModel = $templateModel; - return $this; - } - - /** - * Set template vars - * - * @param array $templateVars - * - * @return $this - */ - public function setTemplateVars($templateVars) - { - $this->templateVars = $templateVars; - - return $this; - } - - /** - * Set template options - * - * @param array $templateOptions - * @return $this - */ - public function setTemplateOptions($templateOptions) - { - $this->templateOptions = $templateOptions; - - return $this; - } - - /** - * Get mail transport - * - * @return TransportInterface - * @throws LocalizedException - */ - public function getTransport() - { - try { - $this->prepareMessage(); - $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); - } finally { - $this->reset(); - } - - return $mailTransport; + $this->partFactory = $objectManager->get(PartFactory::class); } /** @@ -354,21 +100,10 @@ protected function reset() $this->templateIdentifier = null; $this->templateVars = null; $this->templateOptions = null; + $this->attachments = []; return $this; } - /** - * Get template - * - * @return TemplateInterface - */ - protected function getTemplate() - { - return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) - ->setVars($this->templateVars) - ->setOptions($this->templateOptions); - } - /** * Prepare message. * @@ -394,8 +129,9 @@ protected function prepareMessage() ); } $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( - ['parts' => [$mimePart]] + ['parts' => $parts] ); $this->messageData['subject'] = html_entity_decode( @@ -431,4 +167,23 @@ private function addAddressByType(string $addressType, $email, ?string $name = n ); } } + + /** + * @param string|null $content + * @param string|null $fileName + * @param string|null $fileType + * @return TransportBuilder + */ + public function addAttachment(?string $content, ?string $fileName, ?string $fileType) + { + $attachmentPart = $this->partFactory->create(); + $attachmentPart->setContent($content) + ->setType($fileType) + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; + + return $this; + } } From 7bd628f83b77a35ae22dff9ae154b2479710ebb2 Mon Sep 17 00:00:00 2001 From: Sander Hartman Date: Wed, 8 Jan 2020 08:20:31 +0100 Subject: [PATCH 4/4] Remove unneeded properties --- Mail/Template/TransportBuilder.php | 112 +++++++++-------------------- 1 file changed, 34 insertions(+), 78 deletions(-) diff --git a/Mail/Template/TransportBuilder.php b/Mail/Template/TransportBuilder.php index 8e298db..6e66317 100644 --- a/Mail/Template/TransportBuilder.php +++ b/Mail/Template/TransportBuilder.php @@ -1,31 +1,29 @@ templateFactory = $templateFactory; $this->objectManager = $objectManager; $this->_senderResolver = $senderResolver; @@ -171,6 +106,7 @@ public function __construct( ->get(MimePartInterfaceFactory::class); $this->addressConverter = $addressConverter ?: $this->objectManager ->get(AddressConverter::class); + $this->partFactory = $objectManager->get(PartFactory::class); } /** @@ -394,8 +330,9 @@ protected function prepareMessage() ); } $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( - ['parts' => [$mimePart]] + ['parts' => $parts] ); $this->messageData['subject'] = html_entity_decode( @@ -431,4 +368,23 @@ private function addAddressByType(string $addressType, $email, ?string $name = n ); } } + + /** + * @param string|null $content + * @param string|null $fileName + * @param string|null $fileType + * @return TransportBuilder + */ + public function addAttachment(?string $content, ?string $fileName, ?string $fileType) + { + $attachmentPart = $this->partFactory->create(); + $attachmentPart->setContent($content) + ->setType($fileType) + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; + + return $this; + } }