PHP mail multiple attach

∥☆過路亽.° 提交于 2019-12-25 05:07:47

问题


I writte this code to send multiple attachments:

    $tablica_plikow=$_FILES["file"]; //array of files
if(!empty($tablica_plikow['name'])){///if attachment 
          $uid = md5(uniqid(time()));

    $header =  "From: od\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-2\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .=win2iso( $_POST['tresc'])."\r\n\r\n\r\n\r\n\r\n\r\n";///message
    $header .= "--".$uid."\r\n";

    for($i=0; $i<count($tablica_plikow['name']); $i++){

    if ($tablica_plikow["error"][$i] > 0)
    { $komunikat = "<img src=\"img_panel/bttn_error.gif\">"."Return Code: " . $tablica_plikow["error"][$i] ;


    }
    if (file_exists("zalacznik/" . $tablica_plikow["name"][$i]))
      {

      $komunikat = "<img src=\"img_panel/bttn_error.gif\">"."Return Code: " . $tablica_plikow["name"][$i]. " already exists. " ;

      }
    else
      {
        if(is_uploaded_file($tablica_plikow["tmp_name"][$i])) { 
    move_uploaded_file($tablica_plikow["tmp_name"][$i],
    "zalacznik/" . $tablica_plikow["name"][$i]);
    $komunikat = "<img src=\"img_panel/bttn_info.gif\">" . "zalacznik/" . $tablica_plikow["name"][$i];

    $target_path="zalacznik/" . $tablica_plikow["name"][$i];
    $file = "zalacznik/".$tablica_plikow["name"][$i];
    $file_size = filesize($file);
    $handle = fopen($file, "rb");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $name = basename($file);
    $header .= "Content-Type: ".$tablica_plikow["type"][$i]." name=\"".$tablica_plikow["name"][$i]."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\n\r".$content."\r\n\r\n";;
    $header .= "Content-Disposition: attachment; filename=\"".$tablica_plikow["name"][$i]."\"\r\n\r\n";
   // $header .= $content."\r\n\r\n";
    $header .= "--".$uid."-- \r\n";

            }
        }
    }
     if (mail("mail@moj.com", $_POST['tytul'], "", $header)) {

         $komunikat = "<img src=\"img_panel/bttn_info.gif\">mail send";


    } else {
      $komunikat = "<img src=\"img_panel/bttn_error.gif\">error";

      }

But when I send an email with two or more attachments, receive only one file.This file is a concatenation of all attachments. But the first part of the file is the first attachment, and other parts are just hashmap other files.


回答1:


I advice you use standard and tested mailing lib such as phpMail

It has been discussed here before Please see regarding email with attachment in php

Thanks

:)




回答2:


I would use a library eg.

  • Zend_Mail
  • Pear Mail_Mime



回答3:


I'd like to advise you to use PHP Mailer.



来源:https://stackoverflow.com/questions/9953978/php-mail-multiple-attach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!