我正在尝试使用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>
尝试将
正确的路径传递到函数中,如下所示
function smtpmailer($to, $from, $from_name, $subject, $body, $newpath)
并称其为喜欢
smtpmailer('xxxxxxx@me.com', 'xxxxxxxx@gmail.com', 'Name', 'test mail message', 'Hello World!', $newpath);
确保运行 Apache 和 PHP 的用户/组至少对文件具有读取权限。
如果您在服务器上工作并以其他用户身份使用 sFTP/SSH 上传文件,则此问题很常见。
找出运行的用户和组 apache/php ,并授予该用户和组对您尝试附加的文件的读取访问权限。
我遇到了同样的问题。
我需要在我的 php 脚本中运行一个额外的命令,该命令move_uploaded_file() 到我的主机内部目录,然后现在我可以发送附件。
我的提示对我来说很有效:)