inlineTranslation->suspend(); try { $storeId = $this->storeManager->getStore()->getId(); // 邮件变量 $templateVars = [ 'emailTitle' => $recipientName, 'recordHtml' => '
邮件内容
' ]; $templateId = self::FORM_RECORDS_NOTIFICATION_TEMPLATE; // 发件人信息(定义在 Stores > Configuration > General > Store Email Addresses) $from = [ 'email' => $this->scopeConfig->getValue('trans_email/ident_general/email'), 'name' => 'Stemcell' ]; // 创建 Transport $transport = $this->transportBuilder ->setTemplateIdentifier($templateId) ->setTemplateOptions([ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId ]) ->setTemplateVars($templateVars) ->setFromByScope($from) ->addTo($recipientEmail) ->getTransport(); $mail = $transport->getMessage(); $body = new Message(); foreach ($excels as $excel) { if (empty($excel['content'])) { echo $excel['title'].PHP_EOL; } $attachment = new Part($excel['content']); $attachment->type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->filename = mb_convert_encoding($excel['title'], 'GB2312'); // $body->addPart(new Part(' ')); $body->addPart($attachment); } $mail->setBody($body); $transport->sendMessage(); $this->inlineTranslation->resume(); return true; } catch (MailException $e) { $this->inlineTranslation->resume(); throw new \Exception(__('邮件发送失败: %1', $e->getMessage())); } } }