2015-12-16 59 views
1

我正在使用phpmailer發送帶有pdf附件的電子郵件,但它發送垃圾郵件。我的電子郵件很簡單,只需要pdf附件。我試過谷歌上的所有內容,但不知道我錯過了。以下是我的代碼。在php中使用phpmailer進入垃圾郵件的電子郵件

$email_settings = ORMEmailSettings::order_by('id', 'desc')->limit(1)->first(); 
    $email_host = $email_settings->email_host; 
    $email_username = $email_settings->email_username; 
    $email_password = $email_settings->email_password; 

    require_once APPPATH . DIRECTORY_SEPARATOR . 'controllers/admin/smtp/class.phpmailer.php'; 
    if ($eventTicketId != 9999) { 
     $eventId = $this->events_model->getAttachment($eventTicketId[0]->event_id); 
     $emailSubject = $eventId[0]->title; 
    } else { 
     $emailSubject = "Here come the normal subject"; 
    } 
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->SMTPAuth = 'true'; 
    $mail->SMTPSecure = 'tls'; 
    $mail->SMTPKeepAlive = true; 
    $mail->Host = 'xxxx.xxmsrv.de'; 
    $mail->Port = 587; 
    $mail->IsHTML(true); 

    $mail->Username = "[email protected]marketing.com"; 
    $mail->Password = "xxxxxxxx2014*"; 
    $mail->SingleTo = true; 

    $mail->From = "[email protected]"; 
    $mail->FromName = "My Email"; 
    $mail->AddAddress('xxxxxxxx.de'); 
    if ($eventTicketId != 9999) { 
     $filename = "MPM-event" . $eventId[0]->id . "_.pdf"; 
     $filelocation = $_SERVER['DOCUMENT_ROOT'] . '/eventDocs'; 
     $fileNL = $filelocation . "/" . $filename; 
     $mail->AddAttachment($fileNL, $filename, $encoding = 'base64', $type = 'application/pdf'); 
    } 
    $mail->Subject = $emailSubject; 
    $mail->Body = '<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body style="margin: 0; padding: 0; font-family: Arial; font-size: 14px;  line-height: 17px;"> 
</body> 
</html>'; 

    if ($mail->send()) { 
     echo 'if'; 
     exit; 
    } else { 
     echo 'else'; 
     exit; 
    } 
+0

'$ mail-> SMTPAuth ='true';'應該是'$ mail-> SMTPAuth = true;'你不需要設置SingleTo,因爲你只是發送到一個地址。看起來您已經將代碼基於一個過時的示例,因此請確保您正在運行最新的PHPMailer。 – Synchro

回答

2

您的郵件進入垃圾郵件並不意味着您的代碼失敗 - 相反,它顯然工作!問題在於接收服務器不信任您的站點,因此它將傳入的電子郵件標記爲垃圾郵件,以便最終用戶永遠不會看到它。

該解決方案可能不是基於代碼的。相反,您應該爲您的域名添加SPF記錄。只要你有一個有用的網絡主機,這並不難。如果這是你第一次這樣做,你應該考慮使用像http://www.spfwizard.net這樣的東西,它爲你做了所有的辛苦工作。

+0

正如你在我的問題中看到的,我發送pdf作爲附件,我剛剛發現,如果我不發送附件,那麼它會轉到收件箱,但是當附件是它們時,它會進入垃圾郵件。 –

+0

同樣,這並不意味着你的代碼不能正常工作,它只是意味着服務器認爲你的依戀很腥。嘗試SPF,我相信它會被清除。 – TwoStraws

+0

我建議您使用權威的http://www.kitterman.com/spf/validate.html來創建和測試您的SPF。 – Synchro

相關問題