0

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="www.mydomain.com/images/log.png">

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

    //Recipients
    $mail->setFrom('haris.khan@premier.com.pk', 'Premier');
    $mail->addAddress(''.$toemail.'', ''.$name.'');     // Add a recipient
    $mail->addReplyTo('haris.khan@premier.com.pk', 'Information');

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

    $mail->send();
   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>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>

<body>
<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>
</body>
</html>

any help would be highly appreciable.

Haris Khan
  • 335
  • 5
  • 20
  • 2
    use full path of your images like `` – Mohammad Wasim Apr 17 '18 at 05:49
  • 1
    If you don’t want to make your images available via an HTTP(S) URL, then your other option is to embed them into the email directly - see https://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body (Of course this will increase the amount of data your server has to send out for every single email.) – CBroe Apr 17 '18 at 06:47
  • @CBroe kindly review my question again. I edit it. I have my images on https url but in image tag I don't want to give image src as https:domain.com/img. I am giving image source as src="img/logo.png" – Haris Khan Apr 17 '18 at 06:57
  • Possible duplicate of [Send email with PHPMailer - embed image in body](https://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – Nirav Madariya Apr 17 '18 at 07:00
  • _“but in image tag I don't want to give image src as https:domain.com/img. I am giving image source as src="img/logo.png"”_ - that obviously can’t work, because the email itself is not loaded as a document “from your domain”. It’s either full, absolute paths - or embedded images. – CBroe Apr 17 '18 at 08:04

1 Answers1

0

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104