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