dops57958 2017-04-28 12:31 采纳率: 0%
浏览 60

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

I have written this code to send mail through an HTML form using php mailer, but after clicking on the submit button, it shows me this error "Could Not Access File". I have tried almost everything but it doesnt seem to be working. If anyone can help me out here that would be great.

FORM Code :

 <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 file

<?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();

}

?>
  • 写回答

1条回答 默认 最新

  • dongshou6788 2018-04-01 19:00
    关注

    Your input file named "my_file". Look:

    But you addressed $_FILES from named index "file":

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

    Correct code:

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

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算