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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办