Hello! In this post, you will learn to send email with html template using php.
1. Extract($_POST) – Extracting Post Variables From.
2. file_get_contents – Html design form to get the function.
Normally we use php mailer function for sending email with some text also.But here i have design one html template along to send with mail script.
Demo Code is below:
Index.php
2 3 4 5 6 7 8 9 10 11 12 13 14 |
<form action="email.php" method="post"> <p> <input type="text" required placeholder="Enter your firstname" name="firstname" /> </p> <p> <input type="email" required placeholder="Enter your email" name="email" /> </p> <p> <input type="submit" name="send" value="Submit your Request"> </p> </form> |
email.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php if(isset($_POST['send'])) { extract($_POST); $to="$email"; //change to ur mail address $strSubject="Tutorialswebsite | Email | $firstname"; $message = file_get_contents('templete.php'); $message=str_replace('{{firstname}}', $firstname, $message); $headers = 'MIME-Version: 1.0'."\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers .= "Bcc: pradeepku041@gmail.com\r\n"; $headers .= "From: info@tutorials.com"; $mail_sent=mail($to, $strSubject, $message, $headers); if($mail_sent) echo "<script>alert('Thank you. we will get back to you'); window.location='index.php';exit();</script>"; else echo "<script>alert('Sorry.Request not send'); window.location='index.php';exit();</script>"; } ?> |
template.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<html> <body> <table width="100%" cellspacing="0" cellpadding="0" style="max-width:600px;border-left:solid 1px #e6e6e6;border-right:solid 1px #e6e6e6"> <tbody> <tr><td> <b>Hello {{firstname}},</b> <center><p>This tutorials based on sending templates with email</p></center> <center><p>For mare information <a href="http://www.tutorialswebsite.com">click here<a></p></center> <center><h1><a href="http://www.tutorialswebsite.com">Tutorialswebsite<a></h1></center> </td> </tr> </tbody> </table> </body> </html> |
Suggested Posts:
How to Send Email using PHPMailer in Laravel
Send Email via SMTP Server in PHP using PHPMailer
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co