I'm kind of newbie in Magento and don't understand everything I read on the subject so I would be glad if some of you could help me find why I did wrong in the process :)

First of all, I have a test with multiple question and at the end of it, I have a form the client can complete to receive his result by mail. So I created a custom module following this guide : https://www.codextblog.com/magento-2/sending-custom-emails-using-custom-module-in-magento-2/

When I enter the email & the name in the input, I got the " email sent successfully " message but no mail in my mailbox, even in spam section.

I don't know how to debug this code so there is my code :


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <module name="Wo_EyeTest" setup_version="1.0.0">
            <module name="Magento_Cms"/>




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="eyetest" frontName="eyetest">
            <module name="Wo_EyeTest" />


namespace Wo\EyeTest\Controller\Form;
class Index extends \Magento\Framework\App\Action\Action
    public function execute()



namespace Wo\EyeTest\Block\Index;

class Index extends \Magento\Framework\View\Element\Template {

    public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = []) {

        parent::__construct($context, $data);


    protected function _prepareLayout()
        return parent::_prepareLayout();



<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <css src="https://pro.fontawesome.com/releases/v5.12.0/css/all.css" src_type="url"/>
        <css src="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" src_type="url"/>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="eyeTest_form" template="Wo_EyeTest::eyetest_form.phtml"/>


<form action="<?php echo $block->getBaseUrl().'eyetest/form/postmail/';?>" name="customemaildata" method="post" id="contactForm-1" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}'>
    <fieldset class="fieldset">
        <div class="field email required">
            <label class="label" for="email"><span><?php  echo __('Name') ?></span></label>
            <div class="control">
                <input name="name" id="name" title="<?php echo __('Name') ?>" class="input-text" type="text" data-validate="{required:true}"/>
        <div class="field email required">
            <label class="label" for="email"><span><?php  echo __('Email') ?></span></label>
            <div class="control">
                <input name="email" id="email" title="<?php echo __('Email') ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
    <div class="actions-toolbar">
        <div class="primary">
            <input type="hidden" name="hideit" id="hideit" value="" />
            <button type="submit" title="<?php echo __('Submit') ?>" class="action submit primary">
                <span><?php echo __('Submit') ?></span>


namespace Wo\EyeTest\Controller\Form;

use Zend\Log\Filter\Timestamp;
use Magento\Store\Model\StoreManagerInterface;

class Postmail extends \Magento\Framework\App\Action\Action
    const XML_PATH_EMAIL_RECIPIENT_NAME = 'trans_email/ident_support/name';
    const XML_PATH_EMAIL_RECIPIENT_EMAIL = 'trans_email/ident_support/email';

    protected $_inlineTranslation;
    protected $_transportBuilder;
    protected $_scopeConfig;
    protected $_logLoggerInterface;
    protected $storeManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $loggerInterface,
        StoreManagerInterface $storeManager,
        array $data = []

        $this->_inlineTranslation = $inlineTranslation;
        $this->_transportBuilder = $transportBuilder;
        $this->_scopeConfig = $scopeConfig;
        $this->_logLoggerInterface = $loggerInterface;
        $this->messageManager = $context->getMessageManager();
        $this->storeManager = $storeManager;



    public function execute()
        $post = $this->getRequest()->getPost();

            // Send Mail

            $sender = [
                'name' => $post['name'],
                'email' => $post['email']

            $sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

            $sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

            $transport = $this->_transportBuilder
                        'area' => 'frontend',
                        'store' => $this->storeManager->getStore()->getId()
                    'name'  => $post['name'],
                    'email'  => $post['email']
                ->addTo('[email protected]','owner')


            $this->messageManager->addSuccess('Email sent successfully');

        } catch(\Exception $e){



<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">

    <template id="eyetest_email_template" label="Email Form" file="eyetestform.html" type="html" module="Wo_EyeTest" area="frontend"/>



<!--@subject Sending email from my custom module @-->

{{template config_path="design/email/header_template"}}
    <tr class="email-intro">
            Email: {{var email}}
            Name: {{var name}}

{{template config_path="design/email/footer_template"}}

Thank you for any help you could offer me to understand what I did wrong & don't receive mail :)

2 Answers 2


So I found out what's wrong.

I had to configure an SMTP in the BO but there was a checkbox checked that prevent any mail to be sent in developer mode..

If Enable, Magento will not delivery any email to receiver. This is useful for developers.

I just needed to uncheck this.

For anyone that got the problem, its in Store > SMTP > Configuration > Developer > Developer Mode : No


The step is as normal as every other bugs.

You need to identify where the code break. How to do it is follow the flow of your application.

  1. Does the form display?
  2. When you submit, does it send the request to the server? (can it reach the controller?)
  3. When it comes to the server, is everything in the form get sent correctly? (has all the data sent to the controller?)
  4. When you have everything in the right format, is the mail server working? (now you have all the data you want, it's time to send to mail server)
  5. ...

You can continue step by step to check the flow of your application.

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.