如何在phpmailer中嵌入图像 - 我不能这样做,为什么?

时间:2015-12-23 10:09:50

标签: php embed phpmailer

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

<!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>

3 个答案:

答案 0 :(得分:5)

添加<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"/>

答案 1 :(得分:2)

将此标记添加到您希望图像在正文中显示的位置

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

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

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

答案 2 :(得分:0)

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