Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005

    Unanswered: multiple attachments in PHP

    Hello friends, I'm working on a script that processes the POST data from a form and send it to an email address. The data to be sent includes a variable number of attachments (min 4 max N) of image/jpeg type. I managed to send just one attachment, but when I tried to insert a for cycle to add all required attachments the script doesn't work well, I just got a weird, corrupted attached file. As I'm a newbie in this PHP stuff, I attach my code hoping somebody can give me some solutions. Thanks a lot in advance!

    PHP Code:
    $imgStr $_POST['listOfPix'];          // comes as ,pic1.jpg,pic2.jpg,pic3.jpg...
    $imgArray explode(","$imgStr);
    $imgArrayLen count($imgArray);

    // Define CR/LF for different OS (Windows or Mac or Linux)
    if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
    } elseif (
    strtoupper(substr(PHP_OS,0,3)=='MAC')) {
    } else {


    include (
    $body ob_get_contents();

    $header "From: ".$correo.$eol;
    $header .= "Reply-To: ".$correo.$eol;
    $header .= "Return-Path: ".$correo.$eol;
    $header .= "X-Priority: 3\r\n"// Urgent message set to 1; normal set to 3
    $header .= "X-Mailer: PHP/".phpversion().$eol;

    #Files for Attachment
    for ($i 1$i <= $imgArrayLen$i++) { // attaching all pics, one after another

    $f_name "upload/".$imgArray[$i];    // use relative path OR ELSE big headaches.
    $handle fopen($f_name'rb');
    $f_contents fread($handlefilesize($f_name));
    $f_contents chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode();
    $f_type filetype($f_name);

    # Boundry for marking the split & Multitype Headers
    $header .= 'MIME-Version: 1.0'.$eol;
    $header .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
    $message "";
    # Attachment
    $message .= "--".$mime_boundary.$eol;
    $message .= "Content-Type: image/jpeg; name=\"".$imgArray[$i]."\"".$eol;
    $message .= "Content-Transfer-Encoding: base64".$eol;
    $message .= "Content-Disposition: attachment; filename=\"".$imgArray[$i]."\"".$eol.$eol// !! This line needs TWO end of lines !! IMPORTANT !!
    $message .= $f_contents.$eol.$eol;
    // End For

    # HTML Version
    $message .= "--".$mime_boundary.$eol;
    $message .= "Content-Type: text/html; charset=iso-8859-1".$eol;
    $message .= "Content-Transfer-Encoding: 8bit".$eol;
    $message .= $body.$eol.$eol

    # Finished
    $message .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.

    ini_set(sendmail_from,'');  // the INI lines are to force the From Address to be used !
    if (mail($_POST['recipient'], $_POST['subject'], $message$header)) $mail_success 1; else $mail_success 0;

    $n_location "Location: ".$_POST['redirect']."?mail_success=".$mail_success;

  2. #2
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    could I ask why tyou are writing your own routine?

    I have seen several classes which will do this sort thing for you

    possible sources include PEAR,

    If you want to roll your own functionality fine, but unless you have got a really good reason to do so I don't see why you shouldn't use a freely available code already tested and debugged (we hope)

    And as your skill develops contribute back some classes of your own.

    In many ways its a bit like this forum, take what you need, when you need and give back what you can, when you can.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jun 2005
    Your hint is indeed a very good one, I haven't figured to search for mail classes already done. I've just found a good one at , it is pretty easy to set up and running. Thanks Healdem!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts