【发布时间】:2012-07-02 02:08:01
【问题描述】:
我正在尝试使用 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>
【问题讨论】:
-
据我所知,您永远不会使用您的
$filename变量。这是故意的吗?此外,您不会将$newpath作为参数传递给您的函数。 -
@Dan 好吧,当我使用 AddAttachment 时,您可以指定一个文件名,以便在附加后重命名文件。我目前没有使用它,因为我首先需要功能。我应该将它作为参数传递吗?
-
我在您的脚本中没有看到可以从文件系统读取文件的任何其他位置。是否缺少代码?代码的哪一部分会抛出该错误消息?
-
@Dan 我不确定,我认为它是 AddAttachment 行,因为我已将其他所有内容都注释掉,当它到达该行时,会产生错误。
-
在该行之前尝试
echo $newpath;。 AddAttachment 可能正在尝试附加您的文件,但您将其传递给一个空字符串。
标签: php html email phpmailer email-attachments