Re: Email with attachment

From: Date: Sat, 15 May 2010 17:06:55 +0000
Subject: Re: Email with attachment
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-305258@lists.php.net to get a copy of this message
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: contact@example.com'.$eol; $headers .= 'Reply-To: contact@example.com'.$eol;
$headers .= 'Return-Path: contact@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?

« previous php.general (#305258) next »