1

I am sending conformation email while creating new account to the customer using $customer->sendNewAccountEmail(); in controller it is working fine, but i am trying for at a same time, When I want to send another email with another email template to other customer which are already registered, so that's why i want to know about send email through Programmatically.

1 Answer 1

2

I am using this code for sending email.

namespace [Vender]\[Extension]\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use \Magento\Framework\Mail\Template\TransportBuilder;
use \Magento\Framework\Translate\Inline\StateInterface;
use Psr\Log\LoggerInterface;

class Sendmail extends \Magento\Framework\App\Action\Action
{
    const XML_PATH_EMAIL_ADMIN_QUOTE_SENDER = 'Email Sender';
    const XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION = 'Your Template Path';
    const XML_PATH_EMAIL_ADMIN_NAME = 'Sender Name';
    const XML_PATH_EMAIL_ADMIN_EMAIL = 'Receiver Email';

    protected $inlineTranslation;
    protected $transportBuilder;
    protected $_logLoggerInterface;

    public function __construct(
    Context $context,
    StateInterface $inlineTranslation,
    TransportBuilder $transportBuilder,
    LoggerInterface $logLoggerInterface,
    array $data = [])
    {
        $this->inlineTranslation = $inlineTranslation;
        $this->transportBuilder = $transportBuilder;
        $this->_logLoggerInterface = $logLoggerInterface;
        parent::__construct($context,$data);
    }


    public function execute()
    {
        try
        {

            // Send Mail
            $this->inlineTranslation->suspend();
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
               ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION, $storeScope))
               ->setTemplateOptions(
                    [
                        'area' => 'frontend',
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
               ->setTemplateVars([
                    'var1'  => 'Value',
                    'var2'  => 'Value'
                ])
               ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_QUOTE_SENDER, $storeScope))
               ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_EMAIL, $storeScope))
               ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch(\Exception $e){
            $this->_logLoggerInterface->debug($e->getMessage());
            exit;   
        }
    }
}
12
  • Did you find any answer, I also looking for a similar question. And are you sure above coding is working fine ?
    – Ajith
    Sep 20, 2016 at 8:42
  • I am already used this code in my one extension, It working fine. You need to just replaces your template, Variables, Sender and Receivers of email. Sep 20, 2016 at 9:53
  • I'm new to M2, please could you explain what should be 'Your Template Path'
    – Ajith
    Sep 20, 2016 at 10:46
  • Template path means your custom template path which you want to send while your email send. In your extension you create one html file for email template, that file path you need to specify here. Sep 20, 2016 at 10:49
  • Can you tell me how can attach pdf file in email Nov 25, 2016 at 13:46

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.