PHPMailer默认配置SMTP

PHPMailer默认配置SMTP,php,smtp,phpmailer,Php,Smtp,Phpmailer,我知道如何在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 SM

我知道如何在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()


我可以不编辑class.phpmailer.php文件吗


最好不要自己编辑类文件,因为这样会使代码更难维护。

您也可以使用此挂钩:

 /**
     * Fires after PHPMailer is initialized.
     *
     * @since 2.2.0
     *
     * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
     */
    do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

从wp_邮件函数本身的源代码直接修改phpmailer类。

如果是关于wordpress的检查->wordpress\wp includes\class-phpmailer.php文件,我是否可以编辑class.phpmailer.php文件?默认情况下,它(至少是当前版本)以:
class PHPMailer开头{public$Version='5.2.9';public$Priority=3;public$CharSet='iso-8859-1';public$ContentType='text/plain';public$Encoding='8bit';public$ErrorInfo='';public$From='root@localhost“;public$FromName='Root User';
…那么,如果我将
$From
的值更改为,比方说,myname@example.com这方面的法典页面如下: