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.

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

报告相同问题?

悬赏问题

  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真