http://www.php.net/mail includes an example of sending emails with multiple
attachments. For a more structured approach, try using Mail_Message @
  http://opensource.visionp.biz/Mail_Message.120.0.html


Regards,
Erik Osterman
http://osterman.com/


alex at bartl dot net
28-Nov-2002 09:25


/*
This might be some useful stuff to send out emails in either text
or html or multipart version, and attach one or more files or even
none to it. Inspired by Kieran's msg above, I thought it might be 
useful to have a complete function for doing this, so it can be used 
wherever it's needed. Anyway I am not too sure how this script will
behave under Windows.

{br} represent the HTML-tag for line break and should be replaced,
but I did not know how to not get the original tag  parsed here.

function SendMail($From, $FromName, $To, $ToName, $Subject, $Text, $Html,
$AttmFiles)
$From      ... sender mail address like "[EMAIL PROTECTED]"
$FromName  ... sender name like "My Name"
$To        ... recipient mail address like "[EMAIL PROTECTED]"
$ToName    ... recipients name like "Your Name"
$Subject   ... subject of the mail like "This is my first testmail"
$Text      ... text version of the mail
$Html      ... html version of the mail
$AttmFiles ... array containing the filenames to attach like
array("file1","file2")
*/

$TEXT="This is the first test\n in text format\n.";
$HTML="<font color=red>This is the first test in html format.</font>";
$ATTM=array("/home/myself/test/go.jpg",
            "/home/myself/test/SomeDoc.pdf");

SendMail(
    "[EMAIL PROTECTED]","PHP Apache Webmailer",         //sender
    "[EMAIL PROTECTED]","Recipients Name",     //recipient
    "Testmail",                               //subject
    $TEXT,$HTML,$ATTM);                      //body and attachment(s)
    
function
SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){
 $OB="----=_OuterBoundary_000";
 $IB="----=_InnerBoundery_001";
 $Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) 
  or die("neither text nor html part present.");
 $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
 $From or die("sender address missing");
 $To or die("recipient address missing");
    
 $headers ="MIME-Version: 1.0\r\n"; 
 $headers.="From: ".$FromName." <".$From.">\n"; 
 $headers.="To: ".$ToName." <".$To.">\n"; 
 $headers.="Reply-To: ".$FromName." <".$From.">\n"; 
 $headers.="X-Priority: 1\n"; 
 $headers.="X-MSMail-Priority: High\n"; 
 $headers.="X-Mailer: My PHP Mailer\n"; 
 $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

 //Messages start with text/html alternatives in OB
 $Msg ="This is a multi-part message in MIME format.\n";
 $Msg.="\n--".$OB."\n";
 $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

 //plaintext section 
 $Msg.="\n--".$IB."\n";
 $Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
 $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
 // plaintext goes here
 $Msg.=$Text."\n\n";

 // html section 
 $Msg.="\n--".$IB."\n";
 $Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
 $Msg.="Content-Transfer-Encoding: base64\n\n";
 // html goes here 
 $Msg.=chunk_split(base64_encode($Html))."\n\n";

 // end of IB
 $Msg.="\n--".$IB."--\n";

 // attachments
 if($AttmFiles){
  foreach($AttmFiles as $AttmFile){
   $patharray = explode ("/", $AttmFile); 
   $FileName=$patharray[count($patharray)-1];
   $Msg.= "\n--".$OB."\n";
   $Msg.="Content-Type:
application/octetstream;\n\tname=\"".$FileName."\"\n";
   $Msg.="Content-Transfer-Encoding: base64\n";
   $Msg.="Content-Disposition:
attachment;\n\tfilename=\"".$FileName."\"\n\n";
            
   //file goes here
   $fd=fopen ($AttmFile, "r");
   $FileContent=fread($fd,filesize($AttmFile));
   fclose ($fd);
   $FileContent=chunk_split(base64_encode($FileContent));
   $Msg.=$FileContent;
   $Msg.="\n\n";
  }
 }
    
 //message ends
 $Msg.="\n--".$OB."--\n";
 mail($To,$Subject,$Msg,$headers);    
 //syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");
}

-----Original Message-----
From: Ajay Singh [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 06, 2003 10:19 AM
To: [EMAIL PROTECTED]
Subject: [PHP] Multiple attachments in an email

Hi,

Please guide how to send multiple attachments in one mail through php mail
script. The following code is working for the one attachment:

Thanks & regards,
Ajay


 // Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

// Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! </p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to