views:

74

answers:

3

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

$to = "[email protected]";  
$subject = "Test mail";  
$message = "Hello! This is a simple email message.";  
$from = "[email protected]";  
$headers = "From: $from";  
mail($to,$subject,$message,$headers);  
echo "Mail Sent.";  

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

A: 

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:

http://phpmailer.worxware.com/index.php?pg=exampleagmail

Tomasz Kowalczyk
A: 

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!

Jordy
I would send the email automatically so the user doesn't need to type the content.
anonymous123
Then why you need a template? To show the user it has been sended?
Jordy
template i mean like a design or a background for the email content/message.
anonymous123
Watch my edit on my answer, vote me +1 :D
Jordy
How to use the template??
anonymous123
Jordy your way of the mark here, check my answer, that's what he is looking for.
RobertPitt
A: 

Lets have a small crack at this :)

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

    public function __construct($to = false)
    {
        if($to !== false)
        {
            if(is_array($to))
            {
                foreach($to as $_to){ $this->recipients[$_to] = $_to; }
            }else
            {
                $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)
    {
         if(!file_exists($path_to_file))
         {
             trigger_error('..blah...',E_USER_ERROR);
             return;
         }
         $this->path_to_file = $path_to_file;
    }

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


    public function compile()
    {
        ob_start();

        include $this->path_to_file;
        extract($this->variables);

        $content = ob_get_contents();
        ob_end_clean();

        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(
    '[email protected]',
    '[email protected]'
);

$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
Emailer->send();

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

Regards.
RobertPitt