无法访问phpmailer函数中的文件错误



我已经编写了此代码以使用php mailer通过HTML表单发送邮件,但是单击"提交"按钮后,它显示了此错误"无法访问文件"。我尝试了几乎所有事情,但似乎没有起作用。如果有人可以在这里帮助我。

表格代码:

 <form name="contactform" role="form" name="contactForm" action="send_form_careers.php" method="post" novalidate enctype="multipart/form-data">
                    <div class="col-xs-12">
                        <label for="name">Full Name</label><br>
                        <input type="text" id="name" name="name" class="text" required><br><br>
                        <label for="phone">Phone</label><br>
                        <input type="tel" id="phone" name="phone" class="text" required><br><br>
                        <label for="email">Email</label><br>
                        <input type="email" id="email" name="email" class="text" required><br><br>          
                        <input type="file" name="my_file" id="my_file" class="inputfile" required />
                        <label for="file">Upload Your Resume</label>
                        <div class="button" style="text-align:center">
                            <input type="submit" class="btn btn-read" value="Submit" name="submit">
                        </div>
                   </div>
                   </form>

php文件

<?php 
include('phpmailer/class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$to = "giteshnnd@gmail.com";
$mail->AddAddress($to);
$mail->From       = $_POST['email'];
$mail->FromName   = $_POST['name'];
$mail->Subject  = "Test Email using PHP";
$body             = "<table>
                         <tr>
                            <th colspan='2'>This Sample Mail</th>
                         </tr>
                         <tr>
                            <td style='font-weight:bold'>Name :</td>
                            <td>".$_POST['name']."</td>
                         </tr>
                         <tr>
                          <td style='font-weight:bold'>E-mail : </td>
                          <td>".$_POST['email']."</td>
                        </tr>
                        <tr>
                          <td style='font-weight:bold'>Phone : </td>
                          <td>".$_POST['phone']."</td>
                        </tr>
                        <tr>
                          <td style='font-weight:bold'>Message : </td>
                          <td>".$_POST['message']."</td>
                        </tr>
                     <table>";
$body             = preg_replace('/\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);
$mail->IsSMTP();                           // tell the class to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP server port
//$mail->Host       = "mail.yourdomain.com"; // SMTP server
//$mail->Username   = "name@domain.com";     // SMTP server username
//$mail->Password   = "password";            // SMTP server password
$mail->IsSendmail();  // tell the class to use Sendmail
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 80; // set word wrap
$mail->AddAttachment($_FILES['file']['tmp_name'],
                     $_FILES['file']['name']);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>

您的输入文件名为" my_file"。看:

但是您从命名索引"文件"中解决了$_FILES

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

正确的代码:

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

最新更新