Я пытаюсь отправить письмо с файлом в качестве вложения. Я загрузил веб-сайт в 000webhost, и когда я отправляю форму, он возвращает некоторые предупреждения.Ошибка отправки файла с помощью электронной почты (HTML + PHP)
Это код моего HTML формы:
<form class='contacto' action="../php/enviar.php" method="post" enctype="multipart/form-data">
<div><label>Nombre:</label><input type='text' value='' name="to"></div>
<div><label>E-Mail:</label><input type='text' value='' name="from"></div>
<div><label>Asunto:</label><input type='text' value='' name="subject"></div>
<div><label>Mensaje:</label><textarea rows='6' name="messagehtml"></textarea></div>
<div><label>Adjuntar archivo:</label><input type="file" name="fileatt"></div>
<div><input type='submit' value='Enviar Mensaje' id="enviar"></div>
</form>
Это PHP-файл:
<?php
function mail_file($to, $subject, $messagehtml, $from, $fileatt, $replyto="") {
// handles mime type for better receiving
$ext = strrchr($fileatt , '.');
$ftype = "";
if ($ext == ".doc") $ftype = "application/msword";
if ($ext == ".jpg") $ftype = "image/jpeg";
if ($ext == ".gif") $ftype = "image/gif";
if ($ext == ".zip") $ftype = "application/zip";
if ($ext == ".pdf") $ftype = "application/pdf";
if ($ftype=="") $ftype = "application/octet-stream";
// read file into $data var
$file = fopen($fileatt, "w");
$data = fread($file, filesize($fileatt));
fclose($file);
// split the file into chunks for attaching
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
// build the headers for attachment and html
$h = "From: $from\r\n";
if ($replyto) $h .= "Reply-To: ".$replyto."\r\n";
$h .= "MIME-Version: 1.0\r\n";
$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$h .= "This is a multi-part message in MIME format.\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-type:text/html; charset=iso-8859-1\r\n";
$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$h .= $messagehtml."\r\n\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
$h .= "Content-Transfer-Encoding: base64\r\n";
$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
$h .= $content."\r\n\r\n";
$h .= "--".$uid."--";
// send mail
return mail($to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h)) ;
}
mail_file("[email protected]", $_POST['subject'], $_POST["messagehtml"], $_POST['from'], $_FILES['fileatt'], "");
?>
Я изменил $ _FILE [ 'fileatt'] (в конце кода PHP) за $ _FILES [ 'fileatt']
Теперь я получил это предупреждение:
Warning: fopen() expects parameter 1 to be string, array given in
Warning: filesize() [function.filesize]: stat failed for Array in
Warning: fread(): supplied argument is not a valid stream resource in
Warning: fclose(): supplied argument is not a valid stream resource in
Warning: basename() expects parameter 1 to be string, array given in
Это похоже на то, что мой код подготовлен для нескольких файлов, но я хочу использовать эту форму для отправки нулевого или одного файла.
Благодаря
избавиться от возвращения, плюс у вас нет '$ _FILES' массив. –
'$ _POST ['fileatt']' не существует. Вам нужно получить файл из '$ _FILES' (см. Http://php.net/manual/en/features.file-upload.php). –
Да, это была большая ошибка, я изменил $ _POST ['fileatt'] на $ FILE _ ['fileatt'], эта часть немного запутывает для меня –