【发布时间】:2017-02-13 02:03:09
【问题描述】:
我正在尝试设置一个处理多个文件附件的联系表单。我正在使用 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>
任何帮助将不胜感激!
谢谢。
【问题讨论】:
标签: php phpmailer email-attachments