Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Attaching HTML files to emails

8:58 pm on Oct 4, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 28, 2003
posts: 486
votes: 1

I'm using the following php code to try to attach .html files to email using php but it's not attaching the file:

$subject = "Test email";
$mail_headers= "From: Me <me@mydomain.com>\r\n";
$mail_boundary = md5(uniqid(time()));
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed;boundary=\"$mail_boundary \"";
$mail_headers .= "\r\n\r\n";
$mail_headers .= "This is a multi-part message in MIME format.";
$mail_headers .= "\r\n\r\n";
$userfile = "/home/account_name/public_html/test.html";
$fp = fopen($userfile, "r");
$file = fread($fp, filesize($userfile));
$file = chunk_split(base64_encode($file));
$mail_body = "--$mail_boundary\n";
$mail_body .= "Content-type: text/plain; charset=euc-kr\r\n";
$mail_body .= "Content-transfer-encoding: 8bit\r\n\r\n";
$mail_body .= " Here goes the project file.\r\n";
$mail_body .= "--$mail_boundary\r\n";
$filename = basename($userfile);
$mail_body .= "Content-type: text/html; name=$filename\r\n";
$mail_body .= "Content-transfer-encoding:base64\r\n\r\n";
$mail_body .= $file. "\r\n\r\n";
$mail_body .= " --$mail_boundary--";
$send_check2 = mail($recipient_email,$subject,$mail_body,$mail_headers);
if ($send_check2) {
echo "<p>The email has been sent.</p>";

The email is being sent out but there is no attachment. What am I doing wrong?

Also, it shows this output in the email message:
This is a multi-part message in MIME format.


Content-type: text/plain; charset=euc-kr
Content-transfer-encoding: 8bit

Here goes the project file.

Content-type: text/html; name=test.html


8:44 am on Oct 5, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
votes: 0

Google for PHPMailer. Its easy to use and will solve all your mail problems.


5:08 am on Oct 6, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 28, 2003
posts: 486
votes: 1

Thanks! I'll look into it.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members