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没有定义:
我尝试过返回,我尝试过争论。这将简化事情,但我无法让它工作。
九州编程