Skip to content

Instantly share code, notes, and snippets.

@sharkyak
Created April 15, 2016 06:11
Show Gist options
  • Save sharkyak/103ddf56f0cc20c5e57cad6351ea221f to your computer and use it in GitHub Desktop.
Save sharkyak/103ddf56f0cc20c5e57cad6351ea221f to your computer and use it in GitHub Desktop.
Contact form file send
<?php
if($_POST)
{
$to_email = "a.kazakov@computer-ufa.ru"; //Recipient email, Replace with own email here
$from_email = "noreply@YOUR-DOMAIN.com"; //From email address (eg: no-reply@YOUR-DOMAIN.com)
$subject = "subject";
//Sanitize input data using PHP filter_var().
$phone1 = filter_var($_POST["phone1"], FILTER_SANITIZE_STRING);
$phone2 = filter_var($_POST["phone2"], FILTER_SANITIZE_STRING);
$phone3 = filter_var($_POST["phone3"], FILTER_SANITIZE_STRING);
//email body
$message_body = $phone1.$phone2.$phone3 ;
### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES['file'])) //check uploaded file
{
//get file details we need
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_error = $_FILES['file']['error'];
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}else{
//proceed with PHP email.
$headers = "From:".$from_email."\r\n".
'X-Mailer: PHP/' . phpversion();
$body = $message_body;
}
$send_mail = mail($to_email, $subject, $body, $headers);
header('Location: http://dev.ak-agency.ru/marlena/');
}
<form id="file-form" method="post" action="contact_me.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520" />
<label class="form-label">Загрузите фото:</label>
<div class="w-input form-input file">
<input type="file" name="file">
</div>
<label class="form-label">Ваш телефон:</label>
<input type="text" placeholder="+7" name="phone1" class="w-input form-phone-input">
<input type="text" placeholder="123" name="phone2" class="w-input form-phone-input _2">
<input type="text" placeholder="1234567" name="phone3" class="w-input form-phone-input _3">
<div class="form-btn-wrap">
<input type="submit" value="Оставить заявку" class="w-button form-btn">
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment