Re: [PHP] Email with attachment
Php Developer wrote: Hi, I found a good function at php.net that sends email with atachment, but unfortunately i still cannot figure out how to send a body of the email too. Here is the code: function sendEmailWithAttachement($to, $subject, $message, $file) { if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; } elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; } else { $eol="\n"; } $filename = basename($file); What does the following do? Does it echo something to the screen? displaymessage($filename); $file_size = filesize($file); $file_type = filetype($file); displaymessage($file_size); Shouldn't you be checking to see if the file exists and is readable before you try opening it? $handle = fopen($file, "rb"); $content = fread($handle, $file_size); $content = chunk_split(base64_encode($content)); You should make sure ( strlen($content) !== 0 ) at this point. fclose($handle); # Common Headers $headers = 'From: cont...@example.com'.$eol; $headers .= 'Reply-To: cont...@example.com'.$eol; $headers .= 'Return-Path: cont...@example.com'.$eol; // these two to set reply address $headers .= "Message-ID: <".time()." 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/related; boundary=\"".$mime_boundary."\"".$eol; $msg = ""; # Attachment $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: ".$file_type."; name=\"".$filename."\"".$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf' $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= $message; Looks like you are missing some line endings on the above line. Plus, why do the following lines require two EOL's but the above lines only use one EOL? $msg .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $content.$eol.$eol; # Setup for text OR html $msg .= "Content-Type: multipart/alternative".$eol; # Text Version $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$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 .= "+ + Text Only + +".$eol.$eol; # Finished $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. mail($to, $subject, $msg, $headers); } -- Jim Lucas A: Maybe because some people are too annoyed by top-posting. Q: Why do I not get an answer to my question(s)? A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Email with attachment
Hi, I found a good function at php.net that sends email with atachment, but unfortunately i still cannot figure out how to send a body of the email too. Here is the code: function sendEmailWithAttachement($to, $subject, $message, $file) { if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; } elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; } else { $eol="\n"; } $filename = basename($file); displaymessage($filename); $file_size = filesize($file); $file_type = filetype($file); displaymessage($file_size); $handle = fopen($file, "rb"); $content = fread($handle, $file_size); $content = chunk_split(base64_encode($content)); fclose($handle); # Common Headers $headers = 'From: cont...@example.com'.$eol; $headers .= 'Reply-To: cont...@example.com'.$eol; $headers .= 'Return-Path: cont...@example.com'.$eol; // these two to set reply address $headers .= "Message-ID: <".time()." 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/related; boundary=\"".$mime_boundary."\"".$eol; $msg = ""; # Attachment $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: ".$file_type."; name=\"".$filename."\"".$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf' $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= $message; $msg .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $content.$eol.$eol; # Setup for text OR html $msg .= "Content-Type: multipart/alternative".$eol; # Text Version $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$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 .= "+ + Text Only + +".$eol.$eol; # Finished $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. mail($to, $subject, $msg, $headers); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Email with attachment function
Hello, I'm trying to write a function that sends an email with an attachment using sendmail, But I can't seem to get it working, the code I currently have is below, Any ideas would be much appreciated. I'm probably doing something stupid as the output is what its meant to be except it isn't divided up (i.e. attachment is text) Regards Mark Cubitt function send_mail($to, $from, $subject, $body, $attachment = '', $attachmentDir = '') { $path_to_sendmail = "/usr/sbin/sendmail"; $fp = popen("$path_to_sendmail -t", "w"); if ($attachment != '') { $filename = $attachmentDir.$attachment; $filePointer = fopen($filename, "rb"); $fileContent = fread($filePointer, filesize($filename)); $fileContent = chunk_split(base64_encode($fileContent)); fclose($filePointer); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $num = fputs($fp, "To: $to\n"); $num += fputs($fp, "From: $from\n"); $num += fputs($fp, "Subject: $subject\n"); $num += fputs($fp, "MIME-Version: 1.0\n"); $num += fputs($fp, "Content-type: multipart/mixed;\n"); $num += fputs($fp, " boundry=\"{".$mime_boundary."}\"\n\n"); $num += fputs($fp, 'This is a multi-part message in MIME format'."\n"); $num += fputs($fp, "\n"); $num += fputs($fp, "--{".$mime_boundary."}\n"); $num += fputs($fp, "Content-type: text/html; charset=iso-8859-1\n"); //$num += fputs($fp, "Content-Transfer-Encoding: 7bit\n"); $num += fputs($fp, "\n"); // $num += fputs($fp, "Subject: $subject\n"); $num += fputs($fp, "$body"); $num += fputs($fp, "--{".$mime_boundary."}\n"); $num += fputs($fp, "--{".$mime_boundary."}\n"); $num += fputs($fp, "Content-type: application/zip;\n"); $num += fputs($fp, " name=\"{".$attachment."}\"\n"); $num += fputs($fp, "Content-Transfer-Encoding: base64\n"); //$num += fputs($fp, "Content-Disposition: attachment;\n"); //$num += fputs($fp, " filename=\"{".$attachment."\"}\n"); $num += fputs($fp, "\n"); $num += fputs($fp, $fileContent); $num += fputs($fp, "\n"); $num += fputs($fp, "--{".$mime_boundary."}--\n"); } else { $num = fputs($fp, "To: $to\n"); $num += fputs($fp, "From: $from\n"); $num += fputs($fp, "MIME-Version: 1.0\n"); $num += fputs($fp, "Content-type: text/html; charset=iso-8859-1\n"); $num += fputs($fp, "Subject: $subject\n\n"); $num += fputs($fp, "$body"); } pclose($fp); if ($num>0) { return 1; } else { return 0; } } // end function send_mail() -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php