Email with attachment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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: contact@xxxxxxxxxxx'.$eol;
$headers .= 'Reply-To: contact@xxxxxxxxxxx'.$eol;
$headers .= 'Return-Path: contact@xxxxxxxxxxx'.$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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux