PHPMailer默认配置SMTP
发布时间:2020-12-13 18:18:05 所属栏目:PHP教程 来源:网络整理
导读:我知道如何在 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 = "you
我知道如何在
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(). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |