How to send email verification mail in magento 2 programmatically
In Magento 2, you can send an email verification mail programmatically using the Magento\Framework\Mail\Template
class and the Magento\Framework\Mail\Message
class. Here's an example of how to do it:
Step 1: Create a new email template
Create a new email template in the app/code/Vendor/Module/view/frontend/email
directory. For example, create a file named verify_email.html
with the following content:
<p>Hello {{ var.name }},</p>
<p>Thank you for verifying your email address.</p>
<p>Best regards,</p>
<p>Your Name</p>
Step 2: Create a new email template variable
In the verify_email.html
file, add a variable name
to store the customer's name. You can do this by adding the following code:
{{ var.name }}
Step 3: Create a PHP class to send the email
Create a new PHP class in the app/code/Vendor/Module/Model
directory. For example, create a file named EmailVerification.php
with the following content:
<?php
namespace Vendor\Module\Model;
use Magento\Framework\Mail\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\App\ObjectManager;
class EmailVerification
{
private $template;
private $message;
public function __construct(
Template $template,
MessageInterface $message
) {
$this->template = $template;
$this->message = $message;
}
public function sendVerificationEmail($customerEmail, $customerName)
{
// Set the email template
$this->template->setTemplate('verify_email');
// Set the email variables
$this->template->setVariables(['name' => $customerName]);
// Set the email sender
$this->message->setFrom('[email protected]');
// Set the email recipient
$this->message->addTo($customerEmail);
// Set the email subject
$this->message->setSubject('Verify Your Email Address');
// Send the email
$this->message->send();
}
}
Step 4: Use the PHP class to send the email
In your Magento 2 module's etc/di.xml
file, add the following code to register the EmailVerification
class:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Vendor\Module\Model\EmailVerification" type="Vendor\Module\Model\EmailVerification"/>
</config>
Now, you can use the EmailVerification
class to send an email verification mail programmatically. For example, you can use the following code to send an email verification mail when a customer creates an account:
<?php
namespace Vendor\Module\Controller\Account;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\EmailVerification;
class CreateAccount extends Action
{
private $emailVerification;
public function __construct(
Context $context,
EmailVerification $emailVerification
) {
parent::__construct($context);
$this->emailVerification = $emailVerification;
}
public function execute()
{
// Get the customer email and name
$customerEmail = '[email protected]';
$customerName = 'John Doe';
// Send the email verification mail
$this->emailVerification->sendVerificationEmail($customerEmail, $customerName);
// Return a success message
return $this->resultFactory->create(ResultFactory::TYPE_SUCCESS)->setData('Email verification mail sent successfully!');
}
}
That's it! With these steps, you should be able to send an email verification mail programmatically in Magento 2.