我正在尝试使用PHPMailer通过电子邮件发送我服务器上存在的文件。当我运行此代码时,我收到“无法访问文件”,并且电子邮件发送时没有附件。任何人都会指导我如何解决此问题
$checkyes=$_POST['check'];
$date = date('Y-m-d');
$my_path ="/data/data/www/fms/pricelists/$checkyes{$date}.xls";
include "class.phpmailer.php"; // include the class file name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxx";
$mail->Password = "xxxxxxx";
$mail->SetFrom("xxxxxxxxx");
$mail->Subject = $sub1;
$mail->Body = $text_mail;
$mail->AddAddress("xxx@xxxxxx.com");
$mail->AddAddress("xxxxxxx@gmail.com");
$mail->AddAttachment($my_file, $my_path);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
答案 0 :(得分:4)
这有效:
$my_path ="/data/data/www/fms/pricelists/example.xls";
include "class.phpmailer.php"; // include the class file name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxx";
$mail->Password = "xxxxxxx";
$mail->SetFrom("xxxxxxxxx");
$mail->Subject = $sub1;
$mail->Body = $text_mail;
$mail->AddAddress("xxx@xxxxxx.com");
$mail->AddAddress("xxxxxxx@gmail.com");
$mail->AddAttachment($my_path);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
答案 1 :(得分:2)
您以错误的方式添加了参数:
public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment')
所以这就是要走的路:
$mail->AddAttachment($my_path . $my_file);