Here we learn how to send mail using our smtp detail in magento2
For this we need to override mageto \Magento\Framework\Mail\Transport class by your custom model class
We have a complete module for smtp setup. You can check here SMTP Configurator for Magento2 here.
We can do it in two step
1# For override we need to write code in module di.xml file at location app/code/NameSpace/ModuleName/etc
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <!-- for override magento default Transport class with our custom module model--> <preference for="\Magento\Framework\Mail\Transport" type="NameSpace\ModuleName\Model\Transport"/> </config>
2# Now we’ll define our Transport model in our custom module
<?php /** * Mail Transport */ namespace NameSpace\ModuleName\Model; class Transport extends \Laminas\Mail\Transport\Sendmail implements \Magento\Framework\Mail\TransportInterface { /** * @var \Magento\Framework\Mail\MessageInterface */ protected $_message; /** * @param MessageInterface $message * @param null $parameters * @throws \InvalidArgumentException */ public function __construct(\Magento\Framework\Mail\MessageInterface $message) { if (!$message instanceof \Laminas\Mail) { throw new \InvalidArgumentException('The message should be an instance of \Laminas\Mail'); } $smtpHost= 'xxx.xxxx.xxx';//your smtp host '; $smtpConf = [ 'auth' => 'login',//auth type 'ssl' => 'tsl', 'port' => '587', 'username' => '[email protected]',//smtm user name 'password' => 'xxxxxxxxxxxxxx'//smtppassword ]; parent::__construct($smtpHost, $smtpConf); $this->_message = $message; } /** * Send a mail using this transport * @return void * @throws \Magento\Framework\Exception\MailException */ public function sendMessage() { try { parent::send($this->_message); } catch (\Exception $e) { throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e); } } }
Now all mail goes from your Magento 2 instance using your SMTP server 🙂
37 comments
If you are facing issue with the module in working with Magento version 2.3.5. Then, you can click here to create a ticket and the team will assist you accordingly.
Thank you !!
For magento2 version 2.2.Xyou need to do following updateSetp 1add following method in NameSpaceModuleNameModelTransport.php (Model file)/** * @inheritdoc */public function getMessage() { return $this->message;}Step 2=>update file app/code/NameSpace/ModuleName/etc/di.xml
Thanks Abhishek
tell me your magento updated version. is it 2.2.X?
Thanks Abhishek
you need to do following update
Setp 1
add following method in NameSpaceModuleNameModelTransport.php (Model file)
/** * @inheritdoc */
public function getMessage() {
return $this->message;
}
Step 2=>
update file app/code/NameSpace/ModuleName/etc/di.xml
Thanks Abhishek
Thanks Abhishek
Thanks Abhishek
Permissions are 777 so they have to be correct
Thanks Abhishek
main.ERROR: Source class “JilcoSMTP_Magento2ModelTransport” for “JilcoSMTP_Magento2ModelTransportInterceptor” generation does not exist.
Thanks Abhishek
Ik deleted that folder complety and stil the same error. Of course i did a static conten deploy and di
Thanks Abhishek
Magento 2.2.1?
$smtpConf = [
‘auth’ => ‘login’,//auth type
‘tsl’ => ‘tsl’,
‘port’ => ‘587’,
‘username’ => ‘[email protected]’,//smtm user name
‘password’ => ‘xxxxxxxxxxxxxx’//smtppassword
];
Mistake in the DI.xml…..
Manny thanks!
$smtpConf = [
‘auth’ => ‘login’,//auth type
‘ssl’ => ‘tls’,
‘port’ => ‘587’,
‘username’ => ‘[email protected]’,//smtm user name
‘password’ => ‘xxxxxxxxxxxxxx’//smtppassword
];
Just a small correction: on line 27 of the
Transport
class I think it should be ‘tls’ not ‘tsl’.No Logs available @ M2 EE 2.0.5
I am using AWS SES servics.