问题
I am trying to email a file that exists on my server using PHPMailer. When I run this code, I get "Could not access file" and the email sends without the attachment... what is wrong here??
<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>
回答1:
Try passing the correct path into your function like this
function smtpmailer($to, $from, $from_name, $subject, $body, $newpath)
and calling it like
smtpmailer('xxxxxxx@me.com', 'xxxxxxxx@gmail.com', 'Name', 'test mail message', 'Hello World!', $newpath);
回答2:
Make sure the user/group that Apache and PHP are running under have at least READ permissions on the file.
This issue is common if you're working on a server and upload files using sFTP/SSH as a different user.
Find out what user and group apache/php are running under and give that user and group read access to the file you are trying to attache.
回答3:
I was having the same problem.
I needed to run an extra command into my php script that move_uploaded_file() to an internal directory from my host home, then so now I could send the attachment.
That my tip that works well for me :)
来源:https://stackoverflow.com/questions/11280485/phpmailer-could-not-access-file