dqw70970 2019-03-23 16:18
浏览 182
已采纳

PHPMailer使用FOR循环添加图像

I have this PHP code:

for($x = 0; $x < count($obrazy); $x++) 
{ 
   $mail->AddEmbeddedImage($obrazy[$x], 'tel'.$x); 
}

the variable "obrazy" is a table storing path (link to the image) e.g. "obraz/zdjecie.png". I need to do this with a loop because the array of obrazy can have a different size each time. The above loop does not work for me, because when I want to show this image in the mail, nothing is displayed.

$iterator = 0;

while ($row = mysqli_fetch_array($rezultat)) 
{      
         $mail->Body = $mail->Body.'//..........

                <img src="cid:tel'.$iterator.'" style="

                    width: 160px;
                    display: block;
                    float: left;

                "/> //..........
           ';

          $iterator++;
}

"// ......" means another part of the code that does not matter to the question being asked. The while loop must be because I extract different data from the database. Everything works only the images do not want to be displayed in the email, even if instead of "$iterator" I enter the number. Links to the image contained in the table are RELIABLE for the PHP file.

  • 写回答

1条回答 默认 最新

  • doudang1052 2019-03-23 17:57
    关注

    Chances are your path or permissions are wrong; Check the return value from addEmbeddedImage:

    if (!$mail->AddEmbeddedImage($obrazy[$x], 'tel'.$x)) {
      echo 'Failed to attach '. $obrazy[$x];
    }
    

    It's probably a good idea to turn the path into an absolute one, for example by saying something like __DIR__ . '/' . $obrazy[$x] instead of just the relative path.

    It may be that your current working directory is not where you think it is. Try doing it like this:

    if (!$mail->AddEmbeddedImage(getcwd() . '/' . $obrazy[$x], 'tel'.$x)) {
      echo 'Failed to attach '. getcwd() . '/' . $obrazy[$x];
    }
    

    Then it will show you the full path of the files it's failing to find, and you can check whether they are correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动