2

I'm trying to add image to mail body from folder.but it not display only display text on alt tag.this is my code.

<?php 
   $to = $email;
        $subject="Reservation notification From abc";
        $from = 'abc@abc.com';
            $body = '<html><body>';
            $body .= '<img src="img/logo.png" alt="Hotel" /><br/>';
            $body .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
            $body .= '<p style="color:#3366FF;font-size:14px;">Hello'.' '.$name.',</p>';
            $body .= "</table>";
            $body .= "</body></html>";

        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $$from \r\n";
        $headers = 'MIME-Version: 1.0' . "\r\n" .
           'Content-type: text/html; charset=iso-8859-1' . "\r\n" .

           'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $body,$headers);
?>
KT1
  • 311
  • 2
  • 4
  • 13
  • 1
    img/logo.png works on web pages because browser considers it as a relative url and appends your domain name to the src. But in emails you should use absolute paths. Check my answer below. – Ananth Nov 11 '14 at 09:51

2 Answers2

4

This should work as expected:

<?php 
    ...
    $body .= '<img src="http://domain.com/absolute/path/to/image/img/logo.png" alt="Hotel" /><br/>';
    ...
?>
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • My image locaton in 'Ubuntu' is `/var/www/html/test_mail/logo.png` so I used `'` which is full absolute path, but it is not sending the image by using **mail()** function. So what should I do now ? – Abhishek Kamal Feb 19 '20 at 17:20
  • @AbhishekKamal , "Absolute path" means the path at which the image is accessible on the internet, not on your local file system. – Ananth Feb 20 '20 at 12:58
  • ohhh.. mail() function can only send images if images are only on the internet not on local-computer . I got it ! – Abhishek Kamal Feb 21 '20 at 13:53
1

You can either link to a fully qualified URL of the image, e.g. http://yourserver.com/images/image.png

Or, you can embed the image in your email using MIME. See this response: PHP Attaching an image to an email

You may choose whichever you want, still it is not guaranteed that the image will be displayed by the mail client - in most cases, the client decides whether it will or will not show the images.

Community
  • 1
  • 1
naivists
  • 32,681
  • 5
  • 61
  • 85
  • Another link for the MIME option: http://stackoverflow.com/questions/17368770/php-send-an-email-with-image-attachment – cmbuckley Nov 11 '14 at 09:52