I am using Magento 2.2.2
I have one form on my website with the fields "Name" and "Email". Now when the customer submits the form I want to send an email to the email address filled by the customer.
I want to send custom HTML in this email and this HTML includes images. How can I achieve this?
I have used below code:
<form class="form" method="post" name="form_1" id="form_1" enctype="multipart/form-data">
<div class="formdiv">
<h2>Custom Form</h2>
</div>
<label>Name</label>
<input type="text" id="name" name="name" required="required" placeholder="Enter Your Name" />
<label>Email</label>
<input type="email" id="email_share" name="email_share" required="required" placeholder="Enter Your Email" />
<input type="submit" id="submit_result" name="submit_result" value="Submit">
</form>
<?php
if(isset($_POST['submit_result']))
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$to = "[email protected]";
$nameTo = $_POST['txtname'];
$transport = $objectManager->create('Magento\Framework\Mail\Template\TransportBuilder');
$data = $transport
->setTemplateIdentifier(7)
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => 1])
->setFrom(['name' => 'Test','email' => '[email protected]'])
->addTo($to,$nameTo)
->getTransport();
$data->sendMessage();
}
?>
And below as well:
<form class="form" method="post" name="form_1" id="form_1" enctype="multipart/form-data">
<div class="formdiv">
<h2>Custom Form</h2>
</div>
<label>Name</label>
<input type="text" id="name" name="name" required="required" placeholder="Enter Your Name" />
<label>Email</label>
<input type="email" id="email_share" name="email_share" required="required" placeholder="Enter Your Email" />
<input type="submit" id="submit_result" name="submit_result" value="Submit">
</form>
<?php
if(isset($_POST['submit_result']))
{
$to = "[email protected]";
$nameTo = $_POST['txtname'];
$from = "[email protected]";
$nameFrom = "test";
$body ="<table cellpadding='4' cellspacing='0' style='border-collapse:collapse' width='500' border='1'>";
$body.="<tr><td align='left' colspan='2' style='background:#CCC;'><strong>Test email</strong></td></tr>";
$body.="<tr><td width='150' align='left'>Name</td><td align='left'>".$_POST['name']."</td></tr>";
$body.="<tr><td width='150' align='left'>Email</td><td align='left'>".$_POST['email_share']."<br /></td></tr>";
$body.="</table>";
$email = new \Zend_Mail();
$email->setSubject("Test Request");
$email->setBodyHtml($body);
$email->setFrom($from, $nameFrom);
$email->addTo($to, $nameTo);
$email->send();
}
?>
I have tried both logics but they are not working