Skip to content

Instantly share code, notes, and snippets.

@steffen-wirth
Forked from 0-Sony/CustomMail.php
Created May 14, 2021 14:21
Show Gist options
  • Save steffen-wirth/18ab8f5d071c548f10d4a629db2ed4a5 to your computer and use it in GitHub Desktop.
Save steffen-wirth/18ab8f5d071c548f10d4a629db2ed4a5 to your computer and use it in GitHub Desktop.
Magento 2 : Custom Send Email - (Tested on Magento 2.2.6)
<?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 implement 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()
);
}
}
<?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);
}
<?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>
<!--@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"}}
<?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>
<?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]));
}
}
<?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;
}
}
<?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