Advertisement

05.27.2008 at 10:20AM PDT, ID: 23435617
[x]
Attachment Details

PHP send email with attachment

Asked by bloggermouth in PHP Scripting Language

I am using the following function to send out invoices as an attachment. The script is sending the file as expected but the message body in the email shows the following;
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

I would prefer the actual message but can't figure out how to properly form the headers.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
function SendMessages($an, $ae, $copy, $ss, $company, $rr, $xp, $attach) {
	global $headers, $msg;
		if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
		  $eol="\r\n";
		} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
		  $eol="\r";
		} else {
		  $eol="\n";
		}
 
		$file = "/home/ken/www/NetTrade/files/".$attach;
		$file_size = filesize($file);
		$handle = fopen($file, "r");
		$content = fread($handle, $file_size);
		fclose($handle);
		$content = chunk_split(base64_encode($content));
		$name = basename($file);
		$now = date("Y/m/d H:i:s");
		$headers = "From: $an <$ae>". $eol;
		$headers .= "Reply-To: $an <$ae>". $eol;
		$headers .= "Return-Path: $an <$ae>". $eol;
		if ($rr=="yes") {
		$headers .= "Return-Receipt-To: $an <$ae>". $eol;
		$headers .= "Disposition-Notification-To: $an <$ae>". $eol;
		}
		$headers .= "X-Priority: $xp". $eol;
		$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">". $eol;
		$headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters 
		
		# Boundry for marking the split & Multitype Headers
		$mime_boundary=md5(time());
		$headers .= 'MIME-Version: 1.0'.$eol;
		$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol;
		$msg = "";
		
		# Text version of the email
		$headers .= "--".$mime_boundary.$eol;
		$headers .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
		$headers .= "Content-Transfer-Encoding: 7bit".$eol;
		$msg .= "This is a multi-part message in MIME format.".$eol;
		$msg .= "If you are reading this, please update your email-reading-software.".$eol;
		$msg .= "You have received an HTML message from $company.".$eol;
		$msg .= "If you have any questions regarding this email, please contact $an, $ae".$eol;
		
		# HTML Version of the email
		$headers .= "--".$mime_boundary.$eol;
		$headers .= "Content-Type: text/html; charset=iso-8859-1".$eol;
		$headers .= "Content-Transfer-Encoding: 7bit".$eol;
 
		$msg = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
		<html>
		 <head>
		 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
		 <link rel=\"stylesheet\" type=\"text/css\" href=\"$ss\">
		 </head>
		 <body bgcolor=\"#FFFFFF\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">
		 $copy<br><br>
		 </body>
		 </html>".$eol;
 
		# Add Attachment to Email
		$headers .= "--".$mime_boundary.$eol;
		$headers .= "Content-Type: application/pdf; name=\"$attach\"".$eol; 
		$headers .= "Content-Transfer-Encoding: base64".$eol;
		$headers .= "Content-Disposition: attachment".$eol;
		$headers .= $content.$eol;
 
		$msg .= $eol.$eol;
		
		# Finished
		$msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.
	}
[+][-]05.27.2008 at 10:49AM PDT, ID: 21653980

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 10:59AM PDT, ID: 21654066

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:21PM PDT, ID: 21654765

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Sign Up Now!
Solution Provided By: nplib
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628