Skip to content

Instantly share code, notes, and snippets.

@codingfox-rus
Last active August 29, 2015 14:02
Show Gist options
  • Save codingfox-rus/c151fc3b9186fd4c9c5d to your computer and use it in GitHub Desktop.
Save codingfox-rus/c151fc3b9186fd4c9c5d to your computer and use it in GitHub Desktop.
PHP mail() example with attached file
<?php
# получаем данные и отсекаем пробельные символы в начале и конце:
$name = @ trim ($_POST['your-name']);
$email = @ trim ($_POST['your-email']);
$fileName = $_FILES['file-rezume']['name'];
if ( isset($_POST['your-message']) ) {
$userMessage = @ trim ($_POST['your-message']);
} else {
$userMessage = "";
}
//$emailTo = "vopros@enjoy-job.ru";
$emailTo = "scarheadd@mail.ru";
$from = "mail@enjoy-job.ru";
$subject = "=?utf-8?B?" . base64_encode("Новая заявка") . "?=";
$message = "Имя: $name \n E-mail: $email \n Сообщение: $userMessage \n";
$uploaddir = "./userfiles/";
$uploadfile = $uploaddir . basename($fileName);
if (move_uploaded_file($_FILES['file-rezume']['tmp_name'], $uploadfile) == FALSE) {
echo "Ошибка при загрузке файла!<br>";
}
$r = sendMailAttachment($emailTo, $from, $subject, $message, $uploadfile); // отправка письма c вложением
echo ($r) ? 'Письмо отправлено' : 'Ошибка. Письмо не отправлено!';
function sendMailAttachment($mailTo, $from, $subject, $message, $file = false){
$separator = "---"; // разделитель в письме
// Заголовки для письма
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: $from\nReply-To: $from\n"; // задаем от кого письмо
$headers .= "Content-Type: multipart/mixed; boundary=\"$separator\""; // в заголовке указываем разделитель
// если письмо с вложением
if ($file) {
$bodyMail = "--$separator\n"; // начало тела письма, выводим разделитель
$bodyMail .= 'Content-type: text/plain; charset="utf-8"'; // кодировка письма
$bodyMail .= "Content-Transfer-Encoding: quoted-printable"; // задаем конвертацию письма
$bodyMail .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode(basename($file))."?=\n\n"; // задаем название файла
$bodyMail .= $message."\n"; // добавляем текст письма
$bodyMail .= "--$separator\n";
$fileRead = fopen($file, "r"); // открываем файл
$contentFile = fread($fileRead, filesize($file)); // считываем его до конца
fclose($fileRead); // закрываем файл
$bodyMail .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode(basename($file))."?=\n";
$bodyMail .= "Content-Transfer-Encoding: base64\n"; // кодировка файла
$bodyMail .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode(basename($file))."?=\n\n";
$bodyMail .= chunk_split(base64_encode($contentFile))."\n"; // кодируем и прикрепляем файл
$bodyMail .= "--".$separator ."--\n";
// письмо без вложения
} else {
$bodyMail = $message;
}
$result = mail($mailTo, $subject, $bodyMail, $headers); // отправка письма
if ($result == FALSE) {
return "Ошибка при отправке через функцию mail()";
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment