This topic is locked
[SOLVED]

  Attach images to email / show images in email

9/10/2013 2:58:43 PM
PHPRunner General questions
C
copper21 author

Hello,
I am testing the capability to send an email with either images from a database attached to an email OR the images being able to be placed in the email itself using the IMG tag. I looked at the Tips and Tricks posting on how to send attachments from file in database, but it does not seem to attach the image.
The other issue that I am having is the database resides on an internal network, not the internet. Is there a way to even do this when sending an email to lets say Google or Yahoo emails?
I successfully have sent an email with an image from the database in the email using imager.php; but I was reading the email from a computer that is attached to our local network. I then opened the email on a computer that was on a public wifi and could not see the image; I am guessing due to the internal firewall.
SQL SERVER 2008, Image fields stored as VARBINARY. I have 2 images fields from the line of data to send.
Thank you very much in advance for any help with this topic.

C
cgphp 9/10/2013

Post your code.

C
copper21 author 9/10/2013

$sql = "SELECT * FROM dbo.Table WHERE ID = 123456";

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);
$email= "me@test.com";
$msg = "";
$msg.="<html>
<head><title>TEST Notification</title></head>
<body> <tr> <td>
<h2>Test Notification</h2>
<p> Brian,


<font color= red>This is some sample text!</font>


<img height='50' width='100' src='http://webserver/web/imager.php?table=dbo_Table&field=IMAGEFIELD1&key1=".$data['ID']."'>;


<img height='300' width='400' src='http://webserver/web/imager.php?table=dbo_Table&field=IMAGEFIELD2&key1=".$data['ID']."'>;


<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=14&size=400x300&maptype=roadmap&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'>&nbsp;&nbsp;&nbsp;&nbsp;
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=17&size=400x300&maptype=satellite&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'></p>;
</td></tr></body></html>";
$subject="Test Notification";
$from="me@test.com";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from));
if(!$ret["mailed"])
{echo $ret["message"];}
Thanks Cristian!

C
cgphp 9/11/2013

If you point your browser to

http://webserver/web/imager.php?table=dbo_Table&field=IMAGEFIELD1&key1=123456


do you see the image? Make sure webserver/web/imager.php is public accessible.

C
copper21 author 9/11/2013



If you point your browser to

http://webserver/web/imager.php?table=dbo_Table&field=IMAGEFIELD1&key1=123456


do you see the image? Make sure webserver/web/imager.php is public accessible.


I just get a broken image icon when I put that URL into a web browser.

C
cgphp 9/11/2013

Post the code of the imager.php file.

C
copper21 author 9/11/2013



Post the code of the imager.php file.


Okay, for some reason now it works. When I send an email with the email script I gave, all is working great, I get the two images from the database into the email and the two maps that I have in there too. This was viewed from a computer that is on our local network.
Without making this project public, is there a way to get the images into the email? I would be okay with attachments. I will not have the ability to open this application up to the public. Does the tutorial http://www.asprunner.com/forums/topic/21238-sending-email-with-attachment-from-the-database/ work with images stored as VARBINARY? As mentioned before, I was unable to get it to work.
Thanks again for your help Cristian!

C
cgphp 9/11/2013

You can attach the images as described in the Sergey tutorial. Post the code you use to attach the images stored in the database.

C
copper21 author 9/11/2013



You can attach the images as described in the Sergey tutorial. Post the code you use to attach the images stored in the database.


This is used in a button right now. There are two images in the line of data, IMAGEFIELD1 & IMAGEFIELD2 that I would like to attach. As mentioned before, these fields are VARBINARY and do not store an image, but rather a very long HEXDECIMAL character string. I just took what Sergey has in Tips and Tricks and put it in the email that I added earlier; but I took out the imager.php parts so that I could just attach them. I did add a "from" field in the email array as Sergey does not have it in his.
$sql = "SELECT * FROM dbo.Table WHERE ID =". $keys['ID'];

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);
$email= "me@email.com";
$msg = "File Attached";
$subject="Test Notification";
$fileArray = my_json_decode($data["IMAGEFIELD1"]);
$attachments = array();
$msg = "";
foreach($fileArray as $f)
{

$attachments[] = array("path" => $f["name"]);

}
$msg.="<html>
<head><title>TEST Notification</title></head>
<body> <tr> <td>
<h2>Test Notification</h2>
<p> Brian,


<font color= red>This is a test message</font>


<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=14&size=400x300&maptype=roadmap&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'>&nbsp;&nbsp;&nbsp;&nbsp;
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=17&size=400x300&maptype=satellite&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'></p>;
</td></tr></body></html>";
$from="me@email.com";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from, "attachments" => $attachments));
if(!$ret["mailed"])
{echo $ret["message"];}

C
cgphp 9/11/2013

If you click the button, what happen? Do you receive the message?

C
copper21 author 9/11/2013

Yes, I do. All works except for the images from IMAGEFIELD1 and IMAGEFIELD2 are not attached.

C
cgphp 9/11/2013

If the file are saved as binary, you can try this solution:

$sql = "SELECT * FROM dbo.Table WHERE ID =". $keys['ID'];

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);
$email= "me@email.com";
$subject="Test Notification";
$attachments = array();
$image_1 = 'files/image_1.jpg';

$image_2 = 'files/image_2.jpg';
file_put_contents($image_1, $data["IMAGEFIELD1"]);

file_put_contents($image_2, $data["IMAGEFIELD2"]);
$attachments[] = array("path" => $image_1);

$attachments[] = array("path" => $image_2);
$msg = "<html>
<head><title>TEST Notification</title></head>
<body> <tr> <td>
<h2>Test Notification</h2>
<p> Brian,
<font color= red>This is a test message</font>
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=14&size=400x300&maptype=roadmap&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'>&nbsp;&nbsp;&nbsp;&nbsp;
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=17&size=400x300&maptype=satellite&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'></p>;
</td></tr></body></html>";
$from="me@email.com";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from, "attachments" => $attachments));
unlink($image_1);

unlink($image_2);
if(!$ret["mailed"])
{echo $ret["message"];}
C
copper21 author 9/11/2013

Cristian,
Sorry, but I no longer receive an email with this code. Do I need to create a folder neamed "files"? I read up on file_put_contents and it looks like it should have created one for me; so I figured I did not have to.
Brian

C
cgphp 9/11/2013

Yes, create a folder named files for example or what else you like. Put the folder at the same level as the other PHPrunner folders. Using firebug helps you to debug the server response more easily.

C
copper21 author 9/11/2013

Perfect Cristian!
I added the "files" folder. At first the email did not work, but then I gave IIS_IUSRS permission to write to that folder and it worked. Thank you very much; this is very cool.
On a side note I just have 2 more questions.

  1. If I want to save all of the images in the "files" folder that get emailed, could I add a timestamp to each of the images so that they don't get overwritten; giving them a fairly unique name?
  2. Is what I did with the IIS_IUSRS permissions safe? I right clicked just on the "files" folder and gave them write permissions.
    You are awesome!
    Brian

C
cgphp 9/11/2013

1. If I want to save all of the images in the "files" folder that get emailed, could I add a timestamp to each of the images so that they don't get overwritten; giving them a fairly unique name?


$sql = "SELECT * FROM dbo.Table WHERE ID =". $keys['ID'];

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);
$email= "me@email.com";
$subject="Test Notification";
$attachments = array();
$timestamp = date('Ymdhis');

$image_1 = 'files/'.$timestamp.'_1.jpg';

$image_2 = 'files/'.$timestamp.'_2.jpg';
file_put_contents($image_1, $data["IMAGEFIELD1"]);

file_put_contents($image_2, $data["IMAGEFIELD2"]);
$attachments[] = array("path" => $image_1);

$attachments[] = array("path" => $image_2);
$msg = "<html>
<head><title>TEST Notification</title></head>
<body> <tr> <td>
<h2>Test Notification</h2>
<p> Brian,
<font color= red>This is a test message</font>
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=14&size=400x300&maptype=roadmap&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'>;
<img src='http://maps.googleapis.com/maps/api/staticmap?center=".$data['Latitude'].",".$data['Longitude']."&zoom=17&size=400x300&maptype=satellite&markers=color:blue%7Clabel:A%7C".$data['Latitude'].",".$data['Longitude']."&sensor=false'></p>;
</td></tr></body></html>";
$from="me@email.com";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from, "attachments" => $attachments));
if(!$ret["mailed"])
{echo $ret["message"];}


2. Is what I did with the IIS_IUSRS permissions safe? I right clicked just on the "files" folder and gave them write permissions.


Put the folder outside the webroot so it is not public accessible. For example:

$timestamp = date('Ymdhis');

$image_1 = '../files/'.$timestamp.'_1.jpg';

$image_2 = '../files/'.$timestamp.'_2.jpg';
C
copper21 author 9/11/2013

Thanks again. I am using IIS. So creating a folder named "files" lets say at C:\inetpub\files and giving that folder write access to IIS_USRS is correct? I would then have my code like this?
$timestamp = date('Ymdhis');
$image_1 = 'C:/inetpub/files/'.$timestamp.'_1.jpg';

$image_2 = 'C:/inetpub/files/'.$timestamp.'_2.jpg';

C
cgphp 9/12/2013

Yes, your solution should work.

C
copper21 author 9/12/2013



Yes, your solution should work.


It did! Thanks for your help on this!