简体   繁体   中英

Include an image in email PHP

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";
      }
    }
  }
?>

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%">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM