I created a custom email and it's working fine like this:
$data['amount'] = round($amount);
$data['customer_name'] = $customer->getFirstname() . ' ' . $customer->getLastname();
$data['customer_telephone'] = $customer->getMainTelephone();
$data['customer_email'] = $customer->getEmail();
$data['customer_credit_balance'] = round($creditBalance);
$data['requested_at'] = $model->getRequestedAt();
$data['customer_bank_account'] = $description;
$storeName = $this->getStoreName();
$storeEmail = $this->getStoreEmail();
$senderInfo = [
'name' => $storeName,
'email' => $storeEmail
];
$receiverInfo = [
'name' => $data['customer_name'],
'email' => $data['customer_email']
];
$invoiceId = 5;
$this->emailHelper->sendMail(
$data,
$senderInfo,
$receiverInfo,
'approve',
$invoiceId
);
email helper:
namespace Test\Xxxx\Helper;
class Email extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_EMAIL_TEMPLATE_FIELD_REQUEST = 'test/xxxx/request_mail';
const XML_PATH_EMAIL_TEMPLATE_FIELD_APPROVE = 'test/xxxx/approve_mail';
const XML_PATH_EMAIL_TEMPLATE_FIELD_REJECT = 'test/xxxx/reject_mail';
/* Here section and group refer to name of section and group where you create this field in configuration*/
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
protected $inlineTranslation;
/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
/**
* @var string
*/
protected $temp_id;
/**
* @param Magento\Framework\App\Helper\Context $context
* @param Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param Magento\Store\Model\StoreManagerInterface $storeManager
* @param Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
* @param Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Sales\Model\Order\Invoice $invoice
) {
$this->_scopeConfig = $context;
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->inlineTranslation = $inlineTranslation;
$this->_transportBuilder = $transportBuilder;
}
/**
* Return store configuration value of your template field that which id you set for template
*
* @param string $path
* @param int $storeId
* @return mixed
*/
protected function getConfigValue($path, $storeId)
{
return $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* Return store
*
* @return Store
*/
public function getStore()
{
return $this->_storeManager->getStore();
}
/**
* Return template id according to store
*
* @return mixed
*/
public function getTemplateId($xmlPath)
{
return $this->getConfigValue($xmlPath, $this->getStore()->getStoreId());
}
/**
* [generateTemplate description] with template file and tempaltes variables values
* @param Mixed $emailTemplateVariables
* @param Mixed $senderInfo
* @param Mixed $receiverInfo
* @return void
*/
public function generateTemplate($emailTemplateVariables,$senderInfo,$receiverInfo)
{
$template = $this->_transportBuilder->setTemplateIdentifier($this->temp_id)
->setTemplateOptions(
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND, /* here you can defile area and
store of template for which you prepare it */
'store' => $this->_storeManager->getStore()->getId(),
]
)
->setTemplateVars($emailTemplateVariables)
->setFrom($senderInfo)
->addTo($receiverInfo['email'],$receiverInfo['name']);
return $this;
}
/**
* [sendInvoicedOrderEmail description]
* @param Mixed $emailTemplateVariables
* @param Mixed $senderInfo
* @param Mixed $receiverInfo
* @return void
*/
/* your send mail method*/
public function sendMail($emailTemplateVariables,$senderInfo,$receiverInfo,$type,$invoiceId = null)
{
if ($type == 'request') {
$typeId = self::XML_PATH_EMAIL_TEMPLATE_FIELD_REQUEST;
} elseif ($type == 'approve') {
$typeId = self::XML_PATH_EMAIL_TEMPLATE_FIELD_APPROVE;
if (!empty($invoiceId)) {
$this->attachInvoicePdf($invoiceId);
}
} else{
$typeId = self::XML_PATH_EMAIL_TEMPLATE_FIELD_REJECT;
}
$this->temp_id = $this->getTemplateId($typeId);
$this->inlineTranslation->suspend();
$this->generateTemplate($emailTemplateVariables,$senderInfo,$receiverInfo);
$transport = $this->_transportBuilder->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
}
public function attachInvoicePdf($invoiceId){
// ???? how
}
}
template:
<!--@subject Your Request is Approved @-->
<!--@vars
{"htmlescape var=$name":"Recipient Name",
"htmlescape var=$sendername":"Sender name",
"var code":"Gift Card Code",
"var balance":"Gift Card Balance",
"var status":"Status",
"htmlescape var=$message":"Message",
"var expiredat":"Expired At"}
@-->
<!--@styles
body,td { background:#FFFFFF; margin:0; padding:0; color:#2f2f2f; font:13px/1.35em Verdana, Arial, Helvetica, sans-serif; }
pre{ white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;}
@-->
{{template config_path="design/email/header_template"}}
<div style="font:13px/1.35em Verdana, Arial, Helvetica, sans-serif;">
<table cellspacing="0" cellpadding="0" border="0" width="98%"
style="margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;">
<tr>
<td align="center" valign="top">
<!-- [ middle starts here] -->
<table cellspacing="0" cellpadding="0" border="0" width="660">
<tr>
<td valign="top">
<p>
<strong>Dear {{var customer_name}},</strong>,<br/>
Your request to process {{var amount}} has been approved and processed.</br>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
if i have invoice id as a variable, how can i attach my pdf invoice to this email?