Answer the question
In order to leave comments, you need to log in
Sending files by mail. Why are they loaded on the server, but not attached to the mail?
Good afternoon!
Tell me good people!
The file is uploaded to the server - everything is fine. But the question arose of sending these files to the mail.
I added a sending code (which worked when sending one file), everything is loaded on the server, but the letter comes to the mail without files.
Please tell me, I'm not very strong in this matter
Here is the code
<?php
if( isset( $_POST['my_file_upload'] ) ){
// ВАЖНО! тут должны быть все проверки безопасности передавемых файлов и вывести ошибки если нужно
$uploaddir = './uploads'; // . - текущая папка где находится submit.php
// cоздадим папку если её нет
if( ! is_dir( $uploaddir ) ) mkdir( $uploaddir, 0777 );
$files = $_FILES; // полученные файлы
$done_files = array();
// переместим файлы из временной директории в указанную
foreach( $files as $file ){
$file_name = $file['name'];
if( move_uploaded_file( $file['tmp_name'], "$uploaddir/$file_name" ) ){
$done_files[] = realpath( "$uploaddir/$file_name" );
}
}
$data = $done_files ? array('files' => $done_files ) : array('error' => 'Ошибка загрузки файлов.');
$thm = 'Файл';
$msg = "Файл";
$mail_to = '[email protected]';
// Отправляем почтовое сообщение
if(empty($done_files)) mail($mail_to, $thm, $msg);
else send_mail($mail_to, $thm, $msg, $done_files);
// Вспомогательная функция для отправки почтового сообщения с вложением
function send_mail($to, $thm, $html, $done_files)
{
$fp = fopen($done_files,"r");
if (!$fp)
{
print "Файл $path не может быть прочитан";
exit();
}
$file = fread($fp, filesize($done_files));
fclose($fp);
$boundary = "--".md5(uniqid(time())); // генерируем разделитель
$headers .= "MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
$multipart .= "--$boundary\n";
$kod = 'utf-8'; // или $kod = 'windows-1251';
$multipart .= "Content-Type: text/html; charset=$kod\n";
$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n";
$multipart .= "$html\n\n";
$message_part = "--$boundary\n";
$message_part .= "Content-Type: application/octet-stream\n";
$message_part .= "Content-Transfer-Encoding: base64\n";
$message_part .= "Content-Disposition: attachment; filename = \"".$done_files."\"\n\n";
$message_part .= chunk_split(base64_encode($file))."\n";
$multipart .= $message_part."--$boundary--\n";
if(!mail($to, $thm, $multipart, $headers))
{
echo "К сожалению, письмо не отправлено";
exit();
}
}
die( json_encode( $data ) );
}
Answer the question
In order to leave comments, you need to log in
judging by the $done_files code, this is an array, and in the send_mail function you work with it as with a string. why?
What is the Opencart tag for?
If you do this and it's true in Opencart, there is a class included for sending emails. See system/library/mail
It has a method for attaching files to mail function addAttachment($filename)
Webkit browsers need prefix
jsfiddle.net/QW01_01/txn4pj5f
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question