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

This is the code:


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])
                'store' => $storeManager->getStore(),
        // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
        ->addTo('myemail@mail.com', 'Customer Name')
    $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!



<?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"/>



Das ist ein Test 


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')

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' => "xxxxx@xxx.xxx", 'name' => 'AnyName');  
  • thanks that helped a lot! Now this is line is the problem: ->getTransport(); – k33n Jun 12 '18 at 14:17

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)
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 support@xxx.com'
        $from = array('email' => "xxxxx@xxx.xxx", 'name' => 'AnyName');
        $to = array('xxxxx@xxx.xxx');
        $transport = $this->_transportBuilder->setTemplateIdentifier('order_template')

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

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

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.