1

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 :

app/code/Wo/EyeTest/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Wo_EyeTest" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Cms"/>
        </sequence>
    </module>
</config>

app/code/Wo/EyeTest/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Wo_EyeTest',
    __DIR__
);

app/code/Wo/EyeTest/etc/fronted/routes.xml

<?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" />
        </route>
    </router>
</config>

app/code/Wo/EyeTest/Controller/Form/Index.php

<?php
namespace Wo\EyeTest\Controller\Form;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

app/code/Wo/EyeTest/Block/Index/Index.php

<?php

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();
    }

}

app/code/Wo/EyeTest/view/frontend/layout/eyetest_form_index.xml

<?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">
    <head>
        <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"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="eyeTest_form" template="Wo_EyeTest::eyetest_form.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Wo/EyeTesT/view/templates/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>
        </div>
        <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>
        </div>
    </fieldset>
    <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>
            </button>
        </div>
    </div>
</form>

app/code/Wo/EyeTest/Controller/Form/Postmail.php

<?php
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;

        parent::__construct($context);


    }

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

        try
        {
            // Send Mail
            $this->_inlineTranslation->suspend();

            $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
                ->setTemplateIdentifier('eyetest_email_template')
                ->setTemplateOptions(
                    [
                        'area' => 'frontend',
                        'store' => $this->storeManager->getStore()->getId()
                    ]
                )
                ->setTemplateVars([
                    'name'  => $post['name'],
                    'email'  => $post['email']
                ])
                ->setFrom($sender)
                ->addTo($sentToEmail,$sentToName)
                ->addTo('chapaie@hotmail.com','owner')
                ->getTransport();

            $transport->sendMessage();

            $this->_inlineTranslation->resume();
            $this->messageManager->addSuccess('Email sent successfully');
            $this->_redirect('eyetest/index/index');

        } catch(\Exception $e){
            $this->messageManager->addError($e->getMessage());
            $this->_logLoggerInterface->debug($e->getMessage());
            exit;
        }



    }
}

app/code/Wo/EyeTest/etc/email_templates.xml

<?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"/>

</config>

app/code/Wo/EyeTest/view/frontend/email/eyetestform.html

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

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

{{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 :)

0

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.

0

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

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.