Jump to content

php form, send email only if all conditions met


azukah

Recommended Posts

hi-

I have this form that works fine: if an image is not the right format or it's too big, it tells the user the error msg and redirects them back to the form BUT it still sends the email... how do i go about sending the email ONLY if the image is the right format & it's within the size limit..???

 

<?php
define ("MAX_SIZE","100"); 
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}
$errors=0;

if(isset($_POST['Submit'])) 
{
	$image=$_FILES['image']['name'];
	if ($image) 
	{
		$filename = stripslashes($_FILES['image']['name']);
  		$extension = getExtension($filename);
		$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
			$error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
		}
		else
		{
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
$error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
$errors=1;
}

$image_name=time().'.'.$extension;
$newname="images_board/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
$error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors) 
{
	$error = "<p>Thank You!</p><p>Your request has been submitted</p>";
}
//for email... ///
$studentName=$_POST['studentName'];
$studentId=$_POST['studentId'];
$studentEmail=$_POST['studentEmail'];
$studentPhone=$_POST['studentPhone'];
$postSubject=$_POST['postSubject'];
$postDescription=$_POST['postDescription'];

$to='email@email.com';
$subject="Bulletin Board Request: $postSubject"; 
$mainbody.="Student Name: $studentName<br/><br/>";
$mainbody.="Student Id: $studentId<br><br/>";
$mainbody.="Contact email: $studentEmail<br/><br/>";
$mainbody.="Contact Phone: $studentPhone<br/><br/>";
$mainbody.="Subject: $postSubject<br/><br/>";
$mainbody.="Description: $postDescription<br/><br/>";
$mainbody.="Image: $newname<br/>";

$headers="MIME-Version: 1.0\r\n";
$headers.="Content-type:text/html;charset=utf-8\r\n"; 
$headers.="From: $studentName";

mail($to,$subject,$mainbody,$headers);
?>

Link to comment
Share on other sites

i think i solved it .... i submitted a couple of times with big images and i haven't received any emails..

 

[code=php:0]
<?php
define ("MAX_SIZE","100"); 
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}
$errors=0;

if(isset($_POST['Submit'])) 
{
    $image=$_FILES['image']['name'];
    if ($image) 
    {
       $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
       $extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
       {
          $errors=1; // added here
          $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
       }
       else
       {
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
   $errors=1; // moved it here
   $error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
}

$image_name=time().'.'.$extension;
$newname="images_board/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
   $errors=1; // moved it here
   $error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
}}}}
if(isset($_POST['Submit']) && !$errors) 
{
    $error = "<p>Thank You!</p><p>Your request has been submitted</p>";
}
//for email... ///
$studentName=$_POST['studentName'];
$studentId=$_POST['studentId'];
$studentEmail=$_POST['studentEmail'];
$studentPhone=$_POST['studentPhone'];
$postSubject=$_POST['postSubject'];
$postDescription=$_POST['postDescription'];

$to='email@email.com';
$subject="Bulletin Board Request: $postSubject"; 
$mainbody.="Student Name: $studentName<br/><br/>";
$mainbody.="Student Id: $studentId<br><br/>";
$mainbody.="Contact email: $studentEmail<br/><br/>";
$mainbody.="Contact Phone: $studentPhone<br/><br/>";
$mainbody.="Subject: $postSubject<br/><br/>";
$mainbody.="Description: $postDescription<br/><br/>";
$mainbody.="Image: $newname<br/>";

$headers="MIME-Version: 1.0\r\n";
$headers.="Content-type:text/html;charset=utf-8\r\n"; 
$headers.="From: $studentName";

mail($to,$subject,$mainbody,$headers);
?>

[/code]

Link to comment
Share on other sites

You don't need the $errors anymore, this should do it:

 

<?php
define ("MAX_SIZE","100"); 
function getExtension($str) 
{
$i = strrpos($str,".");
    if (!$i) 
    { 
return ""; 
     }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if(isset($_POST['Submit'])) 
{
   $image=$_FILES['image']['name'];

    if($image) 
    {
       	$filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
       	$extension = strtolower($extension);

	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
       	{
                $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
       	}
       	else
       	{
		$size=filesize($_FILES['image']['tmp_name']);

	if ($size > MAX_SIZE*1024)
	{
   			$error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
	}
	else
	{
		$image_name=time().'.'.$extension;
		$newname="images_board/".$image_name;
		$copied = copy($_FILES['image']['tmp_name'], $newname);

		if (!$copied) 
		{
   				$error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
		}
		else
		{
			$error = "<p>Thank You!</p><p>Your request has been submitted</p>";
			//for email... ///
			$studentName=$_POST['studentName'];
			$studentId=$_POST['studentId'];
			$studentEmail=$_POST['studentEmail'];
			$studentPhone=$_POST['studentPhone'];
			$postSubject=$_POST['postSubject'];
			$postDescription=$_POST['postDescription'];

			$to='email@email.com';
			$subject="Bulletin Board Request: $postSubject"; 
			$mainbody.="Student Name: $studentName<br/><br/>";
			$mainbody.="Student Id: $studentId<br><br/>";
			$mainbody.="Contact email: $studentEmail<br/><br/>";
			$mainbody.="Contact Phone: $studentPhone<br/><br/>";
			$mainbody.="Subject: $postSubject<br/><br/>";
			$mainbody.="Description: $postDescription<br/><br/>";
			$mainbody.="Image: $newname<br/>";

			$headers="MIME-Version: 1.0\r\n";
			$headers.="Content-type:text/html;charset=utf-8\r\n"; 
			$headers.="From: $studentName";

			mail($to,$subject,$mainbody,$headers);
		}
	}
}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.