发送带有电子邮件附件的文件错误(HTML + PHP)



我正在尝试发送带有文件作为附件的邮件。我将网站上传到000webhost,当我提交表单时,它返回一些警告。

这是我的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: $fromrn";
if ($replyto) $h .= "Reply-To: ".$replyto."rn";
$h .= "MIME-Version: 1.0rn";
$h .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
$h .= "This is a multi-part message in MIME format.rn";
$h .= "--".$uid."rn";
$h .= "Content-type:text/html; charset=iso-8859-1rn";
$h .= "Content-Transfer-Encoding: 7bitrnrn";
$h .= $messagehtml."rnrn";
$h .= "--".$uid."rn";
$h .= "Content-Type: ".$ftype."; name="".basename($fileatt).""rn";
$h .= "Content-Transfer-Encoding: base64rn";
$h .= "Content-Disposition: attachment; filename="".basename($fileatt).""rnrn";
$h .= $content."rnrn";
$h .= "--".$uid."--";
// send mail
return mail( $to, $subject, strip_tags($messagehtml), str_replace("rn","n",$h) ) ;
}
mail_file("mymail@gmail.com", $_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

看起来我的代码是为多个文件准备的,但是我想使用这个表单发送零个或一个文件。

谢谢

尝试使用PHPMailer库:https://github.com/PHPMailer/PHPMailer

代码示例:

<?php
/**
 * This example shows sending a message using a local sendmail binary.
 */
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

相关内容

  • 没有找到相关文章

最新更新