Jump to content

Contact form not working?


supwd

Recommended Posts

So I'm new to php and i grabbed this script off google, I really like it since it's simple and it has the ability to send an attachment. The only problem is it isn't getting sent to me when I submit the form. Can anyone figure out why? It would help me out so much!

 

I filled in my email in the $emailto; variable, I can't figure out why it won't work :(.

 

<?php
//*****************************************SETTINGS START***************************************************************
//
//Set your email to address, this is the email address which you would all emails sent from this form to go to
$emailto    = 'superiorwebdesigns@gmail.com';
//
//Set the error messages you would like to be displayed in the case of an empty email address, an incorrectly
//formatted email address, an empty subject and an empty message body
$noname	  	   = 'You must fill in your name.';
$noemail	   = 'You must fill in an email address to send to.';
$wrongemail    = 'The email address your entered appears to be invalid.';
$blacklisted   = 'Your email address has been blacklisted.';	
$nosubject     = 'You must fill in a message subject.';
$nomessage     = 'You must enter a message.';
$waiting       = 'Please wait while your message is sent.';
$botcheckmessage = 'You incorrectly answered the anti-spam question.';
$possiblehackattempt = 'A possible hack attempt has been detected, please check your entered data and try again.';
$invalidfiletype = 'We do not accept this type of file.  We can only accept files of type: PDF, PHP, ZIP';
//
//Set the success message you would like to appear when a message has been correctly sent
$ok			  = 'Your message has been successfully sent.';
//
//The below is used for simple header injection attack prevention and includes common strings used in such attacks
$illegalchars = array(
"Content-Type:", 
"To:",
"Cc:", 
"Bcc:"
); 
//
//The below is a list of allowed filetypes, please see the filetypes.txt file packaged with this download for an 
//extensive list of filetypes
$filetypes = array(
"application/x-zip-compressed",		//.zip files
"application/pdf"					//.pdf files
);
//The below is a blacklist of email addresses, if you want to stop certain people from using this form, add their email
//addresses here
$blacklist = array(
"somebody@dontcomebackagain.com",
"somebodyelse@dontcomebackagain.com"
);
//*****************************************SETTINGS END*****************************************************************


//Check for a POST request from the form, if one is received do the below
if($_SERVER['REQUEST_METHOD'] == 'POST') {

//Set the $errors and $hackattempt variables to NULL
$hackattempt = "";
$errors = "";

   //Check all of the POSTed data for any illegal Text as defined above in the illegalchars[] array (above)
   foreach ($_POST as $key => $value){ 
      $$key = $value; 
      
      foreach($illegalchars as $illegalchar){ 
       //If any text matching the text contined in the illegalchars[] array (above) is found display the below error
         if(stripos($value,$illegalchar) !== FALSE){  
            $errors .= "<div class=\"errortext\">$possiblehackattempt</div>"; 
            $hackattempt = "1";
         } 
      } 
} 

//If we are happy the POSTed data is relatively clean, continue with below	
if(!$hackattempt)
{
//Get the POSTed data and store in the $emailto, $emailsubject and $emailmessage variables
$fromname     = $_POST['fromname'];
$emailfrom      = $_POST['emailfrom'];
$emailsubject = $_POST['emailsubject'];
$emailmessage = $_POST['emailmessage'];
$botcheck = $_POST['botcheck'];
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];

//If the name field is blank, retun the below error
if(!$fromname) 
{
$errors .= "<div class=\"errortext\">$noname</div>";
}

//If the email address field is blank, retun the below error
if(!$emailfrom) 
{
$errors .= "<div class=\"errortext\">$noemail</div>";
}

//If the email address entered appears to be of an incorrect format return the below error
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailfrom))
{ 
$errors .= "<div class=\"errortext\">$wrongemail</div>";
}

if(in_array($emailfrom, $blacklist))
{
	$errors .= "<div class=\"errortext\">$blacklisted</div>";
}

//If the email subject field is blank, return the below error
if(!$emailsubject) 
{
$errors .= "<div class=\"errortext\">$nosubject</div>";
}

// hard coded testing list form fields for CR and LF characters - all the fields that SHOULDN'T have them in 
if (eregi("\r",$emailsubject) || eregi("\n",$emailsubject)){ 
// die or report 
$errors .= "<div class=\"errortext\">$possiblehackattempt</div>";
} 

//If the email message field is blank, return the below error
if(!$emailmessage) 
{
$errors .= "<div class=\"errortext\">$nomessage</div>";
}

if($value1 + $value2 != $botcheck)
{
$errors .= "<div class=\"errortext\">$botcheckmessage</div>";
}

//Get the POSTed attachment data and store in the $attachment, $attachmenttype and $attachmentname variables
$attachment     = $_FILES['attachment']['tmp_name'];
$attachmenttype = $_FILES['attachment']['type'];
$attachmentname = $_FILES['attachment']['name'];

//Put the "From" address in the header
$headers = "From: $fromname <$emailfrom>";

//Check if a file has been POSTed from the form, if so continue with below
if (is_uploaded_file($attachment)) 
{
  //Check the POSTed attachment type, if it isn't in the allowed list then report the below error
  if(!in_array($attachmenttype, $filetypes))
{
	$errors .= "<div class=\"errortext\">$invalidfiletype</div>";
}
  
  $waiting = '1';
  		
  //Read the file to be attached, get the filesize and close
  $file = fopen($attachment,'rb');
  $data = fread($file,filesize($attachment));
  fclose($file);

  //Generate a MIME boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  //Add the headers required for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  //Add a multipart boundary  
  $emailmessage = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $emailmessage . "\n\n";

  //Base64 encode the attachment, this is to ensure the attachment survives delivery
  $data = chunk_split(base64_encode($data));

  //Add the actual attachment to the message
  $emailmessage .= "--{$mime_boundary}\n" .
              "Content-Type: {$attachmenttype};\n" .
              " name=\"{$attachmentname}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

//If there are no errors, send the message using the code below
if(!$errors ) 
{
mail($emailto, $emailsubject, $emailmessage, $headers);

//Return the below success message
$success .= "\n<div class=\"successtext\">$ok</div>";

//Clear the POSTed values
unset($_POST['fromname'], $_POST['emailfrom'], $_POST['emailsubject'], $_POST['emailmessage'], $_FILES);
}
}
}
$n1 = rand(0, 10);
$n2 = rand(0, 10);

$content .= '<script type="text/javascript"> function displayWaiting() {  if (document.getElementById(\'waiting\')) {   document.getElementById(\'waiting\').style.display=\'block\';  } } </script>';	
$content .= '<form method="POST" enctype="multipart/form-data">';
$content .= ''.$errors.' '.$success.'';
$content .= '<p>Your Name:<br />';
$content .= '<input type="text" name="fromname" value="'.$_POST['fromname'].'" class="input"/></p>';
$content .= '<p>Your Email:<br />';
$content .= '<input type="text" name="emailfrom" value="'.$_POST['emailfrom'].'" class="input"/></p>';
$content .= 'Subject:<br />';
$content .= '<input type="text" name="emailsubject" value="'.$_POST['emailsubject'].'" class="input"/></p>';
$content .= '<p>Message:<br />';
$content .= '<textarea name="emailmessage" class="textarea">'.$_POST['emailmessage'].'</textarea></p>';
$content .= '<p>File Attachment:<br />';
$content .= '<input type="file" name="attachment" class="fileinput" /></p>';
$content .= '<p>Are you human?</p>';
$content .= '<p>'.$n1.' + '.$n2.' = <input type="text" name="botcheck" size="2" maxlength="2" class="botcheck"/></p>';
$content .= '<input type="hidden" name="value1" value="'.$n1.'" />';
$content .= '<input type="hidden" name="value2" value="'.$n2.'" />';
$content .= '<p><input type="submit" id="submit" name="submit" value="Send" class="button" onClick="displayWaiting();"/></p>';
$content .= '<div id="waiting" style="display:none" class="waiting">'.$waiting.'</div>';
$content .= '</form>';

echo $content;

?>

Link to comment
Share on other sites

hehe, that's my script!  I wrote this, you grabbed it from the Themeforest forums right?

 

Are you running this from a hosting account or from your own local box?  Also, have you checked your SPAM folder?

 

Also, you seem to have commented out the following lines:

 

//"Content-Disposition: attachment;\n" .
//              " filename=\"{$fileatt_name}\"\n" .

 

Change it to:

 

"Content-Disposition: attachment;\n" .
              " filename=\"{$fileatt_name}\"\n" .

 

Hope this helps.

Link to comment
Share on other sites

