Hubert Kubasiewicz
Hubert Kubasiewicz

Reputation: 375

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

I am trying to include an image into my message in phpmailer. The following is my code, Mails are being sent but without the embeded image, instead they're appear to be attached to the email. Not sure what is wrong with my code, please help?

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

Upvotes: 7

Views: 13110

Answers (3)

RP Singh
RP Singh

Reputation: 31

Add this tag where you want the image to appear in the body

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

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

Works in Outlook and all other email client software

Upvotes: 2

PeteB
PeteB

Reputation: 122

This reason why this isn't working is because you haven't assigned the string containing the html for the image to a variable that's used in the email body. The problem is with the semi-colon on the second line and then the way you reference the image in the string on the third line.

$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>";

Should be

$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>";

Or alternatively

$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>";

Upvotes: 0

Ryan
Ryan

Reputation: 412

Add on the <img> tag put 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>";

Why you use so much \ ?? you can do it also like this :

<img src="cid:Kartka"/>

Upvotes: 6

Related Questions