Last active
November 20, 2023 17:07
-
-
Save 0-Sony/a85dd83c49ce7a0d17cec7ad9b663c8d to your computer and use it in GitHub Desktop.
Magento 2 : Custom Send Email - (Tested on Magento 2.2.6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@menincode.com> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace Namespace\MyModule\Model\SendMail; | |
use Magento\Framework\App\Area; | |
use Magento\Framework\Mail\Template\TransportBuilder; | |
class CustomMail implements CustomMailInterface | |
{ | |
/** | |
* CustomMail constructor. | |
* @param TransportBuilder $transportBuilder | |
*/ | |
public function __construct( | |
TransportBuilder $transportBuilder | |
){ | |
$this->transportBuilder = $transportBuilder; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function send(TemplateVars $templateVars) | |
{ | |
$this->prepareMail($templateVars); | |
$this->transportBuilder->getTransport()->sendMessage(); | |
} | |
/** | |
* @param TemplateVars $templateVars | |
*/ | |
protected function prepareMail(TemplateVars $templateVars) | |
{ | |
$vars = $templateVars->toArray(); | |
$store = $vars['store']; | |
$templateId = $vars['template']; | |
$this->transportBuilder->setTemplateIdentifier($templateId); | |
$this->transportBuilder->setTemplateOptions( | |
['area' => Area::AREA_FRONTEND ,'store' => $store->getId()] | |
); | |
$this->transportBuilder->setTemplateVars($vars); | |
$sender = "sender@myshop.com" | |
$this->transportBuilder->setFrom(['email' => $sender, 'name' => 'Sales']); | |
$address =['customer@somewhere.com','customer2@somewhere.com']; | |
$this->transportBuilder->addTo( | |
$address, | |
$store->getWebsite()->getName() | |
); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@menincode.com> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace Namespace\MyModule\Model\SendMail; | |
use Namespace\MyModule\Model\SendMail\TemplateVars; | |
interface CustomMailInterface | |
{ | |
/** | |
* @param TemplateVars $templateVars | |
* @return void | |
*/ | |
public function send(TemplateVars $templateVars); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="Namespace\MyModule\Model\SendMail\CustomMailInterface" | |
type="Namespace\MyModule\Model\SendMail\CustomMail" /> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--@subject My Custom Email Subject @--> | |
{{template config_path="design/email/header_template"}} | |
<table> | |
<tr> | |
<td> | |
<h1>{{var store.getFrontendName()}}</h1> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
{{var message}} | |
</td> | |
</tr> | |
<tr> | |
<td> | |
Hello World , I can use my custom variable like that : {{var custom_var}} | |
</td> | |
</tr> | |
</table> | |
{{template config_path="design/email/footer_template"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> | |
<template id="mycustom_email_template" label="Custom Email Template" | |
file="email-template.html" type="html" module="Namespace_MyModule" area="frontend"/> | |
</config> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@menincode.com> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace Namespace\MyModule\Helper\Mail; | |
use Namespace\MyModule\Model\SendMail\CustomMailInterface; | |
use Namespace\MyModule\Model\SendMail\TemplateVarsFactory; | |
class SendMailHelper | |
{ | |
/** | |
* SendMailHelper constructor. | |
* @param CustomMailInterface $customMail | |
* @param TemplateVarsFactory $templateVarsFactory | |
*/ | |
public function __construct( | |
CustomMailInterface $customMail, | |
TemplateVarsFactory $templateVarsFactory | |
) { | |
$this->customMail = $adminMail; | |
$this->templateVarsFactory = $templateVarsFactory; | |
} | |
/** | |
* @param array $data | |
*/ | |
public function sendMail(array $data) | |
{ | |
$this->customMail->send($this->templateVarsFactory->create(['data' => $data])); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@menincode.com> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace Namespace\MyModule\Model\SendMail; | |
class TemplateVars | |
{ | |
/** | |
* @var array | |
*/ | |
protected $data; | |
/** | |
* CustomMailVars constructor. | |
* @param array $data | |
*/ | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
return $this->data; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@menincode.com> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace Namespace\MyModule\SomeWehere | |
use Namespace\MyModule\Helper\Mail\SendMailHelper; | |
use Magento\Store\Api\StoreRepositoryInterface; | |
class YourCustomClass | |
{ | |
protected $customMail; | |
/** | |
* YourCustomClass constructor. | |
* @param CustomMailInterface $customMail | |
* @param StoreRepositoryInterface $storeRepository | |
*/ | |
public function __construct( | |
SendMailHelper $sendMailHelper | |
StoreRepositoryInterface $storeRepository | |
) | |
{ | |
$this->sendMailHelper = $sendMailHelper; | |
$this->storeRepository = $storeRepository; | |
} | |
public function sendMail() | |
{ | |
$store_id = '2'; | |
$store = $this->storeRepository->getById($store_id); | |
$data = [ | |
'template' => 'mycustom_email_template'), | |
'store' => $store, | |
'custom_var' => 'put what you want', | |
'message' => __("My custom email message) | |
]; | |
$this->sendMailHelper->send($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment