Skip to content

Instantly share code, notes, and snippets.

@ColtMoney
Created July 20, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColtMoney/682fcb83c6ee9419d6a82986f5310ba4 to your computer and use it in GitHub Desktop.
Save ColtMoney/682fcb83c6ee9419d6a82986f5310ba4 to your computer and use it in GitHub Desktop.
<?php
$to = 'example@gmail.com';
$errors = array();
$method = $_SERVER['REQUEST_METHOD'];
$c = true;
if ( $method === 'POST' ) {
$project_name = trim($_POST["project_name"]);
$form_subject = trim($_POST["form_subject"]);
foreach ( $_POST as $key => $value ) {
if ( $value != "" && $key != "project_name" && $key != "form_subject" ) {
$message .= "
" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
} else if ( $method === 'GET' ) {
$project_name = trim($_GET["project_name"]);
$form_subject = trim($_GET["form_subject"]);
foreach ( $_GET as $key => $value ) {
if ( $value != "" && $key != "project_name" && $key != "form_subject" ) {
$message .= "
" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
}
$message = "<table style='width: 100%;'>$message</table>";
function adopt($text) {
return '=?UTF-8?B?'.Base64_encode($text).'?=';
}
if ( isset( $_FILES["file"]) ) {
$file_name = $_FILES["file"]["name"];
$file_size =$_FILES["file"]["size"];
$file_tmp =$_FILES["file"]["tmp_name"];
$file_type=$_FILES["file"]["type"];
$file_ext=strtolower(end(explode(".",$_FILES["file"]["name"])));
$expensions= array("jpeg","jpg","png", "doc", "docx", "pdf");
if(in_array($file_ext,$expensions) === false){
$errors["status"] = "error";
$errors["text"]= "Выберите файл jpg, png, pdf или doc";
}
else if ( !empty( $_FILES['file']['tmp_name'] ) and $_FILES['file']['error'] == 0 ) {
$filepath = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
} else {
$filepath = '';
$filename = '';
}
}
if(empty($errors)){
send_mail($to, $message, $project_name, $form_subject, $filepath, $filename);
$errors["status"] = "success";
$errors["text"] = "Заявка отправлена!";
}
// Вспомогательная функция для отправки почтового сообщения с вложением
function send_mail($to, $message, $project_name, $form_subject, $filepath, $filename)
{
$boundary = "--".md5(uniqid(time())); // генерируем разделитель
$headers = "From: ".adopt($project_name)." <".$to.">" . PHP_EOL . "Reply-To: ".$to."" . PHP_EOL;
$headers .= "MIME-Version: 1.0\r\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
$multipart = "--".$boundary."\r\n";
$multipart .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$multipart .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$message = $message."\r\n\r\n";
$multipart .= $message;
$file = '';
if ( !empty( $filepath ) ) {
$fp = fopen($filepath, "r");
if ( $fp ) {
$content = fread($fp, filesize($filepath));
fclose($fp);
$file .= "--".$boundary."\r\n";
$file .= "Content-Type: application/octet-stream\r\n";
$file .= "Content-Transfer-Encoding: base64\r\n";
$file .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$file .= chunk_split(base64_encode($content))."\r\n";
}
}
$multipart .= $file."--".$boundary."--\r\n";
mail($to, adopt($form_subject), $multipart, $headers);
}
echo json_encode($errors);
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment