Send HTML in email via PHP and WordPress

Send HTML in email via PHP and WordPress

I like to share full format of PHP code and HTML format to send email on website.

Mail Function for PHP:

First you have to create a PHP file name like myemail.php (You can create your own)

PUT HTML form,

<form action="#" method="POST">
<input name="first_name"/>
<input name="last_name"/>
<input name="email"/>
<input name="phone_number"/>
<input name="messages"/>
<input type="submit"/>
</form>


Then start PHP tag to condition. If first_name means our 1st input fields, last_name, email and phone number to check its empty or not. If empty then our conditions return false and this message doesn't send otherwise message send.

Form conditions like,

<?php
if( isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['phone_number'])) {}
?>

If user fill up all fields. Then we are storing all input value in $msg variable. Look like,

$msg =
"<h4>YOUR DETAILS</h4>".
"First name: <b>".$_POST['first_name']."</b></br>".
"Last name: <b>".$_POST['last_name']."</b></br>".
"Email: <b>".$_POST['email']."</b></br>".
"Phone Number: <b>".$_POST['phone_number']."</b></br>".
"Additional Notes: <b>".$_POST['messages']."</b></br>";

Then header set HTML format,

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: Quote' . "\r\n";

And finally send message code:

// send email
$result = mail("youremail@mail.com","Subject",$msg,$headers);
if($result) {
echo '<div class="alert alert-success" role="alert">Message has been sent</div>';
}else {
echo '<div class="alert alert-success" role="alert">Message not sent</div>';
}


If mail sent then you will show "Message has been sent"

Full Code:

<?php
if( isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['phone_number'])) {

$msg =
"<h4>YOUR DETAILS</h4>".
"First name: <b>".$_POST['first_name']."</b></br>".
"Last name: <b>".$_POST['last_name']."</b></br>".
"Email: <b>".$_POST['email']."</b></br>".
"Phone Number: <b>".$_POST['phone_number']."</b></br>".
"Additional Notes: <b>".$_POST['messages']."</b></br>";

$from = $_POST['email'];

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: Quote' . "\r\n";
// send email
$result = mail("youremail@mail.com, ".$from,"New Quote: From ".$from,$msg,$headers);
if($result) {
echo '<div class="alert alert-success" role="alert">Message has been sent</div>';
}else {
echo '<div class="alert alert-success" role="alert">Message not sent</div>';
}

}else {
//die('no post data to process');
}

?>


Example 1:

<?php
$to = "youremail@mail.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>

<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>


Example 2:

<?php
$to = 'yourmail@mail.com';
$subject = 'Website Change Request';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: name@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
?>
Send HTML in email via PHP and WordPress Send HTML in email via PHP and WordPress Reviewed by TechTubeHQ on September 06, 2022 Rating: 5

No comments:

Powered by Blogger.