PDA

View Full Version : Getting form to send image


pauldf
03-14-2010, 04:40 AM
How's things guys

I have a basic .php form to handle people sending me their contact details etc. but what i was wondering how would i go about getting that form to let them choose a photo/image from their hard drive and sending that to me as well?
I have tried to do it already, the form arrives but no photo. I'm fairly new to .php forms so if anyone can help me without melting my brain :grin: i would be really thankful. Below is the form script i have so far:

<?php

/* Subject and Email Variables */


$emailSubject = 'Lost Pet Form';
$webMaster = 'ashrescue@gmail.com';


/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$contactField = $_POST['contact'];
$detailsField = $_POST['details'];
$petnameField = $_POST['petname'];
$imageField = $_POST['image'];


$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Contact: $contactField <br>
Email: $emailField <br>
Pet Details: $detailsField <br>
Pet Name: $petnameField <br>
Photo: $imageField <br>
EOD;


$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);


/* Results rendered as HTML */

Again i would be really thankful if anyone can help me with this, even if you don't have a solution if you could point me in the direction where i could learn how to do it that would help as well.

Thanks

pauldf
03-14-2010, 04:33 PM
How's things guys

I looked more into and it seems that i can't send photos to my email ( is that true?) but i can send them to a folder on my server ( if i can get it to work ).
I made some changes to the code and i have put in the full code below so o would be really thankful if someone could look over it and see where i'm messing up. I test the php file and it just keeps coming back with this error message when i press submit:
Parse error: syntax error, unexpected $end in /content/HostingPlus/a/s/ashanimalrescue.ie/web/lostpetform.php on line 282

well anyway here is the full code below:

<?php

/* Subject and Email Variables */


$emailSubject = 'Lost Pet Form';
$webMaster = 'ashrescue@gmail.com';


/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$contactField = $_POST['contact'];
$detailsField = $_POST['details'];
$petnameField = $_POST['petname'];
$imageField = $_POST['image'];

$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["lostpetphotos"];
$error = $_FILES["image"]["error"];
{
if ($error > 0)
{
die("Error uploading File!");
}
{
if ($type == "image/jpeg" || $size > 20000000)
{
die("Error - image size eceeds 2mb");
}
{
if ($type == "video/avi")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "video/mp4")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/doc")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/pdf")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/docx")
{
die("Error - Please upload jpeg only");
}
else
{
move_upload_file($temp,"lostpetphotos/".$name);
}





$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Contact: $contactField <br>
Email: $emailField <br>
Pet Details: $detailsField <br>
Pet Name: $petnameField <br>
Photo: $imageField <br>
EOD;


$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);


/* Results rendered as HTML */


$theResults = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/index.dwt" codeOutsideHTMLIsLocked="false" -->
<!-- DW6 -->
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<title>Lodging - Home Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="mm_lodging1.css" type="text/css" />
<style type="text/css">
<!--
body {
background-color: #FFFFFF;
}
.style2 {color: #000000}
.style3 {color: #FF0000}
-->
</style>
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
var i,img,nbArr,args=MM_nbGroup.arguments;
if (event == "init" && args.length > 2) {
if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
nbArr[nbArr.length] = img;
for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = args[i+1];
nbArr[nbArr.length] = img;
} }
} else if (event == "over") {
document.MM_nbOver = nbArr = new Array();
for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
nbArr[nbArr.length] = img;
}
} else if (event == "out" ) {
for (i=0; i < document.MM_nbOver.length; i++) {
img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
} else if (event == "down") {
nbArr = document[grpName];
if (nbArr)
for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
document[grpName] = nbArr = new Array();
for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
nbArr[nbArr.length] = img;
} }
}
//-->
</script>
</head>
<body onLoad="MM_preloadImages('images/home1.jpg','images/aboutash1.jpg','images/news.jpg','images/sponsor1.jpg','images/bassethounds1.jpg','images/ezine1.jpg','images/help1.jpg','images/fundraising1.jpg','images/contactash1.jpg','images/beforeadopt1.jpg','images/rehomingpolicy1.jpg')">
<table width="96%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="17" nowrap="nowrap"><img src="mm_spacer.gif" alt="" width="15" height="1" border="0" /></td>
<td height="60" colspan="3" class="logo" nowrap="nowrap"><div align="left"><img src="images/LogoSmall.jpg" width="190" height="200" /><img src="images/mainlogo.jpg" width="865" height="200" /></div></td>
<td width="153"><div align="right"><img src="images/directions.jpg" width="150" height="150" border="1" usemap="#Map8" /></div></td>
<td width="6">&nbsp;</td>
</tr>

<tr bgcolor="#ffffff">
<td colspan="6"><img src="mm_spacer.gif" alt="" width="1" height="1" border="0" /></td>
</tr>

<tr bgcolor="#a4c2c2">
<td width="17" nowrap="nowrap" bgcolor="#FFFFFF">&nbsp;</td>
<td height="36" colspan="3" bgcolor="#FFFFFF" class="navText" id="navigation"><div align="left"></div></td>
<td width="153" bgcolor="#FFFFFF"><div align="right"></div></td>
<td width="6" bgcolor="#FFFFFF">&nbsp;</td>
</tr>

<tr bgcolor="#ffffff">
<td colspan="6"><img src="mm_spacer.gif" alt="" width="1" height="1" border="0" /></td>
</tr>

<tr bgcolor="#ffffff">
<td colspan="2" valign="top" bgcolor="#FFFFFF"><table border="0" cellspacing="0" cellpadding="0" width="230">
<tr>
<td width="15" bgcolor="#FFFFFF">&nbsp;</td>
<td width="166" bgcolor="#FFFFFF" class="sidebarText" id="padding"><table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href="index.html" target="_top" onClick="MM_nbGroup('down','group1','home','images/home1.jpg',1)" onMouseOver="MM_nbGroup('over','home','images/home1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/home.jpg" alt="" name="home" width="150" height="50" border="0" id="home" onload="" /></a></td>
</tr>
<tr>
<td><a href="aboutus.html" target="_top" onClick="MM_nbGroup('down','group1','about','images/aboutash1.jpg',1)" onMouseOver="MM_nbGroup('over','about','images/aboutash1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/aboutash.jpg" alt="" name="about" width="150" height="50" border="0" id="about" onload="" /></a></td>
</tr>
<tr>
<td><a href="javascript:;" target="_top" onClick="MM_nbGroup('down','group1','news','images/news.jpg',1)" onMouseOver="MM_nbGroup('over','news','images/news.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/news1.jpg" alt="" name="news" width="150" height="50" border="0" id="news" onload="" /></a></td>
</tr>
<tr>
<td><a href="sponsor.html" target="_top" onClick="MM_nbGroup('down','group1','sponsor','images/sponsor1.jpg',1)" onMouseOver="MM_nbGroup('over','sponsor','images/sponsor1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/sponsor.jpg" alt="" name="sponsor" width="150" height="50" border="0" id="sponsor" onload="" /></a></td>
</tr>
<tr>
<td><a href="beforeyouadopt.html" target="_top" onClick="MM_nbGroup('down','group1','beforeyouadopt','image s/beforeadopt1.jpg',1)" onMouseOver="MM_nbGroup('over','beforeyouadopt','images/beforeadopt1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img name="beforeyouadopt" src="images/beforeadopt.jpg" border="0" alt="" onLoad="" /></a></td>
</tr>
<tr>
<td><a href="rehomingpolicy.html" target="_top" onClick="MM_nbGroup('down','group1','rehomingpolicy','image s/rehomingpolicy1.jpg',1)" onMouseOver="MM_nbGroup('over','rehomingpolicy','images/rehomingpolicy1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img name="rehomingpolicy" src="images/rehomingpolicy.jpg" border="0" alt="" onLoad="" /></a></td>
</tr>
<tr>
<td><a href="bassethounds.html" target="_top" onClick="MM_nbGroup('down','group1','basset','images/bassethounds1.jpg',1)" onMouseOver="MM_nbGroup('over','basset','images/bassethounds1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/bassethounds.jpg" alt="" name="basset" width="150" height="50" border="0" id="basset" onload="" /></a></td>
</tr>
<tr>
<td><a href="ezine.html" target="_top" onClick="MM_nbGroup('down','group1','ezine','images/ezine1.jpg',1)" onMouseOver="MM_nbGroup('over','ezine','images/ezine1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/ezine.jpg" alt="" name="ezine" width="150" height="50" border="0" id="ezine" onload="" /></a></td>
</tr>
<tr>
<td><a href="javascript:;" target="_top" onClick="MM_nbGroup('down','group1','help','images/help1.jpg',1)" onMouseOver="MM_nbGroup('over','help','images/help1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/help.jpg" alt="" name="help" width="150" height="50" border="0" id="help" onload="" /></a></td>
</tr>
<tr>
<td><a href="javascript:;" target="_top" onClick="MM_nbGroup('down','group1','fundraising','images/fundraising1.jpg',1)" onMouseOver="MM_nbGroup('over','fundraising','images/fundraising1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/fundraising.jpg" alt="" name="fundraising" width="150" height="50" border="0" id="fundraising" onload="" /></a></td>
</tr>
<tr>
<td><a href="contact.html" target="_top" onClick="MM_nbGroup('down','group1','contact','images/contactash1.jpg',1)" onMouseOver="MM_nbGroup('over','contact','images/contactash1.jpg','',1)" onMouseOut="MM_nbGroup('out')"><img src="images/contactash.jpg" alt="" name="contact" width="150" height="50" border="0" id="contact" onload="" /></a></td>
</tr>
</table> </td>
<td width="49" bgcolor="#FFFFFF">&nbsp;</td>
</tr>
</table>
<span class="navText"><img src="images/Twitter.png" width="40" height="40" border="0" usemap="#Map" /><img src="images/Facebook.png" width="40" height="40" border="0" usemap="#Map2" /><img src="images/Bebo.png" width="40" height="40" border="0" usemap="#Map3" /><img src="images/YouTube.png" width="40" height="40" border="0" usemap="#Map4" /><img src="images/Myspace.png" width="40" height="40" border="0" usemap="#Map5" /></span></td>
<td width="4" valign="top">&nbsp;</td>
<td width="846" valign="top"><br />
<br />
<table width="658" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="658" class="pageName"><p>&nbsp;</p></td>
</tr>

<tr>
<td class="bodyText"><!-- InstanceBeginEditable name="EditRegion1" --><span class="style2" style="font-size: 18px; font-weight: bold; font-style: italic">Thank you for getting in touch with ASH. We will do our best to answer your email as soon as we can.</span><!-- InstanceEndEditable --><br /> </td>
</tr>
</table> </td>
<td width="153"><p align="right"><img src="images/adopt.jpg" width="150" height="150" border="1" usemap="#Map7" /></p>
<p align="right"><img src="images/donate.jpg" width="150" height="150" border="1" /></p>
<p align="right"><img src="images/lost&amp;found.jpg" width="150" height="150" border="1" usemap="#Map6" /></p>
<p align="right"><img src="images/friendofASH.jpg" width="150" height="150" border="1" /></p>
<p align="right"><img src="images/storiesfromthepaw.jpg" width="150" height="150" border="1" /></p></td>
<td width="6"><p>&nbsp;</p>
<p>&nbsp;</p></td>
</tr>

<tr bgcolor="#ffffff">
<td colspan="6"><img src="mm_spacer.gif" alt="" width="1" height="1" border="0" /></td>
</tr>

<tr>
<td width="17">&nbsp;</td>
<td width="213">&nbsp;</td>
<td width="4">&nbsp;</td>
<td width="846"><div align="center"><img src="images/dogchoice.jpg" width="700" height="150" /></div></td>
<td width="153">&nbsp;</td>
<td width="6">&nbsp;</td>
</tr>
</table>

<map name="Map" id="Map"><area shape="rect" coords="5,3,35,36" href="http://twitter.com/ashanimalrescue" target="_blank" alt="twitter" />
</map>
<map name="Map2" id="Map2"><area shape="rect" coords="2,3,36,38" href="http://www.facebook.com/ashanimalrescue" target="_blank" alt="facebook" />
</map>
<map name="Map3" id="Map3"><area shape="rect" coords="3,5,37,38" href="http://www.bebo.com/ashanimalrescue" target="_blank" alt="bebo" />
</map>
<map name="Map4" id="Map4"><area shape="rect" coords="6,4,37,38" href="http://www.youtube.com/ashanimalrescue" target="_blank" alt="youtube" />
</map>
<map name="Map5" id="Map5"><area shape="rect" coords="5,3,34,38" href="http://www.myspace.com/ashanimalrescue" target="_blank" alt="myspace" />
</map>
<map name="Map6" id="Map6"><area shape="rect" coords="5,4,145,149" href="lost&amp;found.html" target="_self" />
</map>
<map name="Map7" id="Map7"><area shape="rect" coords="4,5,147,145" href="adopt.html" target="_self" />
</map>
<map name="Map8" id="Map8">
<area shape="rect" coords="5,3,144,147" href="directions.html" target="_self" />
</map></body>
<!-- InstanceEnd --></html>


EOD;
echo "$theResults";

?>

Cheers again guys.

tux
03-14-2010, 05:51 PM
Here you go, you will have to put your details where needed but this should send an email with an attachment.

<?php
//define the receiver of the email
$to = 'your email here';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: senders email here\r\nReply-To: reply to email here";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('path to your file here')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="your file name here"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Let me know if it works or if you need more help.

Regards Paul

pauldf
03-14-2010, 10:33 PM
Hi Tux

Thanks a million for posting, sorry now to wreck your brain but you know the script you wrote above do i add that to what i already have or do i just replace what i have:

<?php

/* Subject and Email Variables */


$emailSubject = 'Lost Pet Form';
$webMaster = 'ashrescue@gmail.com';


/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$contactField = $_POST['contact'];
$detailsField = $_POST['details'];
$petnameField = $_POST['petname'];
$imageField = $_POST['image'];

$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["lostpetphotos"];
$error = $_FILES["image"]["error"];
{
if ($error > 0)
{
die("Error uploading File!");
}
{
if ($type == "image/jpeg" || $size > 20000000)
{
die("Error - image size eceeds 2mb");
}
{
if ($type == "video/avi")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "video/mp4")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/doc")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/pdf")
{
die("Error - Please upload jpeg only");
}
{
if ($type == "document/docx")
{
die("Error - Please upload jpeg only");
}
else
{
move_upload_file($temp,"lostpetphotos/".$name);
}





$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Contact: $contactField <br>
Email: $emailField <br>
Pet Details: $detailsField <br>
Pet Name: $petnameField <br>
Photo: $imageField <br>
EOD;


$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);


With what you have sent me. If i just have to add it to what i have where about's should it go or if i have to replace all the code i have will i still be able to find out the peoples contact details etc. it's just i looked and i don't see them written into your script anywhere. Sorry for all the questions but simple contact forms i can handle but they are very simple it's when i start getting deeper into php that the brain starts to melt. Here is the link to the actual page that will be up on the site once i get everything sorted:

http://www.ashanimalrescue.ie/lostdetails.html

You will be able to see what i'm asking for off people. Oh and one more thing can i get their details and jpegs sent to my email address or will i have to create a folder on my server and have the jpegs sent there?

Once again thanks for taking the time to post its much appreciated.

Paul

tux
03-15-2010, 07:55 AM
The script will just attach the file that you specify. I thought you would be adding your own functionality to it.

You can have it send a jpg to your email with contact details but will have to create a TEMP folder I think. If you want the files saved to your server anyway you could do that then attach them to the email if you wanted.

Tell me it is you want to do and I'll try to sort something for you. May take a couple of days tho' as I'm a bit busy at work.

pauldf
03-15-2010, 10:21 AM
Thanks for posting back. Basically the form i'm looking to put up on my site is for people who lost a pet, they can email me their contact details: name, contact no. and email as well as their pets details: pets name and description
as well as giving them the option so upload a jpeg to send to me as well. That way once i receive all that information i can add it the website. The test form page is here ( http://www.ashanimalrescue.ie/lostdetails.html ) well when i say test, that is the actual form once i can get a php script to handle all the information. If i could just have everything they send, sent to my email address that would be cool but if the only way to get this to work is to create a "Temp" folder on my server that would be no problem.

Take your time and don't put yourself out if you can get something in a few days cool if not no worries. Now that i know exactly what that script you sent me is for i can mess around with it from here, and see can i build the rest around it.

Once again thanks for posting

All the best

Paul

tux
03-15-2010, 03:45 PM
If you are going to use the image on the site is there any need to have it emailed to you for you to upload it back to the site again?

Is your site dynamic or static, by dynamic I mean is it populated from a database using php or are you coding each page yourself?

Some other points...

Your design seems to be quite wide. When viewed at res 1024x768 it creates horizontal scroll bars. You may want to make it smaller.

When you post code, highlight it and choose the relevant icon, # <> or php and it will keep it tidy on the forum.

pauldf
03-15-2010, 04:49 PM
Hi

Basically i have a lost dog template made in photoshop ( it has a place for the images / text etc. ) where i can just drag and drop the jpeg into it as well as the other details they send so i taught having everything sent to me in one email might make it easier and faster for me to do the template, rather then have their text details come to me via email and then i would have to go onto the server to get the jpeg to bring it into photoshop.
It's a static site, the only involvement of php so far, is just to handle the form scripts.
Thanks for the heads up about the size i'll look into that oh and for letting me know about posting code here, i couldn't figure why when i put up the code it all just stayed black and yours was the different colours that its meant to be.

Thanks again

davidj
03-15-2010, 05:14 PM
@pauldf

Please can you edit your posts to include code tags

pauldf
03-15-2010, 05:32 PM
no worries , my bad :oops:

I can't seem to edit the older post's it won't let me hmmm

davidj
03-15-2010, 07:01 PM
Haway mods

sort these posts out with code tags

This never happened on my watch!

tux
03-18-2010, 08:30 PM
Sorry its been a while but here is the code I promised you. I notice you have removed the form from the link you gave so you will have to make sure you check that the variables match the form fields.

I have allowed error and success messages to be displayed back to the form page so you will have to save it as a PHP file. Also, make sure that the action of you form points to what ever you save the script as.

Okay here we go.

This code needs to be copied right at the top of your form page, above everything else.......

<?php
// this code needs to go at the very top of the page that contains your form
// and the page needs to be resaved as a php file
$error = $_GET['error'];
$success = $_GET['success'];

if($error == 0){
$error = "";
}elseif($error == 1){
$error = "All fields are required!!";
}elseif($error == 2){
$error = "Only .jpg files are accepted!!";
}elseif($error == 3){
$error = "Your file was too big!!";
}elseif($error == 4){
$error = "Something went wrong, your details were not sent!!";
}

if($success){
$success = "Thankyou, your details where sent!!";
}else $success = "";
/////////////////////////////////////////////////////////
?>

This code needs to be on the form page above the form and is to display the success or error messages.....

<?php echo $success; ?><?php echo $error; ?>

This is the script, I have commented out to explain what each bit does so you can try to understand it.....

<?php
///////////////////////////////////////////////declare variables collected from your form
$name = $_POST['name']; //make sure the $_POST keys match the fields in your form.
$petname = $_POST['petname'];
$subject = "Lost Pet Form";
$details = $_POST['details'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$image = $_FILES['image'];
$to_email = "ashrescue@gmail.com";
////////////////////////////////////////////////

// function to get file extension
function getExtension($str) {

$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if(!$name || !$petname || !$details || !$email || !$telephone || !$image){ //Check required fields are completed!
header("location:your_form_page.php?error=1");// if any fields is empty, redirect to error page edit this to your form page
exit;
}else{

define ("MAX_SIZE","5000");// define max file size, change to suit your own maximum file size (eg. 5000 = 5MB)

$uploadedfile = $_FILES['image']['tmp_name'];//get the file from form
// check for correct file extension
if ($uploadedfile)// check we have a file
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg"))// check if the file is not jpg or jpeg
{
$error = 2;
header("location:your_form_page.php?error=2");// if not jpg or jpeg redirect to error page, edit this to your form page
exit;
}
// check for correct file size
else
{
$size=filesize($_FILES['image']['tmp_name']);// get the file size

if ($size > MAX_SIZE*1024)
{
$error = 3;
header("location:your_form_page.php?error=3");// if too big redirect to error page, edit this to you form page
exit;
}
// check we have correct extension and create file
elseif($extension=="jpg" || $extension=="jpeg" )
{
//$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
//$file = $_FILES['image']['name'];//declare temp file
imagejpeg($src,$filename,100);
}
}
}

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed.
$headers = "From: $email\r\nReply-To: $email";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the attachment file contents into a string and encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($uploa dedfile)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php

// plain text message
$message = "Lost pet details.\n\n" .
"Name:" . $name . "\n" .
"Email:" . $email . "\n" .
"Telephone:" . $telephone . "\n\n" .
"Pet Name:" . $petname . "\n" .
"Pet Details:" . $details;

echo $message; ?>


--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for HTML email-->

<h2>Lost pet details.</h2><br><br>
Name: <b><?php echo $name; ?></b><br>
Email: <b><?php echo $email; ?></b><br>
Telephone: <b><?php echo $telephone; ?></b><br><br>
Pet Name: <b><?php echo $petname; ?></b><br>
Pet Details: <b><?php echo $details; ?></b>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/jpg; name="<?php echo $_FILES['image']['name'] ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to_email, $subject, $message, $headers );
//if the message is sent successfully redirect to success page, edit this to your form page
if($mail_sent){
header("location:your_form_page.php?success=1");
exit;
}elseif(!$mail_sent){//if email not sent redirect to error page, edit this to your form page
header("location:your_form_page.php2?error=4");
exit;
}
?>

Not sure if this is the best way to do this and not sure if there is any security issues that need to be addressed. Any advice from others would be cool.

Let me know how you get on.

Point for the future:

If you get lots of takers for this you will find yourself spending hours, static coding this. You should think about learning to build your site dynamically. Its not as hard as you think.

davidj
03-18-2010, 10:36 PM
tip: use a switch instead of those elseifs

pauldf
03-18-2010, 11:31 PM
Thanks a million for that. I'll give it a try and see how things go, A major thanks for the explanations of what each part does as well. I reckon i might look into building dynamic sites in the near future i suppose it would open up a lot more to me if i knew how to do them. Once again though thanks for taking the time to post.

edbr
03-19-2010, 02:29 AM
I reckon i might look into building dynamic sites in the near future i suppose it would open up a lot more to me if i knew how to do them.
IMO you could do a lot worse than checking out the tutorialss davidj has made on this forum, or even taking the course he offers in his signature. He has been responsible for setting many of us on the path to php awareness

tux
03-19-2010, 07:41 AM
IMO you could do a lot worse than checking out the tutorialss davidj has made on this forum, or even taking the course he offers in his signature. He has been responsible for setting many of us on the path to php awareness

I totally agree Edbr. Paul Dj's PHP beginners guide tutorial is here (http://www.dreamweaverclub.com/vtm/php-mysql-apache.php), take a look and seriously consider checking out his offer in his signature.

tip: use a switch instead of those elseifs

Thanks for the tip Dj, I'll have a play with switches.

tux
03-19-2010, 07:55 AM
tip: use a switch instead of those elseifs

Dj, I've had a quick go at switches. Is this then the same as what I've done with the elseif's......

<?php

$success = $_GET['success'];

switch($_GET['error'])
{
case "0":{ $error = ""; break; }

case "1":{ $error = "All fields are required!!"; break; }

case "2":{ $error = "Only .jpg files are accepted!!"; break; }

case "3":{ $error = "Your file was too big!!"; break; }

case "4":{ $error = "Something went wrong, your details were not sent!!"; break; }
}

if($success){
$success = "Thankyou, your details where sent!!";
}else $success = "";

?>

davidj
03-19-2010, 08:48 AM
yes

dont need the braces though

davidj
03-19-2010, 08:49 AM
thanks for the plug BTW

tux
03-19-2010, 07:01 PM
Paul, after taking Dj's advice where I told you to put this....

<?php
// this code needs to go at the very top of the page that contains your form
// and the page needs to be resaved as a php file
$error = $_GET['error'];
$success = $_GET['success'];

if($error == 0){
$error = "";
}elseif($error == 1){
$error = "All fields are required!!";
}elseif($error == 2){
$error = "Only .jpg files are accepted!!";
}elseif($error == 3){
$error = "Your file was too big!!";
}elseif($error == 4){
$error = "Something went wrong, your details were not sent!!";
}

if($success){
$success = "Thankyou, your details where sent!!";
}else $success = "";
/////////////////////////////////////////////////////////
?>

Change it to this.....

<?php
// this code needs to go at the very top of the page that contains your form
// and the page needs to be resaved as a php file
$success = $_GET['success'];

switch($_GET['error'])
{
case "0": $error = ""; break;

case "1": $error = "All fields are required!!"; break;

case "2": $error = "Only .jpg files are accepted!!"; break;

case "3": $error = "Your file was too big!!"; break;

case "4": $error = "Something went wrong, your details were not sent!!"; break;
}

if($success){
$success = "Thankyou, your details where sent!!";
}else $success = "";
/////////////////////////////////////////////////////////

?>

How are you getting on with it?

Regards, Paul

edbr
03-25-2010, 04:37 AM
no you have a limited time to edit. i have added tags for you