0

I am using SMTP on my website and all the mails are working fine. I am receiving all the emails. Now I am developing a custom script on magento root to send mails but it is not working. This is the link that I am using to create the script. Magento 2: Sending Email Programmatically

This is the solution from the above link that I am using. enter image description here

The code that I have created on magento root

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

require __DIR__ . '/app/bootstrap.php';

$objectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, []);

$objectManager = $objectManagerFactory->create([]);

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('global');

$email = new \Zend_Mail();
$email->setSubject("Feedback email");
$email->setBodyText('test mail');
$email->setFrom('[email protected]', 'AFSupply');
$email->addTo('[email protected]', 'Rajeev');
$email->send();

I do not see any error message but I never receive the mails. Plz let me know what I am doing wrong as according to me this should work as the answer in the link has many upvotes. Plz suggest as I am stuck in this.

1

1 Answer 1

0

I don't know why the script in the question did not worked but then I tried to send mails using Magento\Framework\Mail\Template\TransportBuilder as mentioned in the comment and it worked. Here is the code.

<?php
$templateVars = array(
                'sku' => '102365874',
                'email' => 'someEmail.com',
                'zip'   => '654321'
            );

$from = array
(
    'name' => 'Rajeev',
    'email' => 'exampleEmail.com' 
);

$transportBuilder = $objectManager->get('Magento\Framework\Mail\Template\TransportBuilder');

$transport = $transportBuilder->setTemplateIdentifier('configurable_product_stock_check')
        ->setTemplateOptions(['area' => 'frontend', 'store' => '1'])
        ->setTemplateVars($templateVars)
        ->setFrom($from)
        ->addTo('[email protected]', 'Rajeev')
        ->getTransport();
        
$transport->sendMessage();
?>

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.