Sending email through html file with images using phpmailer

I am sending emails to my subscribers using phpmailer. I have an HTML file which contains text and images. Emails are sending successfully but the weird thing is my images are not showing. My images are located at root directory of my HTML file. Is there a way to send email through HTML file with images located at my local directory? I put my images in my HTML file like <img src="images/log.png">

I don't want to give my image url in my HTML file like

<img src="">

here is my code:

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$link= mysqli_connect("$db_host","$db_username","$db_pass", "db") or die ("could not connect to mysql"); 
//if($link){echo "connect";}
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
     $query = "select customer_email from subscribers";
     $result = mysqli_query($link, $query) or die("No customer in the table");;

     while($values = mysqli_fetch_array($result)){

         $toemail = $values['customer_email'];

    //Server settings
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = '(myhost)';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '(my email)';                 // SMTP username
    $mail->Password = '(my password)';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->setFrom('', 'Premier');
    $mail->addAddress(''.$toemail.'', ''.$name.'');     // Add a recipient
    $mail->addReplyTo('', 'Information');

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
include "file.html";
$contents = ob_get_clean();

   echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;

here is my html file

<!doctype html>
<meta charset="utf-8">

<img src="images/logo.png">
<h1> Hello World ..!!! </h1>
<p> This is the <b> auto generated email from HTML Fie </b> for testing purpose. </p>

any help would be highly appreciable.

1 answer

  • answered 2018-04-17 05:44 Synchro

    I don't want to give my image url in my HTML file like...

    Well you're out of luck there. Email messages have no root URL, so relative URLs cannot work; you must include a hostname if you want to use linked images.

    Your only alternative is to use embedded images, where the image data is included within the message, though it's not something you should do for large volumes as it's very inefficient. You can do this in PHPMailer using the addEmbeddedImage() method, and then refer to images from your HTML using their cid values. For example:

    $mail->addEmbeddedImage('images/logo.png', 'logo');

    Then in your HTML:

    <img src="cid:logo">

    The msgHTML() method does this for you.