代码之家  ›  专栏  ›  技术社区  ›  Matt Hutch

在电子邮件php中包含图像

  •  1
  • Matt Hutch  · 技术社区  · 6 年前

    我遇到了一个问题,只有来自身体的文字出现,图像是通过打破,有人看到我可能会出错吗?

    <?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";
          }
        }
      }
    ?>
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   naturaljoin    6 年前

    您使用的图像url是相对的。你需要使用 完整URL 是的。

    而不是:

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

    它需要是公众可以看到的绝对url,例如:

    <img src="https://image-website.com/images/EmailHeader.png" width="75%">
    
        2
  •  0
  •   Bhavin    6 年前

    你不能用 relative path 在电子邮件中。你必须使用 full path 因为 当邮件发送时,需要整个路径来获取图像内容。 我是说,

    换掉这条线,

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

    有了这条线,

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

    另一个选项是您可以使用 base64 它的图像。但在你的情况下,你必须给出完整的路径。