2017-05-31 73 views
-2

我使用PHPMailer的5.2版本我使用PHPMailer的,但是當我調用Send()函數,我真的不接收電子郵件

我已經編輯了一些微妙的信息張貼緣故

try { 

class mailer { 
    private $email_to; 
    private $email_subject; 
    private $email_from; 
    private $email_headers; 
    private $email_html_message; 
    private $email_text_message; 
    private $mail_object; 
    public function __construct($persist = false) { 
     $this->email_to = ''; 
     $this->email_subject = ''; 
     $this->email_from = ''; 
     $this->email_html_message = ''; 
     $this->email_text_message = ''; 
     $this->email_headers = ''; 
    } 
    // Adds the Email Address to be sent to, to the mailer Object 
    public function add_to($email) { 
     $this->email_to = $email; 
     // $mail->AddAddress ($email); 
    } 
    public function add_from($email) { 
     $this->email_from = $email; 
     // $mail->SetFrom ($email); 
    } 
    public function add_subject($subject) { 
     // $mail->Subject ($subject); 
    } 
    public function add_headers($headers) { 
     // $this->email_headers = [$headers[0]] = $headers[1]; 
     $mail->addCustomHeader ($headers); 
    } 
    public function add_html_message($message) { 
     $this->email_html_message = $message; 
    } 
    public function add_text_message($message) { 
     $this->email_text_message = $message; 
    } 
    public function send() { 
     $mime_boundary = 'Multipart_Boundary_x' . md5 (time()) . 'x'; 

     $body = "This is a multi-part message in mime format.\n\n"; 

     $body .= "--$mime_boundary\n"; 
     $body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n"; 
     $body .= "Content-Transfer-Encoding: 7bit\n\n"; 
     $body .= $this->email_text_message; 
     $body .= "\n\n"; 

     $body .= "--$mime_boundary\n"; 
     $body .= "Content-Type: text/html; charset=\"UTF-8\"\n"; 
     $body .= "Content-Transfer-Encoding: 7bit\n\n"; 
     $body .= $this->email_html_message; 
     $body .= "\n\n"; 

     $body .= "--$mime_boundary--\n"; 

     $headers ["Reply-To"] = $this->email_from; 
     $headers ["From"] = "Law Space Match <[email protected]>"; 
     $headers ["IME-Version"] = '1.0'; 
     $headers ["Content-Type"] = 'multipart/alternative; boundary="' . $mime_boundary . '"'; 
     $headers ["Content-Transfer-Encoding"] = '7bit"'; 
     $headers ["Return-path"] = "[email protected]"; 
     $headers ['Subject'] = $this->email_subject; 
     $headers ["X-Sender-IP"] = $_SERVER ['SERVER_ADDR']; 
     $headers ["Date"] = date ('n/d/Y g:i A'); 
     $headers ["To"] = $this->email_to; 

     $mail = new PHPMailer(); 
     $mail->IsSMTP(); 
     $mail->SMTPDebug = 2; 
     $mail->SMTPAuth = true; 
     $mail->SMTPSecure = "tls"; 
     $mail->Host = "smtp.gmail.com"; 
     $mail->Port = 587; 
     $mail->Username = "[email protected]"; 
     $mail->Password = "*********"; 
     $mail->SetFrom("[email protected]", "John Doe"); 
     $mail->AddAddress("[email protected]", "John Doe"); 
     $mail->Subject = "test"; 
     $mail->MsgHTML = $body; // or MsgBody? 
     $mail->Send(); 
     // return $this->mail_object->send ($this->email_to, $headers, $body); 
    } 
    } 
} catch (phpmailerException $e) { 
echo $e->errorMessage(); 
} catch (Throwable $e) { 
echo $e->getMessage(); 
} 

我已通過不同的來源檢查了SMTP的憑據,它處於工作狀態。然而這實際上不會發送給我。 是的,我知道有很多我沒有使用的垃圾代碼。這是因爲我將一箇舊的郵件系統轉移到PHPMailer,所以一旦它處於工作狀態,我將清理它。 - 這是編輯我現在有一個新問題 -

+0

你正在引用類中的$ mail,但它在那裏不可用。 –

+0

請閱讀[變量範圍](http://php.net/manual/en/language.variables.scope.php)。 '$ mail'在你的函數中不存在。 – aynber

回答

1

$mail變量不會在mailer類的send()方法的範圍存在,則可能需要實例化對象PHPMailer在構造函數中,像下面的例子:

class mailer { 
    [...] 
    private $mail; 

    public function __construct($persist = false) { 
     [...] 
     $this->mail = new PHPMailer(); 
    } 
    [...] 
    public function send() { 
     [...] 

     $this->mail->MsgHTML = $body; // or MsgBody? 

     $this->mail->Send(); 
     // return $this->mail_object->send ($this->email_to, $headers, $body); 
    } 
} 

您也可以將PHPMailer實例傳遞給您的類的構造函數。

+0

請注意,您應該用'try/catch'塊包裝'$ this-> mail-> Send()'調用,而不是定義班上。我沒有試圖解決這些問題,只是爲了回答你的問題。 – 2017-05-31 15:02:57

+0

謝謝。我在發佈這個問題後才意識到這一點。有趣的是,盯着它30分鐘後會發生什麼。 PS。謝謝 – OverBakedToast

-1

你的類不應該在try-catch方法內部聲明。考慮一個

include_once 'pathToMailerClass'; 
+0

這是不對的,問題來自於'$ mail'不在類的範圍內,因爲它在外部聲明。 – 2017-05-31 14:57:57

+0

這是對的,但是......我保留我關於刪除那個垃圾的評論 –

0

這條線:

$mail = new PHPMailer(); 

是超出了範圍,因爲它是在類郵包之外定義。 導致創建空郵件類爲$ mail。

+0

「導致爲空郵件創建一個空類。」:不,我想你誤解了回覆。一個'PHPMailer'的實例存在於變量'$ mail'中,但是由於變量在PHP中的作用範圍,在'mailer :: send'方法的範圍內不存在名爲'$ mail'的變量。 – 2017-05-31 15:08:05

相關問題