Take the 2-minute tour ×

How can I send an email using php then add a template design in the email? I'm using this:

$to = "someone@example.com";  
$subject = "Test mail";  
$from = "someonelse@example.com";  
$headers = "From: $from";  
echo "Mail Sent.";  

And it works fine! The problem is just how to add a template.

share|improve this question
You should work on your acceptrate first.. –  Dennis Haarbrink Sep 14 '10 at 7:30

6 Answers 6

Lets have a small crack at this :)

class Emailer
    var $recipients = array();
    var $EmailTemplate;
    var $EmailContents;

    public function __construct($to = false)
        if($to !== false)
                foreach($to as $_to){ $this->recipients[$_to] = $_to; }
                $this->recipients[$to] = $to; //1 Recip

    function SetTemplate(EmailTemplate $EmailTemplate)
        $this->EmailTemplate = $EmailTemplate;

        //When you use the send( ) function you would use $EmailTemplate->compile();

Notice the function SetTemplate() ...

Heres a a small template class

class EmailTemplate
    var $variables = array();
    var $path_to_file= array();
    function __construct($path_to_file)
             trigger_error('Template File not found!',E_USER_ERROR);
         $this->path_to_file = $path_to_file;

    public function __set($key,$val)
        $this->variables[$key] = $val

    public function compile()

        include $this->path_to_file;

        $content = ob_get_contents();

        return $contents;

Here's a small example, you still need to do the core of the script but this will provide you with a nice layout to get started with.

$emails = array(

$Emailer = new Emailer($emails);
 //More code here

$Template = new EmailTemplate('path/to/my/email/template');
    $Template->Firstname = 'Robert';
    $Template->Lastname = 'Pitt';
    $Template->LoginUrl= 'http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php';

$Emailer->SetTemplate($Template); //Email runs the compile

Thats really all there is to it, just have to know how to use objects and its pretty simple from there, ooh and the template would look a little something like this:

Welcome to my site,

Dear <?php echo $Firstname ?>, You have been registered on our site.

Please visit <a href="<?php echo $LoginUrl ?>">This Link</a> to view your upvotes

share|improve this answer
Just brilliant! –  Jeff Jan 27 '12 at 3:03
Nice job. Doesn't the extract statement in the compile method need to precede the include statement? –  Bart Jacobs Jan 31 '12 at 10:08
No it needs to be called prior to loaded, this allows the template variables to be defined and in scope for the template content. –  RobertPitt Feb 2 '12 at 4:15
Hi Robert, great snippet but I am not sure I follow how the mail is sent. You could easily compile the template into SetTemplate, but then there is no send function and the class Emailer does not extend any Mailer class (with a send or mail() function)... am I missing something here? –  AKFourSeven Feb 21 '13 at 9:24

Why not try something as simple as this :

$variables = array();

$variables['name'] = "Robert";
$variables['age'] = "30";

$template = file_get_contents("template.html");

foreach($variables as $key => $value)
    $template = str_replace('{{ '.$key.' }}', $value, $template);

echo $template;

Your template file being something like :


<p>My name is {{ name }} and I am {{ age }} !</p>

share|improve this answer
This is just what I was looking for! –  CMH Aug 31 '13 at 3:03
I like your solution better. Very simple. –  DS. Oct 17 '13 at 2:28

You must only assign certain value [HTML code] as a $message - it wil render automatically. If you don't want to bother with that [X- statuses], just use PHPMailer:


share|improve this answer
        $message_to_client = file_get_contents("client_email.html");
        //$message_to_client = "bla bla {{ EMAIL }} bla bla";

        $variables = array(
            'SITE_TITLE' => $SITE_TITLE,
            'SITE_LOGO' => $SITE_LOGO,
            'SITE_URL' => $SITE_URL,
            'CLIENT_NAME' => strip_tags($data->clientname),
            'PHONE' => strip_tags($data->phone),
            'EMAIL' => strip_tags($data->email),
            'CITY' => strip_tags($data->city),
            'REGION' => strip_tags($data->region),
            'COMMENT' => htmlentities($data->comment)                

        $message_to_client = preg_replace_callback('/{{([a-zA-Z0-9\_\-]*?)}}/i',
             function($match) use ($variables) { 
                 return  $variables[$match[1]]; 
        }, $message_to_client );
share|improve this answer

Try this....

$body='<table width="90%" border="0">
        <td><b>Name:</b></td> <td>'.$name.'</td>
        <td><b>Email:</b></td> <td>'.$email.'</td>
        <td><b>Message:</b></td> <td>'.$message.'</td>

share|improve this answer

Design your own template.

Build up a form for the user to e-mail and build a nice design around it, pick some colour or design your template with something like illustrator or photoshop.

So pick basic html design or images with illustrator! Google: html templates or css templates.. free css templates. Something like that and you will find what you want;)!

Good luck on your journey to epic failure :D!

share|improve this answer
I would send the email automatically so the user doesn't need to type the content. –  anonymous123 Sep 14 '10 at 7:32
Then why you need a template? To show the user it has been sended? –  Jordy Sep 14 '10 at 7:33
template i mean like a design or a background for the email content/message. –  anonymous123 Sep 14 '10 at 7:36
Watch my edit on my answer, vote me +1 :D –  Jordy Sep 14 '10 at 7:45
How to use the template?? –  anonymous123 Sep 14 '10 at 7:49

Your Answer


