emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager ->get(EmailMessageInterfaceFactory::class); $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager ->get(MimeMessageInterfaceFactory::class); $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager ->get(MimePartInterfaceFactory::class); } /** * @param array $messageData */ public function setMessageData(string $key, array $emails): static { $data = []; foreach ($emails as $email) { if (is_string($email)) { $data[] = new \Magento\Framework\Mail\Address($email, $email); } } if (!empty($data)) $this->messageData[$key] = $data; return $this; } /** * 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: $partType = MimeInterface::TYPE_TEXT; break; case TemplateTypesInterface::TYPE_HTML: $partType = MimeInterface::TYPE_HTML; break; default: throw new LocalizedException( new Phrase('Unknown template type') ); } /** @var \Magento\Framework\Mail\MimePartInterface $mimePart */ $mimePart = $this->mimePartInterfaceFactory->create( [ 'content' => $content, 'type' => $partType ] ); $this->messageData['sender'] = $mimePart->getCharset(); $this->messageData['encoding'] = $mimePart->getCharset(); $parts = [$mimePart]; if ($this->attachmentMimePart!=null) { $parts[] = $this->attachmentMimePart; } $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( ['parts' => $parts] ); $this->messageData['subject'] = html_entity_decode( (string)$template->getSubject(), ENT_QUOTES ); $this->messageData['sender'] = new \Magento\Framework\Mail\Address('no-reply@stemcellmart.com.cn', 'STEMCELL在线商城'); $this->messageData['form'] = $this->messageData['sender']; $this->message = $this->emailMessageInterfaceFactory->create($this->messageData); $this->message->setFromAddress('no-reply@stemcellmart.com.cn', 'STEMCELL经销商下单系统'); return $this; } public function setAttachmentMimePart(array $attachment): static { $this->attachmentMimePart = $this->mimePartInterfaceFactory->create($attachment); return $this; } }