EDIT: Hey looks like it does work now, the email just got delayed a couple minutes. So either I fixed it by uncommenting that line, or what I did was upload the script and files into my root directory, instead of some folder. Example:

 

It was in www.moochcafe.info/mailerr/mailer.php

Then I moved it to www.moochcafe.info/mailer.php

 

Does it matter?

 

Thanks so much for the fast reply! Your awesome.

--------

 

lol haha! Yeah i got it from Theme Forest haha... I've been searching for a script like this but couldn't find a descent one, then I found this one but I just can't get it to work.

 

I didn't comment that out actually, but I just uncommented it and it still doesn't work. I'm running it from a hosting account, here is my test address I uploaded it to. www.moochcafe.info/mailer.php

 

Yes I checked my spam folder and the email provider is Gmail which usually receives every single email. Any other ideas?

Link to comment
Share on other sites

Hi supwd,

 

I've just downloaded the file again from ThemeForest and it works for me, I've just removed a variable that didn't need to be in there though (although that shouldn't have made a difference)

 

Try this code:

 

<?php
//*****************************************SETTINGS START***************************************************************
//
//Set your email to address, this is the email address which you would all emails sent from this form to go to
$emailto    = you@youremail.com';
//
//Set the error messages you would like to be displayed in the case of an empty email address, an incorrectly
//formatted email address, an empty subject and an empty message body
$noname	  	   = 'You must fill in your name.';
$noemail	   = 'You must fill in an email address to send to.';
$wrongemail    = 'The email address your entered appears to be invalid.';
$blacklisted   = 'Your email address has been blacklisted.';	
$nosubject     = 'You must fill in a message subject.';
$nomessage     = 'You must enter a message.';
$waiting       = 'Please wait while your message is sent.';
$botcheckmessage = 'You incorrectly answered the anti-spam question.';
$possiblehackattempt = 'A possible hack attempt has been detected, please check your entered data and try again.';
$invalidfiletype = 'We do not accept this type of file.  We can only accept files of type: PDF, PHP, ZIP';
//
//Set the success message you would like to appear when a message has been correctly sent
$ok			  = 'Your message has been successfully sent.';
//
//The below is used for simple header injection attack prevention and includes common strings used in such attacks
$illegalchars = array(
"Content-Type:", 
"To:",
"Cc:", 
"Bcc:"
); 
//
//The below is a list of allowed filetypes, please see the filetypes.txt file packaged with this download for an 
//extensive list of filetypes
$filetypes = array(
"application/x-zip-compressed",		//.zip files
"application/pdf"					//.pdf files
);
//The below is a blacklist of email addresses, if you want to stop certain people from using this form, add their email
//addresses here
$blacklist = array(
"somebody@dontcomebackagain.com",
"somebodyelse@dontcomebackagain.com"
);
//*****************************************SETTINGS END*****************************************************************


//Check for a POST request from the form, if one is received do the below
if($_SERVER['REQUEST_METHOD'] == 'POST') {

//Set the $errors and $hackattempt variables to NULL
$hackattempt = "";
$errors = "";

   //Check all of the POSTed data for any illegal Text as defined above in the illegalchars[] array (above)
   foreach ($_POST as $key => $value){ 
      $$key = $value; 
      
      foreach($illegalchars as $illegalchar){ 
       //If any text matching the text contined in the illegalchars[] array (above) is found display the below error
         if(stripos($value,$illegalchar) !== FALSE){  
            $errors .= "<div class=\"errortext\">$possiblehackattempt</div>"; 
            $hackattempt = "1";
         } 
      } 
} 

//If we are happy the POSTed data is relatively clean, continue with below	
if(!$hackattempt)
{
//Get the POSTed data and store in the $emailto, $emailsubject and $emailmessage variables
$fromname     = $_POST['fromname'];
$emailfrom      = $_POST['emailfrom'];
$emailsubject = $_POST['emailsubject'];
$emailmessage = $_POST['emailmessage'];
$botcheck = $_POST['botcheck'];
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];

//If the name field is blank, retun the below error
if(!$fromname) 
{
$errors .= "<div class=\"errortext\">$noname</div>";
}

//If the email address field is blank, retun the below error
if(!$emailfrom) 
{
$errors .= "<div class=\"errortext\">$noemail</div>";
}

//If the email address entered appears to be of an incorrect format return the below error
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailfrom))
{ 
$errors .= "<div class=\"errortext\">$wrongemail</div>";
}

