File: /home/awaldron/www/handleform.php
<?php
// Email settings
$toEmail = 'awaldron@buildinmotion.com'; // Recipient email
$from = $_POST['emailAddress']; // Sender email
$fromName = $_POST['yourName']; // Sender name
// File upload settings
$attachmentUploadDir = "uploads/";
$allowFileTypes = array('pdf', 'doc', 'docx');
/* Form submission handler code */
$postData = $uploadedFile = $statusMsg = $valErr = '';
$msgClass = 'errordiv';
if(isset($_POST['submitButton'])){
// Get the submitted form data
$postData = $_POST;
extract($_POST);
extract($_FILES);
// Validate input data
if(empty($yourName)){
$valErr .= '• Please enter your name.<br/>';
}
if(empty($emailAddress) || filter_var($emailAddress, FILTER_VALIDATE_EMAIL) === false){
$valErr .= '• Please enter a valid email.<br/>';
}
if(empty($phoneNumber)){
$valErr .= '• Please enter your phone number.<br/>';
}
if(empty($positionType)){
$valErr .= '• Please enter your desired position type.<br/>';
}
if(empty($epaCertified)){
$valErr .= '• Please enter your EPA Certification status.<br/>';
}
if(empty($pittsburghElectricCertified)){
$valErr .= '• Please specify if you\'re certified in Pittsburgh.<br/>';
}
if(empty($yearsOfExperience)){
$valErr .= '• Please enter your years of experience.<br/>';
}
if(empty($doYouHaveAValidPennsylvaniaDriversLicense)){
$valErr .= '• Please specify if you have a PA Driver\'s License.<br/>';
}
if(empty($canYouTroubleshootResidentialElectricProblems)){
$valErr .= '• Please indicate if you can troubleshoot electrical problems.<br/>';
}
if(empty($canYouWorkIndependently)){
$valErr .= '• Please indicate if you can work independently.<br/>';
}
if(empty($canYouInstallA100150200AmpResidentialServiceAndConnectWithPowerCompanysLine)){
$valErr .= '• Please indicate if you can connect service to power lines.<br/>';
}
if(empty($areYouAMemberOfLocal5ElectricalUnionOrHoldALocal5Card)){
$valErr .= '• Please specify if you are a union member.<br/>';
}
if(empty($doYouCurrentlyOwnYourOwnBusiness)){
$valErr .= '• Please specify if you own a buisness.<br/>';
}
if(empty($areYouWillingToWorkOver40HrsAWeekAndTakeOvertime)){
$valErr .= '• Please specify if you\'re willing to work overtime.<br/>';
}
if(empty($highestDegreeObtained)){
$valErr .= '• Please specify your highest degree obtained.<br/>';
}
if(empty($highestDegreeObtained)){
$valErr .= '• Please specify your highest degree obtained.<br/>';
}
if(empty($resume['name'])){
$valErr .= '• Please attach your resume.<br/>';
}
// Check whether submitted data is valid
if(empty($valErr)){
$uploadStatus = 1;
// Upload attachment file
if(!empty($_FILES["resume"]["name"])){
// File path config
$targetDir = $attachmentUploadDir;
$fileName = basename($_FILES["resume"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
// Allow certain file formats
if(in_array($fileType, $allowFileTypes)){
// Upload file to the server
if(move_uploaded_file($_FILES["resume"]["tmp_name"], $targetFilePath)){
$uploadedFile = $targetFilePath;
}else{
$uploadStatus = 0;
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$uploadStatus = 0;
$statusMsg = 'Sorry, only '.implode('/', $allowFileTypes).' files are allowed to upload.';
}
}
if($uploadStatus == 1){
// Email subject
$emailSubject = 'Contact Request Submitted by '.$name;
// Email message
$htmlContent = '<h2>Contact Request Submitted</h2>
<p><b>Name:</b> '.$name.'</p>
<p><b>Email:</b> '.$email.'</p>
<p><b>Subject:</b> '.$subject.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
// Header for sender info
$headers = "From: $fromName"." <".$from.">";
// Add attachment to email
if(!empty($uploadedFile) && file_exists($uploadedFile)){
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(is_file($uploadedFile)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($uploadedFile,"rb");
$data = @fread($fp,filesize($uploadedFile));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" .
"Content-Description: ".basename($uploadedFile)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email;
// Send email
$mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
// Delete attachment file from the server
@unlink($uploadedFile);
}else{
// Set content-type header for sending HTML email
$headers .= "\r\n". "MIME-Version: 1.0";
$headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
// Send email
$mail = mail($toEmail, $emailSubject, $htmlContent, $headers);
}
// If mail sent
if($mail){
$statusMsg = 'Thanks! Your contact request has been submitted successfully.';
$msgClass = 'succdiv';
$postData = '';
}else{
$valErr = "bad email";
$statusMsg = 'Email failed to send, please try again.';
}
}
}else{
$valErr = !empty($valErr)?'<br/>'.trim($valErr, '<br/>'):'';
$statusMsg = '<b>Application not sent. Please correct the following issues:</b>'.$valErr;
}
}
?>