Magento 2 mail delivery problem
Debugging mail delivery problems is always a pain, especially with Magento. Sometime there is a problem with your shop settings, sometimes a problem in one PHP script. Often a complete other source of problem with mail server, hoster or spam filters. Debugging all this is sometimes not possible.
Magento 2 mail delivery problem
If you are working on a new Magento 2 project you may the following scenario: You create your first test order and you are happy, that everything works. Then you understand, that you didn’t get an order mail. You are trying to resend it from your adminhtml and everything looks good:
But your mailbox is still empty. What should you do now?
If you need to debug mail delivery problems, you will need a simple process to resend mails. It is not a good idea to create new orders, because it needs far too much time. Magento offers you the possibility to resend every email template with your custom data. You can send order mails completely without an order. So for this I created a small script that you can call from your Magento 2 root directory to send mails by template:
$receiverMail = "email@example.com";
$templateId = 1; // id of email template
$storeId = 1; // desired store id
$templateParams = ; // params of template by array
var_dump(mail($receiverMail, "Test Subject", "Test Message"));
use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$objectManager = Magento\Framework\App\ObjectManager::getInstance();
$transportBuilder = $objectManager->create("Magento\Framework\Mail\Template\TransportBuilder");
$transport = $transportBuilder->setTemplateIdentifier($templateId)
->setTemplateOptions(['area' => Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
->setFrom(array('email' => 'firstname.lastname@example.org', 'name' => 'SenderName'))
This simple script sends 2 mails.
- standard PHP mail()
this mail is for detecting configuration problems of your server. You need to get this mail. If not, there may be a problem with your PHP settings or mail server.
- Magento template mail
if this mail also works, than you have a proof, that there is no basically problem with sending template mails from Magento.
You can configure this test script in first 4 lines. You can set a different receiver mail address (you should!), set a template id of mail which is sent and also define used store. If you know params a mail template needs, you can set this in template params array. You can find template ids in your adminhtml under Marketing -> Email Templates:
If you do not get any mail, you should check server logs, especially mail logs. As far as I know, this logs are really helpful in finding a problem. If you are on a managed server or shared host, you can contact your admin or support. If everything should be fine and you can’t get mails, the problem may be a spam filter on your mailbox. Try a different mailbox are get to its configuration.
To find Magento 2 mail delivery problem, you can use my test script and tips. Finding the problem was never that easy.