PHP邮件程序不附加大于 100KB 的文件 - PHP

PHPMailer not attaching files larger than 100KB - PHP

我正在尝试设置一个处理多个文件附件的联系表。我正在使用 PHPMailer 并从 PHPMailer example 构建以下脚本以附加多个文件。

以下脚本在附件超过 100KB 之前效果很好。如果文件大于 100KB,附加时会跳过。仅附加并发送小于 100KB 的文件。

我看到这个 Whosebug question 看起来很有希望,但是我机器的 php.ini 文件中的值都设置为 32MB 或更高。

我正在使用 Mailgun 作为 SMTP 服务器,并且可以在日志中看到超过 100KB 的附件根本没有到达 Mailgun,所以它一定与这个脚本或我的 PHP环境。

谁能帮我解决这个问题?

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$host = 'smtp.mailgun.org';
$username = 'postmaster@domain.com';
$password = 'password';

$email_from = 'from@domain.com';
$email_to = 'to@domain.com';

$send = false;

$subject = "Quote Request from Website";

$name = addslashes(strip_tags($_POST['name']));
$email = addslashes(strip_tags($_POST['email']));
$project_type = addslashes(strip_tags($_POST['project_type']));
$message = addslashes(strip_tags($_POST['message']));

$htmlmessage = <<<MESSAGE
    <html>
        <head>
              <title>$subject</title>
        </head>

        <body>
            <p><strong>Name:</strong> $name</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Project Type:</strong> $project_type</p>
            <p><strong>Message:</strong> $message</p>
        </body>
    </html>
MESSAGE;

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Host = $host;
$mail->Port = 587;

$mail->setFrom($email_from, $name);
$mail->addAddress($email_to);
$mail->addReplyTo($email, $name);
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');

// Attach multiple files one by one
$total = count($_FILES['attachments']['tmp_name']);
echo $total;
for ($ct = 0; $ct < $total; $ct++)
{
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['attachments']['name'][$ct]));
    $filename = $_FILES['attachments']['name'][$ct];
    if (move_uploaded_file($_FILES['attachments']['tmp_name'][$ct], $uploadfile)) {
        echo $filename;
        $mail->addAttachment($uploadfile, $filename);
    } else {
        $msg .= 'Failed to move file to ' . $uploadfile;
        echo $msg;
    }

    // $name = $_FILES['attachments']['name'][$ct];
    // $path = $_FILES['attachments']['tmp_name'][$ct];
    // echo $name . ' - ' . $path . '<br>';
    // $mail->addAttachment($path, $name);
}

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body    = $htmlmessage;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

表格:

<form action="contact/quote.php" method="post" id="quote-form" class="validate" role="form" enctype="multipart/form-data">

  <label>Name</label>
  <input type="text" name="name" id="name" required>

  <label>Email</label>
  <input type="email" name="email" id="email" required>

  <label>Project Type</label>
  <select name="project_type" id="project_type" required>
    <option value="" selected>Please Select</option>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
  </select>

  <label>Upload Files</label>
  <input multiple="multiple" type="file" name="attachments[]" value="">

  <label>Message</label>
  <textarea name="message" id="message" rows="5" required></textarea>

  <button type="submit" id="submit">Submit</button>
</form>

如有任何帮助,我们将不胜感激!

谢谢。

您的表单中缺少 MAX_FILE_SIZE 选项,这对您没有帮助,它默认为 100k,与您所看到的完全匹配。 See the docs.

The send_file_upload example provided with PHPMailer 展示了如何正确设置它。

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">