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


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_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;

    public function execute()

            // Send Mail
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
               ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION, $storeScope))
                        'area' => 'frontend',
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    '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))
        } catch(\Exception $e){
  • 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.