Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 557
Hubert Kubasiewicz Avatar asked Dec 23 '15 10:12

Hubert Kubasiewicz


People also ask

Can I use PHPMailer with xampp?

If you're a PHP application developer, you might need to send email notifications through your custom PHP applications. With XAMPP, there are two ways to do this: you can use the included sendmail.exe binary, or you can use the third-party PHPMailer class.

Can I use PHPMailer in localhost?

The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.

How many emails can I send with PHPMailer?

PHPMailer does not set any limits, nor does the mail() function, it's only ISPs like GoDaddy that do. However, they do so by blocking access to normal SMTP ports, and/or redirecting SMTP connections to their own servers ( *. secureserver. * ).


2 Answers

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"/>
like image 126
Ryan Avatar answered Sep 28 '22 02:09

Ryan


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

like image 26
RP Singh Avatar answered Sep 28 '22 02:09

RP Singh