user860511
user860511

Reputation:

Include a HTML Email Template into PHP Code

I have a really nice looking HTML Template that I now need to implement into my mailing system. I am currently using this to send emails:

$to   = $dbuser;
$subject = "Welcome";
$from = "[email protected]";
$headers = "From: $from";
$server = ""; 

ini_set ("SMTP", $localhost);

$url="";
$msg="$url";    
$body = Example Text!

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

How would I include a HTML template (along side CSS) directly into the $body variable of my php email form?

I've done quite a bit of research but I can't find anything substantial.

Upvotes: 0

Views: 15545

Answers (5)

Zoltan Toth
Zoltan Toth

Reputation: 47667

http://php.net/manual/en/function.mail.php - example #5

also remember that in HTML emails you're strongly advised to use inline CSS and old-school HTML formatting where possible to assure maximum compatibility with different email clients. Also no divs - just plain old good table-s

Upvotes: 1

Timothy Martens
Timothy Martens

Reputation: 688

I wrote this for myself yesterday:

  1. Create your html and put it in a file named "./email.html" Copy and
  2. paste the code below to a php file in the same dir as the html file.
  3. Modify the image names if you use them in the HTML just do so like so: src="cid:world.jpg"

And thats it...I think. =)

//attachment file paths/names
$files[0] = './world.jpg';
$files[1] = './world2.jpg';

$to = '';
$bcc = "";
$subject = '';
                    $from = "";

$htmlx = '';

$handle = @fopen("./email.html", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        $htmlx .= $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x".$semi_rand."x";

$headers = "From: $from \n";
$headers .= "Reply-To: $from \n";
$headers .= 'Bcc: '. $bcc . "\n";
$headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . ' boundary="'.$mime_boundary.'"'."\n";
$headers .= "X-Author: <Timothy Martens>\n";



$message  = '--'.$mime_boundary."\n";
$message .= 'Content-Type: text/html; charset=UTF-8'."\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n\n". $htmlx . "\n\n\n";


// preparing attachments
for($i=0;$i<count($files);$i++){
    if(is_file($files[$i])){
        $message .= "--".$mime_boundary."\n";
        $fp = @fopen($files[$i],"rb");
        $data = @fread($fp,filesize($files[$i]));
        @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
        "Content-Description: ".basename($files[$i])."\n" .
        "Content-ID: <".basename($files[$i]).">\n".
        "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    }
}
$message .= "--".$mime_boundary."--";

if (mail($to, $subject, $message, $headers)) {
    echo 'Your message has been sent.'."\n";
} else {
    echo 'There was a problem sending the email.'."\n";
}

Upvotes: 0

homerjam
homerjam

Reputation: 679

One way of doing this that I have used in the past is to create the page as you would normally (using html/php etc) and then use file_get_contents($url) like so:

$body = file_get_contents("http://mydomain.com/emailtemplates/template.php?name=John Doe&subject=Hello");

Because you are using http:// the php is executed rather than pulled into the template, simple but effective!

I also would advise you to use inline css and don't be afraid to use tables!

Upvotes: 2

S7SoSt
S7SoSt

Reputation: 1

First of all you need to add some headers, in order for the HTML to display correctly. Taken from the mail() PHP documentation, this is how you do it:

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

After that, I'm assuming $body is where the text should be, so it's a matter of putting all the HTML between quotation marks (escaping every quotation mark in your HTML with a backwards slash), and that's pretty much it.

Upvotes: 0

Shane Fright
Shane Fright

Reputation: 385

Your missing the header required for the email client to interpret the message as HTML. Add the following to your headers:

$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

Upvotes: 2

Related Questions