PHPMAILER 将电子邮件配置分离到不同的函数中
PHPMAILER Separate email config into different function
PHPMAILER 在我的网站上运行正常。我想做的是将配置部分分成一个单独的函数,这样当我创建不同的响应电子邮件时,我需要做的就是在不同的响应函数中调用 emailConfig()
函数。
function continuedInquiry() {
//config portion I want to separate
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
/**
*rest of the phpmailer code
*/
$mail->send();
notify();
}
function notify() {
//notification email
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
/**
*rest of the phpmailer code
*/
}
这可以正常工作,但是因为我使用了多个邮件程序,所以我想将配置部分分成一个单独的函数,emailConfig()
,如下所示:
function emailConfig() {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
}
并在其他邮件程序函数中调用它:
function continuedInquiry() {
emailConfig();
/**
*rest of the phpmailer code
*/
$mail->send();
notify();
}
//and so on
但我一直收到错误提示 $mail 未定义:
我试过return,我试过参数。这会简化事情,但我无法让它工作。
您可能会发现使用子类来配置它更容易,如下所示:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class myMailer extends PHPMailer
{
public function __construct($exceptions = null)
{
$this->isSMTP();
$this->SMTPDebug = SMTP::DEBUG_OFF;
$this->Host = 'smtp.gmail.com';
$this->Port = 587;
$this->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->SMTPAuth = true;
$this->Username = 'example@gmail.com';
$this->Password = 'password';
parent::__construct($exceptions);
}
}
然后在你的脚本中你可以这样做:
$mail = new myMailer(true);
它会完成所有配置,可以使用了。
就是说,将 "secrets" 之类的密码从代码中移到外部环境变量或配置文件中是个好主意,这样您就不会最终将密码推入 git回购
PHPMAILER 在我的网站上运行正常。我想做的是将配置部分分成一个单独的函数,这样当我创建不同的响应电子邮件时,我需要做的就是在不同的响应函数中调用 emailConfig()
函数。
function continuedInquiry() {
//config portion I want to separate
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
/**
*rest of the phpmailer code
*/
$mail->send();
notify();
}
function notify() {
//notification email
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
/**
*rest of the phpmailer code
*/
}
这可以正常工作,但是因为我使用了多个邮件程序,所以我想将配置部分分成一个单独的函数,emailConfig()
,如下所示:
function emailConfig() {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';
}
并在其他邮件程序函数中调用它:
function continuedInquiry() {
emailConfig();
/**
*rest of the phpmailer code
*/
$mail->send();
notify();
}
//and so on
但我一直收到错误提示 $mail 未定义:
我试过return,我试过参数。这会简化事情,但我无法让它工作。
您可能会发现使用子类来配置它更容易,如下所示:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class myMailer extends PHPMailer
{
public function __construct($exceptions = null)
{
$this->isSMTP();
$this->SMTPDebug = SMTP::DEBUG_OFF;
$this->Host = 'smtp.gmail.com';
$this->Port = 587;
$this->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->SMTPAuth = true;
$this->Username = 'example@gmail.com';
$this->Password = 'password';
parent::__construct($exceptions);
}
}
然后在你的脚本中你可以这样做:
$mail = new myMailer(true);
它会完成所有配置,可以使用了。
就是说,将 "secrets" 之类的密码从代码中移到外部环境变量或配置文件中是个好主意,这样您就不会最终将密码推入 git回购