我正在尝试使用 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>