PHPMailer文件上传不起作用

时间:2017-12-19 21:43:15

标签: php forms file upload phpmailer

我的联系表单工作正常但我无法通过联系表单获取上传的文件。 HTML代码在这里;

<form id="contact_form" method="post" action="php/mail-advanced.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<label for="file"></label>
<button type="submit" id="submit">SEND MESSAGE</button></form>

这些代码在我的php / mail-advanced.php文件中;

<?php

date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.example.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "noreply@example.com";
$mail->Password = "pass";
$mail->setFrom('noreply@example.com', 'Example Message');
$mail->addReplyTo($email);
$mail->addAddress('example@gmail.com', 'Example Message');
$mail->Subject = $subject;
$mail->addAttachment($_FILES['file']);

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

如何在PHPMailer上获取带文件上传的文件?谢谢!

1 个答案:

答案 0 :(得分:-1)

您需要在PHP中传递文件的临时名称。它与实际上传的方式基本相同。

$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);