Magento如何将附件添加到邮件中

redmaomail 2024-07-24 18:31 阅读数 106 #Magento

红帽云邮外贸主机

在 Magento 中,增强沟通策略可以显著提高客户满意度和参与度。其中一种有效的方法就是在电子邮件中添加附件,为客户提供详细的信息,并改善整体用户体验。 

在这篇博文中,我们将指导您完成创建 Magento 模块的过程,以将发票自动附加到您的电子邮件中,从而增强整体用户体验。

为什么向Magento 电子邮件添加附件是一项有价值的增强功能

事实证明,将文件附加到 Magento 电子邮件对企业和客户都有利。

与企业

提高效率并节省时间

通过预先自动准备发票附件,企业可以节省时间和精力。发件人可以轻松点击“发送”,简化沟通过程。

专业的品牌形象

将精心设计的附件包含在内有助于公司树立专业形象。这反映了对组织有序和详细沟通的承诺,提升了业务的整体感知。

与客户

全面的订单详情

附件提供给客户详细的订单信息,例如发票、收据和运输详情,直接在电子邮件中。这消除了客户需要在外部平台导航的必要,确保他们全面了解自己的购买。

简化沟通

附件采用了易于理解的格式,减少了客户阅读冗长电子邮件的必要性。重要信息被清晰、简洁地呈现,从而提升了整体用户体验。

在Magento电子邮件中添加发票附件的步骤说明

1. 创建模块文件

首先,您需要使用以下文件设置模块的基本结构:

文件 di.xml

<?xml version="1.0"?>
 
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Framework\Mail\Template\TransportBuilder"
           	type="Alwayly\SendPdf\Mail\Template\TransportBuilder"/>
   <type name="Magento\Sales\Model\Order\Email\SenderBuilder">
   	<plugin name="add.attachment.email" type="Alwayly\SendPdf\Plugin\SenderBuilder"/>
   </type>
</config>

文件 module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="Alwayly_SendPdf" />
</config>

文件 registration.php

<?php
/*
* @author  Alwayly Solutions <info@Alwayly.com>
* @copyright Copyright (c) 2023 Alwayly Solutions <https://www.alwayly.com>. All rights reserved.
* @license  Open Software License (“OSL”) v. 3.0
*/
 
use Magento\Framework\Component\ComponentRegistrar;
 
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Alwayly_SendPdf', __DIR__);

2. 重写TransportBuilder 

然后,我们需要覆盖Magento 中Alwayly\SendPdf\Mail\Template目录中的TransportBuilder文件以包含附件功能。

<?php
 
declare (strict_types=1);
 
namespace Alwayly\SendPdf\Mail\Template;
 
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Mail\AddressConverter;
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\MimeMessageInterfaceFactory;
use Magento\Framework\Mail\MimePartInterfaceFactory;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;
use Laminas\Mime\Mime;
use Laminas\Mime\Message;
use Laminas\Mime\PartFactory;
 
