-2

I am trying to implement a code to embed random image in php mail body and images are in a directory. I have tried with html tag.But it is not working because images are not in http location. Images are in server location, not in client location.Please help me to resolve this issue.

Thanks in Advance

adarsh hota
  • 327
  • 11
  • 23

2 Answers2

0

you can simply use the glob function which searches for all the pathnames matching pattern, which get all files from directory and then using rand function you can simply select any random image out of it.

function random_pic($dir = 'images')
{
$files = glob($dir . '/*.jpg');
$file = array_rand($files);
return $files[$file];
}
Pritam Jinal
  • 121
  • 9
  • Thanks, But my question is how to send that random pic in mail body.Because When we will send the mail to client machine then that images are not available in his/her directory. So Image will not show in mail body. – Garima Bareja Feb 26 '15 at 04:48
0

simply save your all images on server in a publicly accessible folder, let say on the same directory where your index.php lies, then it can be displayed in mail body.

Pritam Jinal
  • 121
  • 9
  • Need one more help.. I am sending random pic in mail body as attachment. Now I want to send text also in mail body. As I am writing something in message. Mail is not triggered.. – Garima Bareja Feb 26 '15 at 09:35
  • I gave Content-Type = "image/jpg" in message. If I give Content-Type = "text/html" then Image is not dispplay in Mail Body. – Garima Bareja Feb 26 '15 at 09:37
  • have u tried content-type="text/html" and then use tag to display your picture... – Pritam Jinal Feb 26 '15 at 10:42
  • I have tried this. Image is not display in mail body. And If I attach pic then mail body is not showing. :( – Garima Bareja Feb 26 '15 at 11:19
  • $Body = "Dear ". $EmpName . ", On behalf of all at Interra Systems, wishing you a great birthday.We hope you have a great year and accomplish all the fabulous goals you have set. May you get the best of everything in life!!
    Cheers!
    Webmaster "; $Body = $Body .''; echo $Body; $headers = ""; $headers .= "From:$from\r\n"; $headers .= "Content-type: text/html\r\n";
    – Garima Bareja Feb 27 '15 at 05:18