6

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>
Ali
  • 2,993
  • 3
  • 19
  • 42
Hubert Kubasiewicz
  • 365
  • 1
  • 3
  • 16
  • 1
    Possible duplicate of [Send email with PHPMailer - embed image in body](http://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – Oliver Nybroe Dec 23 '15 at 10:16
  • 1
    Also take a look at the [`msgHTML()`](http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_msgHTML) method. – Synchro Dec 23 '15 at 13:41

3 Answers3

6

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"/>
Ryan
  • 412
  • 2
  • 12
  • $mail->AddEmbeddedImage('images/Kartka.png', 'Kartka'); $mail->Body = "

    Test 1 of PHPMailer html

    This is a test

    "; "

    This is a test picture:

    "; - but still it does not work....
    – Hubert Kubasiewicz Dec 23 '15 at 16:07
  • So now I have like this because if I have like this - it says Parse error: syntax error, unexpected T_STRING in /home/ln168827/public_html/phpmail/index.php on line 27 - I done understand what You meant by Add on the tag put src='cid:Kartka' – Hubert Kubasiewicz Dec 23 '15 at 16:09
  • Don't use slashes: / - I can help you further on chat? – Ryan Dec 23 '15 at 16:33
2

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

Xpleria
  • 5,472
  • 5
  • 52
  • 66
RP Singh
  • 31
  • 2
0

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>";
PeteB
  • 124
  • 10