PHPMailer“无法访问文件:”

时间:2012-07-01 08:12:13

标签: php html email phpmailer email-attachments

我正在尝试使用PHPMailer通过电子邮件发送我服务器上存在的文件。当我运行此代码时,我得到“无法访问文件”,电子邮件发送时没有附件......这里有什么问题?

<html>
<title>Email Sent!</title>
<?php 
        include("menu.php");
        include("sqlconnect.php");
        require_once('../PHPMailer/class.phpmailer.php');

        $path = $_POST['path'];
        $filename = $_POST['filename'];
        $newpath = "Library/WebServer/Documents/Inventory/".$path;

define('GUSER', 'xxxxxxx@gmail.com'); // GMail username
define('GPWD', 'xxxxxxx'); // GMail password


function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    $attachtest = $mail->AddAttachment($newpath);   
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}

smtpmailer('xxxxxxx@me.com', 'xxxxxxxx@gmail.com', 'Name', 'test mail message', 'Hello World!');

?>
</html>

3 个答案:

答案 0 :(得分:1)

尝试将正确的路径传递到您的函数中

function smtpmailer($to, $from, $from_name, $subject, $body, $newpath)

并将其称为

 smtpmailer('xxxxxxx@me.com', 'xxxxxxxx@gmail.com', 'Name', 'test mail message', 'Hello World!', $newpath);

答案 1 :(得分:0)

确保运行Apache和PHP的用户/组至少具有该文件的READ权限。

如果您正在使用服务器并使用sFTP / SSH作为其他用户上传文件,则此问题很常见。

找出正在运行的用户和组apache / php,并为该用户和组提供对您尝试附加的文件的读取权限。

答案 2 :(得分:0)

我遇到了同样的问题。

我需要在我的php脚本中运行一个额外的命令,将move_uploaded_file()从我的主机家中运行到内部目录,然后我就可以发送附件了。

我的提示对我有用:)