如何在 phpmailer 中嵌入图片 - 我做不到,为什么?

How to embed image in phpmailer - I can't do it, why?

我想在 phpmailer 的邮件中包含一张图片。以下是我的代码,正在发送邮件但没有嵌入图像,而是它们似乎附加到电子邮件中。 不确定我的代码有什么问题,请帮忙?

<!doctype html>
<html>
<head>
<meta charset="UTF-8">

<?php
    require_once('class.phpmailer.php');   
    require_once('class.smtp.php');    
    $mail = new PHPMailer();   
    $mail->CharSet = "UTF-8";
    $mail->From = "xxxxx";    
    $mail->FromName = "Jan Nowak";   
    $mail->AddReplyTo('xxxx'); 

    $mail->Host = "xxxxxx";  
    $mail->Mailer = "smtp";   
    $mail->SMTPAuth = true;    
    $mail->Username = "xxxxx";    
    $mail->Password = "xxxxxx";    
    $mail->Port = xxx;  usługi poczty
    $mail->Subject = "temat";    
    $mail->Body = 'treść maila';    

    $mail->IsHTML(true);
    $mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
    $mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
        "<p>This is a test picture: <img src=\"images/Kartka.png\" /></p>";


     //$mail->addAttachment ('images/Kartka.jpg'); 
    $mail->AddAddress ("xxxxx");    

     if($mail->Send())    
        {                      
        echo 'E-mail został wysłany'; 
        }            
    else    
        {           
        echo 'E-mail nie mógł zostać wysłany';    
        }
  ?>  

</html>
</head>

<img> 标签上添加 src='cid:Kartka'

$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
"<p>This is a test picture: <img src=\"cid:Kartka\" /></p>";

你为什么用那么多\??你也可以这样做:

<img src="cid:Kartka"/>

这不起作用的原因是您没有将包含图像 html 的字符串分配给电子邮件正文中使用的变量。问题出在第二行的分号,然后是您在第三行的字符串中引用图像的方式。

$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
    "<p>This is a test picture: <img src=\"images/Kartka.png\" /></p>";

应该是

$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>".
    "<p>This is a test picture: <img src=\"cid:Kartka\" /></p>";

或者

$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p><p>This is a test picture: <img src=\"cid:Kartka\" /></p>";

在您希望图像出现在正文中的位置添加此标签

$mail->Body = "... <img src='cid:logo.png> ..";

$mail->AddEmbeddedImage($_SERVER['DOCUMENT_ROOT']."[path_to_image] logo.png",'logo.png','logo.png');

适用于 Outlook 和所有其他电子邮件客户端软件