我使用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,所以一旦它處於工作狀態,我將清理它。 - 這是編輯我現在有一個新問題 -
你正在引用類中的$ mail,但它在那裏不可用。 –
請閱讀[變量範圍](http://php.net/manual/en/language.variables.scope.php)。 '$ mail'在你的函數中不存在。 – aynber