PHPMailer文件上传不起作用

问题描述 投票:0回答:1

我的联系表格工作正常但我无法通过联系表单获取上传的文件。 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 = "[email protected]";
$mail->Password = "pass";
$mail->setFrom('[email protected]', 'Example Message');
$mail->addReplyTo($email);
$mail->addAddress('[email protected]', 'Example Message');
$mail->Subject = $subject;
$mail->addAttachment($_FILES['file']);

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

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

php forms file upload phpmailer
1个回答
-1
投票

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

$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
© www.soinside.com 2019 - 2024. All rights reserved.