2

My code for sending email

<?php
$to = 'piyu.mulay@gmail.com';

$subject = 'Website Change Request';

$headers = "From: chintamani.mulay@gmail.com \r\n";
$headers .= "CC: chintamani.mulay@gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="../1.jpg">';
$message .= '</body></html>';

echo mail($to, $subject, $message, $headers);
?> 

When i put link in <img src="http://weber-steinach.de/files/339349"> it works well but when I put path of file which is in my localserver I won't show and Image in email.

Piyush
  • 3,947
  • 9
  • 36
  • 69
  • 1
    You can't use your localserver. The recipient won't have access to it. You could either host it externally (like you were doing) or attach it as a multi-part message. – jonny2k9 May 18 '15 at 05:22
  • @jonny2k9 Can you provide link of maultipart example – Piyush May 18 '15 at 05:27
  • 2
    http://stackoverflow.com/questions/922898/embedding-attached-images-in-html-emails – jonny2k9 May 18 '15 at 05:35

1 Answers1

5

Finally I come to know how to do this. Here is the source link from where I come to know

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: youremail@host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

<html>

<head>
<style> p {color:green} </style>
</head>
<body>

A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>

</html>'."\n\n".
$bound;

$file = file_get_contents("http://localhost/a/img/1.jpg");

$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;

echo mail($to, $subject, $message, $headers) ;

?>
Piyush
  • 3,947
  • 9
  • 36
  • 69
  • Great answer! Can you also explain on how to show more than just one image? Please :), I tried several ways already none of them worked. – phrogg Dec 12 '18 at 13:31
  • Ahh, I figured it out! I forgot to add the `$bound` between each image. And now it's working! – phrogg Dec 12 '18 at 15:35