I am attempting to send an email from my custom success.phtml file that will contain some needed information.
Everything else in the success file works except for the email part.
I have tried sending email with a template and without one.
The Order Confirmation Page is blank when I use the email code.
I am still a Magento newbie.
I have looked at and used other examples and code, and even the Magento 2 github ones, but obviously I don't understand what is missing.
I used the code from the answer of this post as a guide: how to send custom email no template in magento2?
Can anyone tell me what I need to change in my code?
This is what I have for my latest coding attempt:
In the web server, I made a file called email_templates.xml in /home/userName/public_html/mySite/app/etc/ .
In that email_templates.xml file I pasted the code below:
<?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>
I then made a file called myemail.html in /home/userName/public_html/mySite/vendor/magento/module-email/view/frontend/email
In that myemail.html file I pasted the code below:
<!--@subject Promopoup@-->
{{trans "My var: %myVar" myVar=$data.myvar}}
This is the email part that I pasted in my custom success.phtml file:
<?php
$emailTemplateVariables = array();
$emailTempVariables['myvar'] = $myvar;
$senderName = 'notARealName.com';
$senderEmail = '[email protected]';
$email = '[email protected]';
$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();
?>
Thanks.