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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