【问题标题】:Error sending file attached with email (HTML + PHP)发送电子邮件附件时出错 (HTML + PHP)
【发布时间】:2015-11-28 04:36:49
【问题描述】:

我正在尝试发送带有文件作为附件的邮件。我将网站上传到 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: $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("mymail@gmail.com", $_POST['subject'], $_POST["messagehtml"], $_POST['from'], $_FILES['fileatt'], "");
?>

我为 $_FILES['fileatt'] 更改了 $_FILE['fileatt'](接近 php 代码的末尾)

现在我收到了以下警告:

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

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

谢谢

【问题讨论】:

  • 去掉return,加上你没有$_FILES数组。
  • $_POST['fileatt'] 不存在。您需要从$_FILES 获取文件(参见:php.net/manual/en/features.file-upload.php)。
  • 是的,这是一个很大的错误,我将 $_POST['fileatt'] 更改为 $FILE_['fileatt'],这部分对我来说有点混乱
  • $FILE_['fileatt'], ---$_FILE['fileatt'] 这是不正确的。如前所述,它是$_FILES两次。这样做,奇迹可能会发生;-)
  • 另外,对于return php.net/manual/en/function.return.php "如果从函数内部调用,return语句立即结束当前函数的执行... " - 我已经说过了。所以你的下一个函数不会启动。

标签: php html forms file attachment


【解决方案1】:

尝试使用 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!";
}

【讨论】:

  • 我们可以坚持这个问题,而不是替代吗?
  • 我看到了一些带有该库的示例,但我不明白为什么这些代码的字段中填充了数据,我想将表单数据作为 $_POST['from'] 等发送。可能我用动态帖子更改数据?
  • 我已阅读问题:“我正在尝试发送带有文件附件的邮件。”如果此人未能发送带有附件的电子邮件,我通知了另一种方法来帮助解决该需求。如果我因试图提供帮助而被“投票”,我将不再这样做。
猜你喜欢
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
相关资源
最近更新 更多