kev wood Posted April 30, 2008 Share Posted April 30, 2008 i am trying to add an image to an email. the image it being taken from the server and it is not being displayed within the email. if someone could please show me i am going wrong. the code i have used is this: $image = false; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = thumb_image1.'.'.$exts; // check for each extension foreach($exts as $ext) { if (file_exists($image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } // check if we have an image if ($image) { // ok we have the image $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; } else { // error, we can't find the image. } the code i have used to display the image in the body of the email looks lie this: <img src=".$web_image_folder.'/'.$image." /> all this code is on the same page so if the code is correct it should display the image in the email. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/ Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 your file_exists() function is probably not finding the image since it needs a path (i.e. $web_image_folder) foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } of course you need to define the path first Also, since $image ends up containing a string, it's probably a better idea to define the 'default' as: $image = ""; and then // check if we have an image if ($image != "") { // ok we have the image $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; } else { // error, we can't find the image. } ... although this bit of code has now become redundant Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530188 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 thanks for the reply i have now changed my code and it gives the same result which is no pictures are displayed. my code now looks like this below: $broad_image1 = ' '; $image = false; $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1.'.'.$exts'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } // check if we have an image if ($image != " ") { // ok we have the image } else { // error, we can't find the image. } the image that has needs to be displayed in the email gets uploaded first and is saved on the server in the folder image/thumbs/ and it is given a name on upload which is thumb_image1. i have checked on the server and the file does exist, i might be clutching at straws but could it be the way i am trying to display the image? Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530195 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 Nope, it's still the way you're checking This should work: <?php $image = ""; $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1.'.'.$exts'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } // check if we have an image if ($image != "") { // ok we have the image echo '<img src="' . $web_image_folder.'/'.$image . '" />'; } else { // error, we can't find the image. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530200 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 this has not worked either. ??? the files are on the server i have checked that. could it have something to do with the array for the extension of the file. this is the only other thing i can think it could be. the path to the image folder where they are held is correct, the are stored on the server with the file name thumbs_image1 then the extension follows. the code tells the computer to go to the correct place and look for the correct file name but it returns nothing. also i dont need the image echoed inside the php code as it will be displayed further down the page so i have changed the echo to variable name and it now looks like this: $broad_img1='<img src="' . $web_image_folder.'/'.$image . '" />'; this should not make a difference to the code working, and i tested the code before i made the change and it still was not displayed, although the echo statement was not part of the body of the email so this could have been the reason for it not displaying the first time. does the img tag need to be written any differently since it is being placed into a variable. this variable will then be placed in the body of the email and used to display the image as part of the email sent out. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530214 Share on other sites More sharing options...
Dragen Posted April 30, 2008 Share Posted April 30, 2008 You could try this for debugging, I've added some echoes to output variables at each stage of setting the image: <?php $image = ""; $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1.'.'.$exts'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; echo $image . '<br />'; } } // check if we have an image if ($image != "") { // ok we have the image echo '<img src="' . $web_image_folder.'/'.$image . '" />'; echo 'image found'; } else { // error, we can't find the image. echo 'no image; } ?> You could also try setting dimensions for the image. It may not be displaying because you've got no width or height. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530220 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 hmm ... the array SEEMS okay, although i hate foreach...as loops, hehe. having said that ... i may have spotted a mistake in my code i submitted: $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1.'.'.$exts'; we're putting an array ($exts) as a part of the string ($image_name) !! ... maybe that's the problem?? Change $imagename to: $image_name = 'thumb_image1'; ... hopefully that helps, because you're never gonna find an image called "thumbs_image1.Array.jpg". I never spotted that in my posting, hehe. Ooops! To answer the img tag writing question ... no, you write as you want displayed at all times. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530221 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 thanks for the reply i will give that ago now and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530227 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 it still not working. ??? could it be how i am trying to use the code. what i meanis that the page that this code is being entered onto is the send email page which is sent from a button click. what i have built works like this: The user enters a title into a box and some text to go with it once they have done that they then upload an image which is stored on the server in the location mentioned earlier. the user is then able to go to a preview page where they can see what will be sent out. to get the images to display on this page i have used a mysql database storing the path to the image. this section all works fine. the next step if the user is happy with what they see they can click on the send button, which then sends out the email. the title and text are sent with the images that are hard coded in, but the images which i am tryin to get from the server are not sent. the page that is sent out i will put the code up here so you can see how the page works and see if you can spot any mistakes which would stop it from sending out the images from the server <?php $image = ""; $web_image_folder = 'http://.acmeart.co.uk/mercury/image/thumbs'; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } // check if we have an image if ($image != "") { // ok we have the image $broad_img1='<img src="' . $web_image_folder."/".$image . '" />'; } else { // error, we can't find the image. } // construct mailing list array $merc_list = explode(",",$merc_mailingList); // deploy the emails for($i=0; $i<count($merc_list); $i++){ $message = "<style type=\"text/css\">body { margin: 0; background: ; background-image: url(); background-repeat: no-repeat; } .mainTxt {font-size: 11px; color:#444444; font-family: Arial, Helvetica, sans;} .whiteTxt {font-size: 9px; color:#FFFFFF; font-family: Arial, Helvetica, sans;} a:link {color:#EB7324} ul {font-size: 9px; color:#EB7324;padding-left: 5px;} .style3 {font-family: Geneva, Arial, Helvetica, sans-serif; color: #FFFFFF; font-weight: bold; } .style1 {font-family: Arial, Helvetica, sans-serif; font-size: 10px;color: #666666;} </style>"; $message .= "<body leftmargin=\"0\" topmargin=\"0\"> <table width=\"900\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"5\"> <tr> <td height=\"95\" colspan=\"2\" scope=\"col\"><img src=http://acmeart.co.uk/mercury/layout/BroadsheetSample/broadhead.jpg width=\"900\" height=\"150\"/></td> </tr> <tr> <td height=\"25\" colspan=\"2\" scope=\"col\"><span class=\"mainTxt\"><strong>$broad_topictitle1</strong></span></td> </tr> <tr> <th width=\"23%\" height=\"200\" scope=\"col\"><div align=\"left\">$broad_img1 <p> </p></div></th> <td width=\"77%\" scope=\"col\"><table width=\"99%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\"> <tr> <td align=\"left\" valign=\"top\"><span class=\"mainTxt\">$broad_messagebody1</span></td> <!-- End Image 1 --> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td height=\"28\" colspan=\"2\" scope=\"col\"><span class=\"mainTxt\"><strong>$broad_topictitle2</strong></span></td> </tr> <tr> <th height=\"95\" scope=\"col\"><div align=\"left\"><img src=".$web_image_folder2.'/'.$image2." /><p> </p></div></th> <td scope=\"col\"><table width=\"99%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\"> <tr> <td align=\"left\" valign=\"top\"><span class=\"mainTxt\">$broad_messagebody2</span></td> <!-- End Image 1 --> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td colspan=\"2\" scope=\"col\"><img src=\"http://acmeart.co.uk/mercury/layout/BroadsheetSample/broadfoot.jpg\" width=\"900\" height=\"150\" /></td> </tr> <tr> <td height=\"13\" colspan=\"2\" scope=\"col\"><div align=\"justify\"><span class=\"style1\">Linacre Voice is a monthly bulletin from LinacreOne Community Partnership 140-142 Linacre Road Litherland L21 8JU Tel: (0151) 922 4898 Open Monday - Thursday 10:00am - 4:00pm <font size=\"1\"></font></span> </div></td> </tr> </table> </table> </body>"; // Contacts $replyName = "$merc_replyId"; $replyEmail = "$merc_replyAddress"; $contactname = ""; $contactemail = "$merc_list[$i]"; // Subject $subject = "$merc_messageTitle"; // Headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: ".$replyName." <".$replyEmail.">\r\n"; $headers .= "To: ".$contactname." <".$contactemail.">\r\n"; mail($contactemail, $subject, $message, $headers); } // END for ?> Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530243 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 sorry about the long amount of code. ignore the image2 part i was trying to do it both images displaying before, but why run before you can walk. i think i will try to get one working properly first Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530247 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 Hmm ... okay i may be blind but it all looks okay, except for the second line: $web_image_folder = 'http://.acmeart.co.uk/mercury/image/thumbs'; <--- you're missing the www could that be it?? hehe Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530264 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 i have tried with and without it and it makes no difference. i have been stuck on this now for a while it just does not like me i think. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530267 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 Hmm ... well i know it wouldn't like the missing www (well, more accurately it won't know how to properly interpret the single . - but i stand to be corrected!) Give me some time with it, i'll pore over the code with a fine-toothed comb. Trust me, seemingly insurmountable problems are always the result of a missing bloody comma Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530272 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 just tried again still no joy. this must be starting to annoy you now. everything i have tried it just laughs at me i feel like throwing the computer at the wall sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530280 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 Okay dude, i've got it working on my side I made several (minor) changes: just remove the 'my stuff' with whatever hehehe <?php // ==== MY STUFF ==== $merc_messageTitle = "this is a message!"; $merc_mailingList = "david@digiworks.co.za"; // ================== $image = ""; $web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs'; $exts = array('jpg', 'png', 'gif', 'jpeg'); $image_name = 'thumb_image1'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } // check if we have an image if ($image != "") { // ok we have the image $broad_img1='<img src="' . $web_image_folder."/".$image . '" />'; } else { // error, we can't find the image. } // construct mailing list array $merc_list = explode(",",$merc_mailingList); // deploy the emails for($i=0; $i<count($merc_list); $i++){ $message = '<style type="text/css">body { margin: 0; background: ; background-image: url(); background-repeat: no-repeat; } .mainTxt {font-size: 11px; color:#444444; font-family: Arial, Helvetica, sans;} .whiteTxt {font-size: 9px; color:#FFFFFF; font-family: Arial, Helvetica, sans;} a:link {color:#EB7324} ul {font-size: 9px; color:#EB7324;padding-left: 5px;} .style3 {font-family: Geneva, Arial, Helvetica, sans-serif; color: #FFFFFF; font-weight: bold; } .style1 {font-family: Arial, Helvetica, sans-serif; font-size: 10px;color: #666666;} </style>'; $message .= '<body leftmargin="0" topmargin="0"> <table width="900" border="0" align="center" cellpadding="0" cellspacing="5"> <tr> <td height="95" colspan="2" scope="col"><img src=http://www.acmeart.co.uk/mercury/layout/BroadsheetSample/broadhead.jpg width="900" height="150"/></td> </tr> <tr> <td height="25" colspan="2" scope="col"><span class="mainTxt"><strong>' . $broad_topictitle1 . '</strong></span></td> </tr> <tr> <th width="23%" height="200" scope="col"><div align="left">' . $broad_img1 . ' <p> </p></div></th> <td width="77%" scope="col"><table width="99%" border="0" cellpadding="10" cellspacing="0"> <tr> <td align="left" valign="top"><span class="mainTxt">' . $broad_messagebody1 . '</span></td> <!-- End Image 1 --> </tr> </table> <p>Test Email hahaha</p> <p> </p> <p>I\'m sooooo clever! </p> <p> </p></td> </tr> <tr> <td colspan="2" scope="col"><img src="http://www.acmeart.co.uk/mercury/layout/BroadsheetSample/broadfoot.jpg" width="900" height="150" /></td> </tr> <tr> <td height="13" colspan="2" scope="col"><div align="justify"><span class="style1">Linacre Voice is a monthly bulletin from LinacreOne Community Partnership 140-142 Linacre Road Litherland L21 8JU Tel: (0151) 922 4898 Open Monday - Thursday 10:00am - 4:00pm <font size="1"></font></span> </div></td> </tr> </table> </table> </body>'; // Contacts //$replyName = $merc_replyId; //$replyEmail = $merc_replyAddress; $replyName = ""; $replyEmail = $merc_list[$i]; $contactname = ""; $contactemail = $merc_list[$i]; // Subject $subject = $merc_messageTitle; // Headers $headers = "From: ".$replyName." <".$replyEmail.">" . PHP_EOL; $headers .= "To: ".$contactname." <".$contactemail.">" . PHP_EOL; $headers .= "MIME-Version: 1.0" . PHP_EOL; $headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL; if (mail($contactemail, $subject, $message, $headers)) { echo "Success!! Yay!!"; } else { echo "Boooooo it failed!"; } } // END for ?> You'll notice the PHP_EOL for platform-independant newlines (yay!) and i've changed the way your message is constructed ... no more escaping " looks neater yes? Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530306 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 yes it does look neater. did the images come through on your email from the server because they are not on mine. i am using outlook express to view the email could it have something to do with that? i doubt it though. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530382 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 Heya, Yeah the images did come through. I use a program called PostCast Server ... google it and download it, it's free ... you can then use your 'localhost' as an SMTP server, but you can preview the messages before you actually 'send' them. I did that on my side and it found the images. outlook express might be having a fit about it, but try sending your messages to a gmail or yahoo account ... see what that does? if all else fails, check my last posting under 'MY STUFF' for my email address and email me ... i'll certainly let you know! hehe Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530393 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 i will add your address to the mailing list and se what you get sent. remind me to take it off while i am testing it tho other wise i will end up filling your inbox with the same message over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530397 Share on other sites More sharing options...
kev wood Posted April 30, 2008 Author Share Posted April 30, 2008 i have got to leave this for now but let me know what came through on your email it may just be outlook express being stupid. i will set more email accounts up tomorrow and go on from there. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530407 Share on other sites More sharing options...
theinfamousmielie Posted April 30, 2008 Share Posted April 30, 2008 okay, received your mails no image, you're right. Damn ... ??? okay let me look some more, i'll see what i can do and get back to you Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-530417 Share on other sites More sharing options...
kev wood Posted May 2, 2008 Author Share Posted May 2, 2008 sorry it took so long for me to get back bin the hospital broke my metatarsal playing football. have you had any joy with that code it is annoying aint it. would should work is not. Quote Link to comment https://forums.phpfreaks.com/topic/103540-if-file-exists/#findComment-531718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.