微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHPMailer默认配置SMTP

我知道如何在 PHPMailer中使用SMTP:
$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

它工作正常.但我的问题是:

如何配置PHPMailer认使用这些设置,以便每次我想发送邮件时都不必指定它们?

创建一个函数,并包含/使用它.
function create_PHPmailer() {
  $mail             = new PHPMailer();
  $mail->IsSMTP(); // telling the class to use SMTP
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  return $mail;
}

调用create_PHPmailer()来创建一个新的PHPMailer对象.

或者您可以派生自己的子类,它设置参数:

class MyMailer extends PHPMailer {
  public function __construct() {
    parent::__construct();
    $this->IsSMTP(); // telling the class to use SMTP
    $this->SMTPAuth   = true;                  // enable SMTP authentication
    $this->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $this->Username   = "yourname@yourdomain"; // SMTP account username
    $this->Password   = "yourpassword";        // SMTP account password
  }
}

并使用新的MyMailer().

原文地址:https://www.jb51.cc/php/135303.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