Search code examples
phpphpmailer

PHPmailer AddEmbeddedImage failed to embed Image


I tried two different email client tried google & stackoverflow answered. But failed to solve the issue. I also tested the local image path, it is perfectly ok.

Here is the coding part:

  $mail->addEmbeddedImage('../img/abc-company-
   logo.png','logo','abc-company-logo.png');

Body Part:

$mail->Body    = "<div id='container' style='height:auto;font-
   family:Helvetica;border: 1px solid #CCC;'>
 <div id='header' style='margin: 0 auto; 
  background-color:#0958C3; color:#FFFFFF;
  font-size: 20px;text-align: center;
display:block;
 '>
<img src='cid:logo' alt='Picture Description'/> <br>
<strong>Heading</strong> 
 </div>
</div>";

Solution

  • Problem is that PHPMailer embeds inline images relative to where the script is called from, instead of relative to the document called in.

    This means by passing ../img/abc-company-logo.png to the addEmbeddedImage() will search for such path relative to the actual PHPMailer class file location.

    You should change the path to an absolute file path. See __DIR__, __FILE__ examples here http://php.net/manual/fa/language.constants.predefined.php

    For Example:

    $mail->addEmbeddedImage(dirname(__DIR__) . '/img/abc-company-logo.png','logo','abc-company-logo.png');