0

I need to create a custom script which allows me to send a simple test email with email addresses of Sender, Receiver CC Bcc; Subject line and Body of Email. How could I do this in Magento 2.x?

----- Update -----

I tried to send email by creating a function in my custom script but it didn't work:

Here is my function which I created in my custom script.

function sendEmail(){
    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $transport = $objectManager->create('Magento\Framework\Mail\Template\TransportBuilder'); 
    $templateVars = [
                        'message'    => "This is a test email."
                    ];
    $templateId =1;
    $storeId =1;

    $data = $transport
                    ->setTemplateIdentifier($templateId)
                    ->setTemplateOptions(['area' => Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
                    ->setTemplateVars($templateVars)
                    ->setFrom([
                                'name' => 'Test',
                                'email' => '[email protected]'
                                ])
                    ->addTo(['[email protected]','[email protected]'])
                    ->setReplyTo('[email protected]')
                    ->addBcc('[email protected]')
                    ->getTransport();
    $data->sendMessage();
}

ERROR:

Fatal error: Uncaught UnexpectedValueException: Email template '1' is not defined. in /var/www/html/magento232sd/vendor/magento/module-email/Model/Template/Config.php:225 Stack trace: #0 /var/www/html/magento232sd/vendor/magento/module-email/Model/Template/Config.php(166): Magento\Email\Model\Template\Config->_getInfo(1, 'type') #1 /var/www/html/magento232sd/vendor/magento/module-email/Model/Template.php(215): Magento\Email\Model\Template\Config->getTemplateType(1) #2 /var/www/html/magento232sd/vendor/magento/module-email/Model/AbstractTemplate.php(709): Magento\Email\Model\Template->getType() #3 /var/www/html/magento232sd/vendor/magento/module-email/Model/AbstractTemplate.php(331): Magento\Email\Model\AbstractTemplate->isPlain() #4 /var/www/html/magento232sd/vendor/magento/module-email/Model/Template.php(379): Magento\Email\Model\AbstractTemplate->getProcessedTemplate(Array) #5 /var/www/html/magento232sd/vendor/magent in /var/www/html/magento232sd/vendor/magento/module-email/Model/Template/Config.php on line 225

2

1 Answer 1

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.