Created
March 28, 2014 08:27
-
-
Save rhamdeew/9827915 to your computer and use it in GitHub Desktop.
PHP send mail with attach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$notAllowedExt = array('exe','apk'); //Неприемлимые расширения | |
$maxFileSize = 100 * 1024 * 1024; //100 MB | |
// Получатель (измените на ваш e-mail адрес) | |
$strEmail = "from@from.ru"; | |
$email = filter_var($_REQUEST['email'],FILTER_VALIDATE_EMAIL); | |
if($email!==FALSE) { | |
$name = filter_var($_REQUEST['name'],FILTER_SANITIZE_STRING); | |
$subject = filter_var($_REQUEST['subject'],FILTER_SANITIZE_STRING); | |
$message = filter_var($_REQUEST['message'],FILTER_SANITIZE_STRING); | |
$headers = array(); | |
$headers[] = "MIME-Version: 1.0"; | |
if (isset($_FILES['file'])) { | |
//проверяем размер и тип файла | |
$ext = end(explode('.', strtolower($_FILES['file']['name']))); | |
if (in_array($ext, $notAllowedExt)) { | |
die('false'); | |
} | |
if ($maxFileSize < $_FILES['file']['size']) { | |
die('false'); | |
} | |
if (is_uploaded_file($_FILES['file']['tmp_name'])) { | |
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name']))); | |
$fileName = $_FILES['file']['name']; | |
$boundary =md5(date('r', time())); | |
$headers[] = "Content-Type: multipart/mixed; boundary=\"_1_$boundary\""; | |
//For attach------------------------------------------ | |
$message="This is a multi-part message in MIME format. | |
--_1_$boundary | |
Content-Type: multipart/alternative; boundary=\"_2_$boundary\" | |
--_2_$boundary | |
Content-Type: text/plain; charset=\"utf-8\" | |
$message | |
--_2_$boundary-- | |
--_1_$boundary | |
Content-Type: application/octet-stream; name=\"$fileName\" | |
Content-Transfer-Encoding: base64 | |
Content-Disposition: attachment | |
$attachment | |
--_1_$boundary--"; | |
//For attach------------------------------------------ | |
} | |
} | |
else $headers[] = "Content-type: text/plain; charset=utf-8"; | |
$headers[] = "From: Mail robot <support@site.ru>"; | |
$headers[] = "Reply-To: sender <".$email.">"; | |
$headers[] = "Subject: ".$subject; | |
$headers[] = "X-Mailer: PHP/".phpversion(); | |
mail($strEmail,$subject,$name.":\r\n".$email.":\r\n".$message, implode("\r\n", $headers)); | |
echo 'success'; | |
} | |
else echo 'false'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment