1

I'm experiencing an issue where only the text from body is appearing the image is coming through broken, does anyone see where I might be going wrong?

<?php
  require("php/PHPMailer.php");
  require("php/SMTP.php");

  if (isset($_GET['email'])) {

    $emailID = $_GET['email'];

    if($emailID = 1){
      $mail = new PHPMailer\PHPMailer\PHPMailer();
      $mail->IsSMTP(); // enable SMTP

      $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
      $mail->SMTPAuth = true; // authentication enabled
      $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
      $mail->Host = "";
      $mail->Port = 465; // or 587
      $mail->IsHTML(true);
      $mail->Username = "";
      $mail->Password = "";
      $mail->SetFrom("");
      $mail->Subject = "Test";
      $mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';

      $mail->AddAddress("");

      if(!$mail->Send()) {
          echo "Mailer Error: " . $mail->ErrorInfo;
      } 
      else {
          echo "Message has been sent";
      }
    }
  }
?>
Matt Hutch
  • 433
  • 1
  • 5
  • 18
  • 1
    `if($emailID = 1){` !== `if($emailID == 1){` - one equal is *always* true. And you need a full path to the live image, or include it with the email. – Qirel Jul 12 '18 at 13:46
  • 1
    add full url of the image – Sugumar Venkatesan Jul 12 '18 at 13:46
  • 2
    You can't use a relative path on your server for images in emails. You can embed it with base64 or use the full URL, keep in mind, this is opened on the person's computer, not on your server. – Devon Jul 12 '18 at 13:47
  • Possible duplicate of [How to add image to php mail body](https://stackoverflow.com/questions/26861891/how-to-add-image-to-php-mail-body) – k0pernikus Jul 12 '18 at 13:49

2 Answers2

0

The image URL you are using is relative. You will need to use the full URL.

Instead of:

<img src="images/EmailHeader.png" width="75%">

It needs to be an absolute URL that the public can see, such as:

<img src="https://image-website.com/images/EmailHeader.png" width="75%">
codeafin
  • 465
  • 2
  • 7
0

You can not upload image with relative path in Email. You have to use full path because when the mail is send it require whole path to fetch the image content.,

Replace this line,

  $mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';

With this line,

  $mail->Body = '<html><body><p>My Image</p><img src="http://your-domainname.com/images/EmailHeader.png" width="75%"></body></html>';

Another option is that you can use use base64 images for it. But in your case you have to give full path.

Bhavin
  • 1,962
  • 5
  • 33
  • 49