我正在尝试设置一个处理多个文件附件的联系表单。我正在使用PHPMailer并从PHPMailer example构建以下脚本以附加多个文件。
以下脚本效果很好,直到附件超过100KB。如果文件大于100KB,则在附加时会跳过该文件。仅附加并发送小于100KB的文件。
我看到这个StackOverflow 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>
非常感谢任何帮助!
感谢。
答案 0 :(得分:1)
您错过了表单中的MAX_FILE_SIZE
选项,该选项无法提供帮助,默认为100k,与您看到的完全匹配。 See the docs
The send_file_upload example provided with PHPMailer显示了如何正确设置。
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">