2017-05-31 78 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

相关问题