我想使用一段代码初始化电子邮件,但每次将代码移动到某个函数时,脚本会在发送电子邮件之前停止执行。当我使用此功能加载网页时,它工作正常并发送相应的电子邮件:
function SendAdminIntimationEmail(&$formvars)
{
if(empty($this->admin_email))
{
return false;
}
$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';
//Tell PHPMailer to use SMTP
$mailer->isSMTP();
//Set the hostname of the mail server
$mailer->Host = "test.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mailer->Port = 25;
//Whether to use SMTP authentication
$mailer->SMTPAuth = false;
//Username to use for SMTP authentication
$mailer->Username = "test@test.com";
//Password to use for SMTP authentication
$mailer->Password = "pword";
//Set who the message is to be sent from
$mailer->setFrom('test@test.com', 'test');
//Set an alternative reply-to address
$mailer->addReplyTo('test@test.com', 'First Last');
$mailer->AddAddress($this->admin_email);
$mailer->Subject = "New registration: ".$formvars['name'];
$mailer->From = $this->GetFromAddress();
$mailer->Body ="A new user registered at ".$this->sitename."\r\n".
"Name: ".$formvars['name']."\r\n".
"Email address: ".$formvars['email']."\r\n".
"UserName: ".$formvars['username'];
if(!$mailer->Send())
{
return false;
}
return true;
}
但是当我将一段代码移动到一个单独的函数并尝试传递该函数的$ mailer对象时,该网页会加载一个空白页并且不会发送电子邮件。非工作代码:
function SendAdminIntimationEmail(&$formvars)
{
if(empty($this->admin_email))
{
return false;
}
$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';
InitSMTPMailer($mailer);
$mailer->AddAddress($this->admin_email);
$mailer->Subject = "New registration: ".$formvars['name'];
$mailer->From = $this->GetFromAddress();
$mailer->Body ="A new user registered at ".$this->sitename."\r\n".
"Name: ".$formvars['name']."\r\n".
"Email address: ".$formvars['email']."\r\n".
"UserName: ".$formvars['username'];
if(!$mailer->Send())
{
return false;
}
return true;
}
function InitSMTPMailer($mailer)
{
//Tell PHPMailer to use SMTP
$mailer->isSMTP();
//Set the hostname of the mail server
$mailer->Host = "test.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mailer->Port = 25;
//Whether to use SMTP authentication
$mailer->SMTPAuth = false;
//Username to use for SMTP authentication
$mailer->Username = "test@test.com";
//Password to use for SMTP authentication
$mailer->Password = "pword";
//Set who the message is to be sent from
$mailer->setFrom('test@test.com', 'test');
//Set an alternative reply-to address
$mailer->addReplyTo('test@test.com', 'test');
return $mailer;
}
如果它有用,则此函数始终调用此函数:
function RegisterUser()
{
if(!isset($_POST['submitted']))
{
return false;
}
$formvars = array();
if(!$this->ValidateRegistrationSubmission())
{
return false;
}
$this->CollectRegistrationSubmission($formvars);
if(!$this->SaveToDatabase($formvars))
{
return false;
}
if(!$this->SendUserConfirmationEmail($formvars))
{
return false;
}
$this->SendAdminIntimationEmail($formvars);
return true;
}
添加使脚本无法正常工作的功能后,看起来RegisterUser()在$ this-> SendAdminIntimationEmail($ formvars)中失败; line,因为SendUserConfirmationEmail($ formvars)函数仍然发送电子邮件。
我是PHP的新手,很抱歉,如果这很简单,但是我通过引用传递对象或者制造某些东西的值做错了吗?
答案 0 :(得分:0)
问题是您以错误的方式致电InitSMTPMailer
。
SendAdminIntimationEmail
和InitSMTPMailer
是类方法,但您将InitSMTPMailer
称为全局函数(或者只是忘记$this->
)。
试试这个:
function SendAdminIntimationEmail(&$formvars)
{
if(empty($this->admin_email))
{
return false;
}
$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';
$this->InitSMTPMailer($mailer);
$mailer->AddAddress($this->admin_email);
$mailer->Subject = "New registration: ".$formvars['name'];
$mailer->From = $this->GetFromAddress();
$mailer->Body ="A new user registered at ".$this->sitename."\r\n".
"Name: ".$formvars['name']."\r\n".
"Email address: ".$formvars['email']."\r\n".
"UserName: ".$formvars['username'];
if(!$mailer->Send())
{
return false;
}
return true;
}