无法访问文件:无法访问文件:无法访问文件:PHP 邮件程序问题

Could not access file: Could not access file: Could not access file: PHP Mailer issue

代码在没有附件的情况下运行良好。附件代码在另一个页面上工作正常任何人都可以帮助解决这个问题吗?

require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port     = 465;  
$mail->Username = "info@guildsconnect.org";
$mail->Password = "Guildsconnect";
$mail->Host     = "webs10rdns1.websouls.net";
$mail->Mailer   = "smtp";
$mail->SetFrom($contact_email, $contact_name);
$mail->AddReplyTo($contact_email, $contact_name);
$mail->AddAddress("hashimbhatti906@gmail.com"); 
$mail->Subject = $sub1;
$mail->WordWrap   = 80;
$mail->MsgHTML($emailbodyis);

foreach ($_FILES["attachment"]["name"] as $k => $v) {
    $mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] );
}

$mail->IsHTML(true);

if(!$mail->Send()) {
    $_SESSION["error"] = "Problem in Sending Mail.";
} else {
    $_SESSION["success"] = "Mail Sent Successfully.";
}   

?>

首先,您使用的是非常旧且不受支持的 PHPMailer 版本,因此 upgrade。看起来您的代码也是基于一个非常古老的示例。

根据 the PHP docs.

,您没有安全地处理文件上传,没有在尝试使用之前验证上传的文件

PHPMailer 提供 an example that shows how to handle and upload and attach it correctly. The key parts are to use move_uploaded_file() 在使用前验证上传,而不是信任提供的文件名。解释一下这个例子:

    //Extract an extension from the provided filename
    $ext = PHPMailer::mb_pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
    //Define a safe location to move the uploaded file to, preserving the extension
    $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name'])) . '.' . $ext;

    if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
        //Now create a message
        $mail = new PHPMailer();
...
        //Attach the uploaded file
        if (!$mail->addAttachment($uploadfile, 'My uploaded file')) {
            $msg .= 'Failed to attach file ' . $_FILES['attachment']['name'];
        }
        if (!$mail->send()) {
            $msg .= 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            $msg .= 'Message sent!';
        }
    } else {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }

不需要自己设置MailerisSMTP().

已经为您完成了