我使用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