Magento – how to send custom email no template in magento2

emailmagento2

In magento2 send email with contact module use:

$transport = $this->_transportBuilder
                ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->setReplyTo($post['email'])
                ->getTransport();
$transport->sendMessage();  

So i have create custom send email it not have template. but i don't know how do send email with transportBuilder is true.

this's data i want send.

$myData = Array ( 
   [to] => xxx@gmail.com 
   [email] => admin@gmail.com 
   [subject] => Magento Commerce
   [headers] => array(
            [reply_to] => admin@gmail.com
            )
   [body] => {{template config_path="design/email/header_template"}}
              {{depend Magento Commerce}} Magento Commerce {{/depend}}
                Name:   ABC
                Email:  abc@gmail.com
                Subject:    Magento Commerce
                Message:    Sample text.
            {{template config_path="design/email/footer_template"}}
 [type] => text/html
  [attachments] => 
) 

How do i can send email with $myData use transportBuilder. Many thank!!

Best Answer

You need to create etc/email_templates.xml add below code

<?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="myemail_email_template" label="My Label" file="myemail.html" type="text" module="Modulename_Namespace" area="frontend"/>
</config>

Now you need to add file in your module at view/frontend/email/myemail.html

<!--@subject Promopoup@-->
{{trans "My var: %myVar" myVar=$data.myvar}}

myemail.html is declared in etc/email_templates.xml

Now mail sending code as below:

                $emailTemplateVariables = array();
                $emailTempVariables['myvar'] = $myvar;

                $senderName = 'test';

                $senderEmail = 'sender@test.com';

                $postObject = new \Magento\Framework\DataObject();
                $postObject->setData($emailTempVariables);

                $sender = [
                            'name' => $senderName,
                            'email' => $senderEmail,
                            ];

                $transport = $this->_transportBuilder->setTemplateIdentifier('myemail_email_template')
                ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($sender)
                ->addTo($email)
                ->setReplyTo($senderEmail)            
                ->getTransport();               
                $transport->sendMessage();