2

I want to send transaction email with specific ID(transaction email id) with cron scheduled.

can anyone help me on this

thanks

2 Answers 2

2

Try this,

Firstly you create a cron job,for that you can follow this link http://inchoo.net/magento-2/running-cron-jobs-in-magento-2/ after that add below code in construct() method

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

and in execute() method :

try {

        $email='demoemail@gmail.com';//replace with your transaction email id
        $error = false;
        $cname = 'demo';//replace with your receiver name
        $cemail = 'demo@gmail.com';//replace with your receiver email
        $csubject = 'demo send email';
        $sender = [
         'name' => $cname,
         'email' => $cemail,
         ];

        $postObject=array(
         'myname' => $cname,
         'message' => $message,
         'subject'=>$csubject);
        $this->inlineTranslation->suspend();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $transport = $this->transportBuilder
           ->setTemplateIdentifier('mymodule_email_template')
           ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            )
           ->setTemplateVars($postObject)
           ->setFrom($sender)
           ->addTo($email)
           ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();

         } catch (\Exception $e) {            
            $this->messageManager->addError(
            __('We can\’t process your request right now. Sorry, that\’s all we know.')
        );
        } 
5
  • ok thanks i will try this and get back to you
    – Raghu
    Jul 12, 2017 at 7:00
  • while compile iam getting this error StateInterface does not exist
    – Raghu
    Jul 12, 2017 at 8:35
  • Include below line after namespace define
    – Nidhi
    Jul 12, 2017 at 10:09
  • use \Magento\Framework\Mail\Template\TransportBuilder; use \Magento\Framework\Translate\Inline\StateInterface;
    – Nidhi
    Jul 12, 2017 at 10:09
  • ok thanks.....but how to run the script from root folder
    – Raghu
    Jul 12, 2017 at 10:11
1

Try this, Include below line before class :

use \Magento\Framework\Mail\Template\TransportBuilder;
use \Magento\Framework\Translate\Inline\StateInterface;
2
  • Actually my intension is...I need to send a transactional email template every week( via cron)
    – Raghu
    Jul 12, 2017 at 10:16
  • Set a cron which run every week
    – Nidhi
    Jul 12, 2017 at 12:08

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.