我已编写此代码以使用php邮件程序通过HTML表单发送邮件,但在单击提交按钮后,它会显示此错误"无法访问文件"。我几乎尝试了所有东西,但它似乎没有起作用。 如果有人能在这里帮助我,那就太好了。
FORM代码:
<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();
}
?>
答案 0 :(得分:0)
您的输入文件名为&#34; my_file&#34;。看:
但是您从命名索引&#34;文件&#34;:
中解决了$_FILES
$mail->AddAttachment($_FILES['file']['tmp_name'],
$_FILES['file']['name']);
正确的代码:
$mail->AddAttachment($_FILES['my_file']['tmp_name'],
$_FILES['my_file']['name']);