I can not send a file to the mail. The file is loaded in the form, php version 7.1, so there is no possibility to use the old methods (does not skip)

Here is an example of the last solution that gives an error:

Warning: mail (): Multiple or malformed newlines found in additional_header in ....

if (isset($_POST['name'])) { $mailto = 'weelman@mail.ru'; $from_mail = 'info@zim-kolbasi.ru'; $replyto = 'info@zim-kolbasi.ru'; $from_name = 'Kolbaski'; $message = 'Имя: '.$_POST['name'].' Телефон: '.$_POST['phone'].' Почта: '.$_POST['email'].' Сообщение: '.$_POST['message']; $subject = 'Информация от посетителя Колбас'; if($_FILES["file"]["tmp_name"]) { $filename = $_FILES["file"]["name"]; $content = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"]))); } $EOL = "\r\n"; $uid = md5(uniqid(time())); $name = basename($filename); $header = "From: " . $from_name . " <" . $from_mail . ">\r\n"; $header .= "Reply-To: " . $replyto . "\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--" . $uid . "\r\n"; $header .= "Content-type:text/html; charset=utf-8\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; $header .= "<div style='color: black'>" . $message . "</div>\r\n"; $header .= "--" . $uid . "\r\n"; $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $EOL . ""; $header .= "Content-Transfer-Encoding: base64" . $EOL . ""; $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"" . $EOL . ""; $header .= $content . $EOL; $header .= "--" . $uid . "--"; var_dump($header); if (mail($mailto, $subject, $message, $header)) { echo "<script>alert('Success');</script>"; // or use booleans here } else { echo "<script>alert('Failed');</script>"; } } 

Help me please)

  • That's why removed the problem areas? Now you, I think, will no longer have Multiple or malformed newlines found in additional_header in - Total Pusher

1 answer 1

The reason is multiple line additional_headers in additional_headers .

For security reasons, several new line breaks in a row \r\r, \r\0, \r\n\r\n, \n\n, \n\0 lead to this error.

It should be "cleaned" additional_headers : replace line breaks with single ones.

In your case, replace \r\n\r\n with \r\n\ . And use this variable for headers only. Attached files should be moved to the message body $message .

 $from_name = 'Your name'; $from_mail = 'email@site.ru'; $mailto = 'email@site.ru'; $replyto = 'email@site.ru'; $subject = 'Информация от посетителя Колбас'; // Будем отправлять этот файл $filename = __FILE__; $content = chunk_split(base64_encode(file_get_contents($filename))); $EOL = "\r\n"; $uid = md5(uniqid(time())); $name = basename($filename); // Заголовки $header = sprintf("From: %s <%s>\r\n", $from_name, $from_mail); $header .= sprintf("Reply-To: %s\r\n", $replyto); $header .= "MIME-Version: 1.0\r\n"; $header .= sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\r\n", $uid); // HTML текст $body = '<b>Текст сообщения</b>'; $message = sprintf("--%s\r\n", $uid); $message .= "Content-Type: text/html; charset=utf-8\r\n"; $message .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; $message .= $body . "\r\n"; $message .= sprintf("--%s\r\n", $uid); // Аттач $message .= sprintf("Content-Type: application/octet-stream; name=\"%s\"\r\n", $name); $message .= "Content-Transfer-Encoding: base64" . $EOL; $message .= sprintf("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n", $name); $message .= $content . "\r\n"; $message .= sprintf("--%s--", $uid); var_dump(mail($mailto, $subject, $message, $header)); 
  • I changed the code, but the file does not come to the mail, but the message is: Message: Msg msg beah - af033981e660d6567ccf9949cf0cc9c1 Content-Type: application / octet-stream; name = "38530328_433151437196201_7278755091702611968_n.jpg" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename = "38530328_433151437196201_7278755091702611968_n.jpg" / 9j / 4AAQSkZJRgABAQAAAQABAAD / 7QB8UGhvdG9zaG9wIDMuM - Oriva Dancer
  • I'll try to fix it - Total Pusher
  • thanks a lot - Oriva Dancer
  • put the old version of the code - Oriva Dancer
  • Earned) You are the best, thank you very much - Oriva Dancer