ฉันเริ่มค้นหารอบ ๆ ในโค้ดเบสของ Magento 2 เพื่อหาสตริงเช่น 'อีเมล', 'ข้อความ' ฯลฯ จนกว่าฉันจะพบสิ่งที่ส่งอีเมล ฉัน stumbled เมื่อ sendPaymentFailedEmail () vendor/magento/module-checkout/Helper/Data.phpใน ชุดนี้มีตัวแปรจำนวนมาก แต่ในที่สุดก็ผูกกับวัตถุการขนส่งซึ่งถูกสร้างขึ้นผ่าน 'transportBuilder' transportBuilder \Magento\Framework\Mail\Template\TransportBuilderนี่คือตัวอย่างของ
ในแฟ้มที่เป็นตัวแปรที่มีอยู่ซึ่งเป็นตัวอย่างของ$transport \Magento\Framework\Mail\TransportInterfaceเนื่องจากมีอินเตอร์เฟซ, \Magento\Framework\Mail\Transportนอกจากนี้ยังมีระดับปกติที่เรียกว่า เมื่อเราเปิดไฟล์vendor/magento/framework/Mail/Transport.phpเราจะเห็นว่านี่เป็นการขยาย Zend_Mail_Transport_Sendmail
การขนส่งระดับขยาย \ Zend_Mail_Transport_Sendmail ใช้ \ Magento \ Framework \ Mail \ TransportInterface
นี่คือสิ่งที่คุณกำลังมองหา เมื่อใช้ DI คุณจะสามารถแทนที่การขนส่งนี้ด้วยเฟรมเวิร์กอีเมลอื่นแทน Zend_Mail เช่น Mandrill หรือ Amazon SES
เพียงให้แน่ใจว่าได้รวมsend()วิธีการตั้งแต่นั้นเป็นวิธีการที่เรียกว่าในsendMessage();
public function sendMessage()
{
try {
parent::send($this->_message);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e);
}
}
$email->setBodyHtml($body)