我正在使用PHPMailer版本5.2
为了发帖,我编辑了一些微妙的信息:
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 <service@lawspacematch.com>";
$headers ["IME-Version"] = '1.0';
$headers ["Content-Type"] = 'multipart/alternative; boundary="' . $mime_boundary . '"';
$headers ["Content-Transfer-Encoding"] = '7bit"';
$headers ["Return-path"] = "mailer@lawspacematch.com";
$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 = "user@GMailSMTPdomain.com";
$mail->Password = "*********";
$mail->SetFrom( "sameTest@notGmailDomain.com", "John Doe" );
$mail->AddAddress( "sameTest@notGmailDomain.com", "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 :(得分:1)
$mail
类的send()
方法范围内不存在mailer
变量,您可能希望在构造函数中实例化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
实例传递给班级的构造函数。
答案 1 :(得分:0)
这一行:
$mail = new PHPMailer ();
超出了范围,因为它是在邮件程序类之外定义的。 导致$ mail的空类被创建。
答案 2 :(得分:-1)
不应在try-catch方法中声明您的类。考虑一下
include_once 'pathToMailerClass';