2014-01-28 109 views
0

我在我的服務器上保存了excel文件夾(注意excel正在保存在服務器中),我試圖發送帶有保存在我的服務器上的excel附件的電子郵件,但是我沒有' t收到附件的電子郵件(即:我收到沒有附件的電子郵件),下面是我的代碼,請指導我如何做到這一點。如何發送帶有excel附件的電子郵件

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('/data/data/www/ms/pricelists/newxcelexample.xls'); 

$my_path ="/data/data/www/ms/pricelists/newxcelexample.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.xichlomobile.com"; 
$mail->Port = "25"; // or 587 
$mail->IsHTML(true); 
$mail->Username = "************"; 
$mail->Password = "**********"; 
$mail->SetFrom("**************"); 
$mail->Subject = Test; 
$mail->Body = "Test"; 
$mail->AddAddress("*********"); 
$mail->AddAttachment($my_path); 

if(!$mail->Send()){ 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else{ 
echo "<span style='font-size:45px;color:#000000;'>Message has been sent</span><br><br>"; 
} 

回答

-1

你是否控制過該文件真的存在並被正確保存? Web服務器有權讀取文件(通常由用戶www-data讀取)?

也許嘗試也文件名提供給AddAttachment:

$mailer->AddAttachment($my_path, basename($my_path)); 
// or 
$mailer->AddAttachment($my_path, basename($my_path), "quoted-printable", "application/vnd.ms-excel") ; 

如果您使用的PHPMailer的新版本,你也可以使用異常,看看發生了什麼。也許這並不妨礙郵件外發,而是防止附加文件。

try { 
    // preparing the email just before the mail->Send() 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 

嘗試發送一次另一個文件,例如a * .txt或* .zip(檢查是否對可發送的文件類型有限制,這對於excel文件來說不太可能)。

相關問題