【问题标题】:could not access file error in phpmailer functionphpmailer 函数中无法访问文件错误
【发布时间】:2017-04-28 12:31:11
【问题描述】:

我已编写此代码以使用 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();

}

?>

【问题讨论】:

  • 我不明白,如何解决?
  • 检查你的文件路径
  • 文件路径正确。
  • 首先检查您的文件路径。 1. 你的表单操作是action="send_form_careers.php" 2. 所以你的send_form_careers.php 将与你的表单文件是同一个文件夹 3. send_form_careers.php 文件你include('phpmailer/class.phpmailer.php'); 所以你的 send_form_careers.php 和这个文件结构将是 phpmailer/ 4. 检查你的文件权限是 644

标签: php email


【解决方案1】:

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

但是您从命名索引“文件”中寻址$_FILES

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

正确代码:

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

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2011-10-08
    相关资源
    最近更新 更多