如何在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"/>

将此标签添加到您希望图像出现在主体中的位置

$mail->Body = "... <img src='cid:logo.png> ..";
$mail->AddEmbeddedImage($_SERVER['DOCUMENT_ROOT']."[path_to_image] logo.png",'logo.png','logo.png');

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

这之所以不起作用,是因为您没有将包含图像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>";