PHP多文件上传然后通过PHPMailer内联到电子邮件

时间:2016-05-26 15:18:51

标签: php arrays loops file-upload phpmailer

我有一个麻烦的PHPMailer,并搜索了这个信息的高低,但是nada。

基本上我有一个表单,用户可以上传多个图像然后保存到我服务器上的文件夹中,然后我需要通过PHPMailer发送一封内嵌电子邮件底部的图像的电子邮件。

我可以将第一张图像设为内联,但其余图像不显示..

一些代码:

require '../PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

if(count($_FILES['upload']['name']) > 0){
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) {
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != ""){

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];


            //save the url and the file
            $filePath = "../reports/".$id."/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];

            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) {

                $files[] = $shortname;
                //insert into db 
                //use $shortname for the filename
                //use $filePath for the relative url to the file


          $mail->AddEmbeddedImage($filePath, $shortname, $shortname);
          $atts = '<img src="cid:'.$shortname.'">';


            }
          }
    }
}
$mail->Body .= $atts;

1 个答案:

答案 0 :(得分:0)

解决方案:

$dir2 = opendir('../reports/'.$id);  // Open the directory containing the uploaded files.

$files = array();
while ($files[] = readdir($dir2));
rsort($files);
closedir($dir2);

foreach ($files as $file) {


      if ($file != "." && $file != ".." && $file != 'resources' ){
      $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
      $url = '../reports/'.$id.'/'.$file;
      $mail->AddEmbeddedImage($url, $withoutExt);
      $mail->Body .= '<img src="cid:'.$withoutExt.'">';
      }

}