Send Email with a Template Using PHP

Pass PHP variable from html to php and email template

You're trying to put your email in its own template file, which is great, but you need to actually execute the PHP code within it to fill out the variables.

Rename email-template.html to email-template.phtml. That's a fairly standard extension for php templates.
It's worth noting that using $_REQUEST is not recommended in PHP as it takes both POST and GET parameters, and you'd decided that the user needs to POST the form.

Include and execute the template file in sendmail.php:

<?php
include "class.smtp.php";
include "class.phpmailer.php";

function render_email($email, $message) {
ob_start();
include "email-template.phtml";
return ob_get_contents();
}

$email = $_POST['email'] ;
$message = $_POST['message'];

$body = render_email($email, $message);
...
...

render_email is including the template file, exposing the two variables $email and $message to that template file and then returning the output of including the template.

You use these variables in template.phtml as you were trying to do before.
email-template.phtml:

<table border="1">
<tr>
<td colspan="2">
<h3>
Your registration is:<?= htmlspecialchars($message) ?>
</h3>
</td>
</tr>
</table>

Send HTML in email via PHP

It is pretty simple. Leave the images on the server and send the PHP + CSS to them...

$to = 'bob@example.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: susan@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);

It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

Here is the link I got the information from... (link)

You will need security though...

How to send a html / html5 template email with php?

You need to set the variables before you include the template script, so they'll be expanded when the template runs.

ob_start(); 
$name = "John";
$to = "sadas@yahoo.com";
include('default-message.html');
$message = ob_get_contents();
ob_end_clean();

$subject = "Hello";

$headers .= 'From: Me <abc@mysite.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail ($to, $subject, $message, $headers);

Also, default-message.html should not contain <?php include 'mailservice.php'; ?>, because then it will go back and forth with each script including the other.

PHP html email, using html template

As Pekka was saying, you could simply use str_replace to insert data in your template. Just add a placeholder:

<div clas="header"></div>
<div class="content">{{content}}</div>
<div class="footer"></div>

Then replace the placeholder with your content:

$content = 'Whatever you want to insert...';
$tpl = file_get_contents('yourtemplate.html');
$tpl = str_replace('{{content}}', $content, $tpl);
mail($tpl, ...);

Send html emails using PHPMailer and html templates

Using ob_start

ob_start();
include 'htmlemail.php';
$body = ob_get_clean();

Or

You can also use a templating method to generate the email body for multiple uses.

E.g.

In your html template, have variables assigned like this:

Thank you {NAME} for contacting us.

Your phone number is {PHONE}

Then before calling your phpmailer, create an array to process the email body:

$email_vars = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
);

And finally, with phpmailer...

$body = file_get_contents('htmlemail.phtml');

if(isset($email_vars)){
foreach($email_vars as $k=>$v){
$body = str_replace('{'.strtoupper($k).'}', $v, $body);
}
}

This way your emails will have all the dynamic content you need in the body.

Sending HTML Email via PHP with Variables and External HTML Template

A better approach would be to just include the file and use ob_get_clean():

ob_start();
if($_POST['service']=="Service 1") {include 'email_template-service-1.php';}
else include 'email_template-service-2.php';
$message = ob_get_clean();

Anything echoed between ob_start() and $message = ob_get_clean() will go into the $message variable.



Related Topics



Leave a reply



Submit