0

I am currently trying to send an email from an observer.

This is the code:

/app/code/UO/CustomerAdressChange/Observer

if(!empty($isModified)) {
    //Liefer- oder Rechnungsadresse ist veraendert worden -> eMail schicken

    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $transportBuilder = $objectManager->get('\Magento\Framework\Mail\Template\TransportBuilder');
    $storeId = $storeManager->getStore()->getId();


    $transport = $transportBuilder->setTemplateIdentifier('adress_change_template')
        ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
        ->setTemplateVars(
            [
                'store' => $storeManager->getStore(),
            ]
        )
        ->setFrom('general')
        // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
        ->addTo('[email protected]', 'Customer Name')
        ->getTransport();
    $transport->sendMessage();              }

This is causing an error sadly.

Is there any standard email template identifier which I can set here:

$transport = $transportBuilder->setTemplateIdentifier('adress_change_template')

Any help is welcome. Thanks!

Edit:

/app/code/UO/CustomerAdressChange/etc/email_templates.xml

<?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="adress_change_template" label="AdressChange" file="adresschange.html" type="html" module="UO__CustomerAdressChange" area="frontend"/>
</config>

/app/code/UO/CustomerAdressChange/view/frontend/email/adresschange.html

<div>

Das ist ein Test 

</div>

It seems like it doesn't find my email template?

Update: This line is causing an error:

$email = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('trans_email/ident_general/email');


->setFrom($email, 'Service')

2 Answers 2

1

I wanted to comment instead of post, but this seems like my only way to contribute. Have you created a custom config.xml somehow in order to produce the desired email address? The reason that line keeps failing, is because I imagine that the sender resolver keep hitting a roadblock.

function Setfrom()
  {
         $result = $this->_senderResolver->resolve($from);
         $this->message->setFrom($result['email'], $result['name']);
         return $this;
  }

resolve($sender,$scopeId = null)

Resolve sender information. The $sender can be a string to identify which sender to lookup in the config and return the name and email for. The $sender can be an array prefilled with the name and email key/value pairs.

so perhaps, it is because it is not written as

$from = array('email' => "[email protected]", 'name' => 'AnyName');  
->setFrom($from)
1
  • thanks that helped a lot! Now this is line is the problem: ->getTransport(); Jun 12, 2018 at 14:17
1

key code of sending mail:

use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
class ClassName implements ObserverInterface
{
protected $storeManager;
protected $_transportBuilder;
protected $inlineTranslation;
public function __construct(StoreManagerInterface $smi,TransportBuilder $tb,StateInterface $si)
{        
    $this->storeManager=$smi;
    $this->_transportBuilder=$tb;
    $this->inlineTranslation=$si;
}
public function yourFunctionName()
{
       $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
        $templateVars = array(
            'store' => $this->storeManager->getStore(),
            'customer_name' => $cname,
            'message'   => 'We processed your order ID '.$orederid.'. We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from [email protected]'
        );
        $from = array('email' => "[email protected]", 'name' => 'AnyName');
        $this->inlineTranslation->suspend();
        $to = array('[email protected]');
        $transport = $this->_transportBuilder->setTemplateIdentifier('order_template')
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)
            ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();
   } 
}

please note it setTemplateIdentifier('order_template') so you need to create order_template. for create email template refer this link

8
  • is there a standard email template identifier which I can try? Jun 12, 2018 at 13:06
  • yes try your email template
    – user68116
    Jun 12, 2018 at 13:10
  • Please read my update above. Jun 12, 2018 at 13:31
  • setTemplateIdentifier('adress_change_template'); try this code
    – user68116
    Jun 12, 2018 at 13:32
  • it's not working unfortunately Jun 12, 2018 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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