if(in_array($emailfrom, $blacklist))
{
	$errors .= "<div class=\"errortext\">$blacklisted</div>";
}

//If the email subject field is blank, return the below error
if(!$emailsubject) 
{
$errors .= "<div class=\"errortext\">$nosubject</div>";
}

// hard coded testing list form fields for CR and LF characters - all the fields that SHOULDN'T have them in 
if (eregi("\r",$emailsubject) || eregi("\n",$emailsubject)){ 
// die or report 
$errors .= "<div class=\"errortext\">$possiblehackattempt</div>";
} 

//If the email message field is blank, return the below error
if(!$emailmessage) 
{
$errors .= "<div class=\"errortext\">$nomessage</div>";
}

if($value1 + $value2 != $botcheck)
{
$errors .= "<div class=\"errortext\">$botcheckmessage</div>";
}

//Get the POSTed attachment data and store in the $attachment, $attachmenttype and $attachmentname variables
$attachment     = $_FILES['attachment']['tmp_name'];
$attachmenttype = $_FILES['attachment']['type'];
$attachmentname = $_FILES['attachment']['name'];

//Put the "From" address in the header
$headers = "From: $fromname <$emailfrom>";

//Check if a file has been POSTed from the form, if so continue with below
if (is_uploaded_file($attachment)) 
{
  //Check the POSTed attachment type, if it isn't in the allowed list then report the below error
  if(!in_array($attachmenttype, $filetypes))
{
	$errors .= "<div class=\"errortext\">$invalidfiletype</div>";
}
  		
  //Read the file to be attached, get the filesize and close
  $file = fopen($attachment,'rb');
  $data = fread($file,filesize($attachment));
  fclose($file);

  //Generate a MIME boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  //Add the headers required for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  //Add a multipart boundary  
  $emailmessage = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $emailmessage . "\n\n";

  //Base64 encode the attachment, this is to ensure the attachment survives delivery
  $data = chunk_split(base64_encode($data));

  //Add the actual attachment to the message
  $emailmessage .= "--{$mime_boundary}\n" .
              "Content-Type: {$attachmenttype};\n" .
              " name=\"{$attachmentname}\"\n" .
              "Content-Disposition: attachment;\n" .
              " filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

//If there are no errors, send the message using the code below
if(!$errors) 
{
mail($emailto, $emailsubject, $emailmessage, $headers);

//Return the below success message
$success .= "\n<div class=\"successtext\">$ok</div>";

//Clear the POSTed values
unset($_POST['fromname'], $_POST['emailfrom'], $_POST['emailsubject'], $_POST['emailmessage'], $_FILES);
}
}
}
$n1 = rand(0, 10);
$n2 = rand(0, 10);

$content .= '<script type="text/javascript"> function displayWaiting() {  if (document.getElementById(\'waiting\')) {   document.getElementById(\'waiting\').style.display=\'block\';  } } </script>';	
$content .= '<form method="POST" enctype="multipart/form-data">';
$content .= ''.$errors.' '.$success.'';
$content .= '<p>Your Name:<br />';
$content .= '<input type="text" name="fromname" value="'.$_POST['fromname'].'" class="input"/></p>';
$content .= '<p>Your Email:<br />';
$content .= '<input type="text" name="emailfrom" value="'.$_POST['emailfrom'].'" class="input"/></p>';
$content .= 'Subject:<br />';
$content .= '<input type="text" name="emailsubject" value="'.$_POST['emailsubject'].'" class="input"/></p>';
$content .= '<p>Message:<br />';
$content .= '<textarea name="emailmessage" class="textarea">'.$_POST['emailmessage'].'</textarea></p>';
$content .= '<p>File Attachment:<br />';
$content .= '<input type="file" name="attachment" class="fileinput" /></p>';
$content .= '<p>Are you human?</p>';
$content .= '<p>'.$n1.' + '.$n2.' = <input type="text" name="botcheck" size="2" maxlength="2" class="botcheck"/></p>';
$content .= '<input type="hidden" name="value1" value="'.$n1.'" />';
$content .= '<input type="hidden" name="value2" value="'.$n2.'" />';
$content .= '<p><input type="submit" id="submit" name="submit" value="Send" class="button" onClick="displayWaiting();"/></p>';
$content .= '<div id="waiting" style="display:none" class="waiting">'.$waiting.'</div>';
$content .= '</form>';

echo $content;

?>

 

Hope this helps.

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.