【问题标题】:PHPMailer refuses to send attachmentPHPMailer 拒绝发送附件
【发布时间】:2015-06-06 04:43:26
【问题描述】:

我已经尝试了一段时间来解决这个问题,但我不知道。我正在尝试编写一个简单的表单来发送带有上传文件的电子邮件(最终将扩展为实际有用的东西),但它根本不起作用。

电子邮件带有适当的正文,但未包含任何附件。我已经用文件上传表单尝试过这个,AddAttachments 链接到服务器上的文件,AddAttachments 指向 imgur 上的图像,但它们都不起作用;附件永远不会通过。我现在已经没有耐心了,有谁知道我做错了什么或者没有phpmailer的方法吗?

HTML 表单

<form action="xxxx.php" id="upload" method="post" name="upload">
<input id="fileToUpload" name="fileToUpload" type="file" /> 
<input type="submit" />
</form>

PHP 代码

require("../../../classes/class.phpmailer.php");
$mail = new PHPMailer();

$mail->From     = "xx@xx.co.uk";
$mail->FromName = "Uploader";
$mail->AddAddress("xx@xx.co.uk");

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddAttachment( $_FILES['fileToUpload']['tmp_name'], $_FILES['fileToUpload']['name'] );

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';

【问题讨论】:

标签: php phpmailer


【解决方案1】:

您正在使用一些旧示例和旧版本的 PHPMailer,所以我建议您 update to the latest。您还需要知道how to handle file uploads,这是您所缺少的。这里是the example bundled with PHPMailer

<?php
/**
 * PHPMailer simple file upload and send example
 */
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // Upload handled successfully
        // Now create a message
        // This should be somewhere in your include_path
        require 'PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'First Last');
        $mail->addAddress('whoto@example.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->msgHTML("My message body");
        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg = "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg = "Message sent!";
        }
    } else {
        $msg = 'Failed to move file to ' . $uploadfile;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
        <input type="submit" value="Send File">
    </form>
<?php } else {
    echo $msg;
} ?>
</body>
</html>

【讨论】:

  • 我正在努力在现有网站上做一个小的添加,该网站有相当数量的设置并且使用它的时间有限,因此将所有内容更改为不同版本的 phpmailer 听起来不太实用.
  • 我已尝试运行该示例,但收到响应“无法将文件移动到 /tmp/88d542d578b1fc7dae265b22296701a217343830LIhRZ0”
  • 所以您系统的临时文件夹已损坏,或者您的 Web 服务器没有对其的写入权限 - 修复它们,它会工作。更新就是将旧版本换成新版本并更改一行代码。
【解决方案2】:

查看您的表单,您的表单标签中没有设置 enctype="multipart/form-data" 。

此外,您需要在发送电子邮件之前进行文件附件检查以确保它确实已附加。例如,

if (isset($_FILES['uploaded_file']) &&
    $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
                         $_FILES['fileToUpload']['name']);
}

【讨论】:

  • 我已将 enctype 添加到表单中,并将附件检查添加到 php 中,但它仍然无法正常工作。我仍然收到相同的电子邮件,没有附件。
猜你喜欢
  • 2019-12-23
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多