Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML Email + attachment

Status
Not open for further replies.

JimFL

Programmer
Jun 17, 2005
131
GB
Does anybody have a good solution for creating a HTML email using the mail object in Php that also can attach a static attachment from the server?

I have found various solutions - all that dont seem to resolve my problem.

Can anybody help?

 
phpmailer is widely recommended on this forum as an out of the box solution.

i've also written some code and posted it in the past. and i think there is a FAQ as well that may do what you need. let me know if you want me to repost my code

it's all about getting the content type headers right. you need to start with multipart/mixed and attach the file, then create a multipart/alternative to insert the plain text and html variants of your mail. note, with this method you need TWO different boundary identifiers

if you don't care about non-html clients you can ignore the multipart/alternative and just add the html directly to the mail (as a separate part to the file).

use base64encode() to encode the attachment, and remember to wrap the encoded text at some sensible value (say 70).
 
I would be grateful if I could see your solution as I have searched various posts and Im not sure if they will do what I want.

Thanks for your help.

 
$PDF_file = "test.pdf";
class mime_mail {
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

function mime_mail() {
$this->parts = array();
$this->to = "";
$this->from = "";
$this->cc = "";
$this->subject = "";
$this->body = "";
$this->headers = "";
}


function add_attachment($message, $name = "", $ctype = "application/octet-stream") {
$this->parts [] = array (
"ctype" => $ctype,
"message" => $message,
"encode" => $encode,
"name" => $name
);
}

function build_message($part) {
$message = $part["message"];
$message = chunk_split(base64_encode($message));
$encoding = "base64";
return "Content-Type: ".$part["ctype"].($part["name"]? "; name=\"".$part["name"]."\"" : "")."\nContent-Transfer-Encoding: $encoding\nContent-Disposition: attachment;\nfilename=\"".$part["name"]."\"\n\n$message\n";
}

function build_multipart() {
$boundary = "b".md5(uniqid(time()));
$multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
for($i = sizeof($this->parts)-1; $i>=0; $i--) $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary";
return $multipart.= "--\n";
}

function send() {
$mime = "";
if (!empty($this->from)) $mime .= "From: ".$this->from. "\n";
if (!empty($this->cc)) $mime .= "Cc: ".$this->cc. "\n";
if (!empty($this->headers)) $mime .= $this->headers. "\n";
if (!empty($this->body)) $this->add_attachment($this->body, "", "text/html");
$mime .= "MIME-Version: 1.0\n".$this->build_multipart();
//echo $mime;
return mail($this->to, $this->subject, "", $mime,"-fabc@foo.com");
}
}

$attachment5 = fread(fopen($PDF_file, "r"), filesize($PDF_file));

$mail = new mime_mail();
$mail->from = "test@x.co.uk";
$mail->headers = "Errors-To: xyz@foo.com";
$mail->to = "jimfl@x.co.uk";
$mail->cc = "xxx";
$mail->subject = "CLIENT DOC";
$mail->body = $message;



$mail->add_attachment("$attachment5",$PDF_file, "application/pdf");


This seems to work well - but it has severall problems. I think I need to alter it slightly.

It doesnt seem to email through exchange server - and through hotmail or similar clients the html email is sent as an attachment also. Id prefer it to arrive as an html email with a pdf attachment. Can this be solved by changing the above code?

 
i agree that phpmailer has got to be better than my code. but for what it's worth here is the multipart alternative/multipart mixed mailer that i have posted before (slightly adapted).

Code:
<?
//stub some basic parameters
$to = "          ";
$from = "        ";
$subject = "test subject";

//attachment
$filename = "c:/test.txt"; //complete path to file
//we need the attachment name for a header
$finfo = pathinfo($filename);
$fname = $finfo['basename'];
$fileContents = file_get_contents($filename);

//set up the message text
$defaultmessage = "Your mail client is too old to read html. Get a newer one";

$plaintextmessage = "Hello world";

$htmlmessage = '<div><span style="color:blue;">Hello</span><span style="color:red"> World</span></div>';

//set up the boundaries
$boundary = md5(uniqid("",true));
$b1 = "b1---$boundary";
$b2 = "b2---$boundary";

//separator
$sep = "\n";
//set up the mail header

$headers  = "From: $from$sep";
$headers .= "To: $to$sep";
$headers .= "Return-Path: $from$sep";
$headers .= "MIME-Version: 1.0$sep";
$headers .= "Content-Type: multipart/mixed; boundary=\"$b1\"$sep";
$headers .= "$sep";    

//now set up the message
$message = "--$b1$sep";
$message .= "Content-Type: multipart/alternative; boundary=\"$b2\"$sep";
$message .= "$sep";    

//default message
$message .= $defaultmessage.$sep;
$message .= "$sep";    

//plaintext message

$message .= "--$b2$sep";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"$sep";
$message .= "Content-Transfer-Encoding: 8bit$sep";
$message .= "$sep";
$message .= $plaintextmessage;
$message .= "$sep";

//html message
$message .= "--$b2$sep";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"$sep";
$message .= "Content-Transfer-Encoding: 8bit$sep";
$message .= "$sep";
$message .= $htmlmessage;
$message .= "$sep";
$message .= "--$b2--";
$message .= "$sep";



//create the attachment part
$message .= "--$b1$sep";
$message .= "Content-Type: application/octet-stream$sep";
$message .= "Content-Transfer-Encoding: base64$sep";
$message .= "Content-Disposition: attachment; filename=\"$fname\" $sep";
$message .= "$sep";    
$message .= chunk_split( base64_encode($fileContents), 76, $sep );
$message .= "$sep";
//finish the mail

$message .= "--$b1--$sep";

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

if ($result === FALSE) {
	echo "email was not sent";
} else {
	echo "email was sent";
}
?>

sorry that i have not had time to comment it thoroughly.
 
Hi - thanks for your post. It has resolved the issues I had before and is a much cleaner piece of code. The email is received in HTML and attaches any file perfectly.

Thanks

JimFl

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top