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;
?>