5

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 = 'notARealAddress@123.net';

$email = 'testemail@123.net';

$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.

  • The code does not seem to be the problem, mostly (except for the myemail.html file and the other code in the wrong folders ). What is lacking is a module, a registration file, and a controller to send the email. I will post a solution to this if I can ever get it right LW. If anyone else has a solution, please feel free to chime in. – Krang Sep 9 '16 at 20:20
  • do you have any errors in the log? – dchayka Sep 22 '16 at 20:22
1
<?php
public function sendMailAction(){
$html="
put your html content here
blah blah

";
$mail = Mage::getModel('core/email');
$mail->setToName('Your Name');
$mail->setToEmail('Youe Email');
$mail->setBody('Mail Text / Mail Content');
$mail->setSubject('Mail Subject');
$mail->setFromEmail('Sender Mail Id');
$mail->setFromName("Msg to Show on Subject");
$mail->setType('html');// YOu can use Html or text as Mail format

try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
$this->_redirect('');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
$this->_redirect('');
}
}
?>

and you can get the sender email using

$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.