/**
* Class TransportBuilder
* @package Alwayly\SendPdf\Mail\Template
*/
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
   /**
	* @var Message
	*/
   protected $messageMime;
 
   /**
	* @var
	*/
   protected $message;
 
   /**
	* @var array
	*/
   protected $attachments = [];
 
   /**
	* @var PartFactory|mixed
	*/
   protected $partFactory;
 
   /**
	* @param Message $messageMime
	* @param PartFactory $partFactory
	* @param FactoryInterface $templateFactory
	* @param MessageInterface $message
	* @param SenderResolverInterface $senderResolver
	* @param ObjectManagerInterface $objectManager
	* @param TransportInterfaceFactory $mailTransportFactory
	* @param MessageInterfaceFactory|null $messageFactory
	* @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
	* @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
	* @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
	* @param AddressConverter|null $addressConverter
	*/
   public function __construct(
   	Message $messageMime,
   	PartFactory $partFactory,
   	FactoryInterface $templateFactory,
   	MessageInterface $message,
   	SenderResolverInterface $senderResolver,
   	ObjectManagerInterface $objectManager,
   	TransportInterfaceFactory $mailTransportFactory,
   	MessageInterfaceFactory $messageFactory = null,
   	EmailMessageInterfaceFactory $emailMessageInterfaceFactory = null,
   	MimeMessageInterfaceFactory $mimeMessageInterfaceFactory = null,
   	MimePartInterfaceFactory $mimePartInterfaceFactory = null,
   	AddressConverter $addressConverter = null
   ) {
   	$this->templateFactory = $templateFactory;
   	$this->partFactory = $partFactory;
   	$this->messageMime = $messageMime;
   	parent::__construct(
       	$templateFactory,
       	$message,
       	$senderResolver,
       	$objectManager,
       	$mailTransportFactory,
       	$messageFactory,
       	$emailMessageInterfaceFactory,
       	$mimeMessageInterfaceFactory,
       	$mimePartInterfaceFactory,
       	$addressConverter
   	);
   }
 
   /**
	* @return $this|TransportBuilder
	* @throws LocalizedException
	*/
 protected function prepareMessage()
   {
   	$result = parent::prepareMessage();
   	if (!empty($this->attachments)) {
       	foreach ($this->attachments as $attachment) {
           	$body = $this->message->getBody();
           	if (!$body) {
               	$body = $this->messageMime;
           	}
           	$body->addPart($attachment);
           	$this->message->setBody($body);
       	}
       	$this->attachments = [];
   	}
   	return $result;
   }
 
   /**
	* @param $content
	* @param $fileName
	* @param $fileType
	* @return $this
	*/
   public function addAttachment($content, $fileName, $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;
   }
}

3. SenderBuilder插件

<?php
 
namespace Alwayly\SendPdf\Plugin;
 
use Laminas\Validator\Date;
use Alwayly\SendPdf\Mail\Template\TransportBuilder;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Pdf\Invoice;
use Magento\Framework\Stdlib\DateTime\DateTime;
 
class SenderBuilder
{
   private TransportBuilder $transportBuilder;
 
   private Template $templateContainer;
 
   private Invoice $renderInvoice;
 
   private DateTime $dateTime;
 
   public function __construct(
   	DateTime $dateTime,
   	Invoice $renderInvoice,
   	Template $templateContainer,
   	TransportBuilder $transportBuilder
   ) {
   	$this->transportBuilder = $transportBuilder;
   	$this->templateContainer = $templateContainer;
   	$this->renderInvoice = $renderInvoice;
   	$this->dateTime = $dateTime;
   }
 
   public function beforeSend(\Magento\Sales\Model\Order\Email\SenderBuilder $subject)
   {
   	$dataInvoice = $this->_getDataTemplate();
   	try {
       	if(!empty($dataInvoice)){
           	$pdfContent = $this->renderInvoice->getPdf($dataInvoice)->render();
           	$date = $this->dateTime->date('Y-m-d_H-i-s');
           	$this->transportBuilder->addAttachment($pdfContent, 'invoice' . $date . '.pdf', 'application/pdf');
       	}
   	} catch (\Exception $e) {
       	return;
   	}
   }
 
   private function _getDataTemplate()
   {
   	$data = $this->templateContainer->getTemplateVars();
   	if (array_key_exists('invoice_id', $data)) {
       	return [$data['invoice']];
   	}
   	if (isset($data['order']) && $data['order']->hasInvoices()) {
       	return $data['order']->getInvoiceCollection()->getItems();
   	}
   	return [$data['invoice']] ?? '';
   }

结果

当发出发票或订单的电子邮件时,会自动地在邮件中添加发票附件。

Magento 邮件附件

总结

总之,按照这些步骤,您可以轻松地将发票附件添加到Magento的电子邮件中。这样做可以增强您的沟通策略,为客户提供详细信息,从而促进积极的购物体验。

根据您的具体需求对该模块进行定制,从而在竞争激烈的电子商务市场中提高客户参与度。如果需要,欢迎联系我们,我们为您提供更多的Magento平台定制服务。


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机