简体   繁体   English

在电子邮件PHP中包含图片

[英]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 . 您将需要使用完整的URL

Instead of: 代替:

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

It needs to be an absolute URL that the public can see, such as: 它必须是公众可以看到的绝对URL,例如:

<img src="https://image-website.com/images/EmailHeader.png" width="75%">

You can not upload image with relative path in Email. 您无法在电子邮件中上传具有relative path图像。 You have to use full path because when the mail is send it require whole path to fetch the image content. 您必须使用full path因为发送邮件时,它需要完整路径来获取图像内容。 ,

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. 另一个选择是您可以使用base64映像。 But in your case you have to give full path. 但是在您的情况下,您必须给出完整的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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