PHPMailer,致命错误:在非对象上调用成员函数Send() [英] PHPMailer, Fatal error: Call to a member function Send() on a non-object

查看:90
本文介绍了PHPMailer,致命错误:在非对象上调用成员函数Send()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误PHP Fatal error: Call to a member function Send() on a non-object.此错误与if(!$mail->Send())行有关.

I get this error PHP Fatal error: Call to a member function Send() on a non-object. This error concerns the line if(!$mail->Send()).

我在另一个论坛上搜索过,可能与在某个地方使用"load->"有关,但我不确定原因和方式.

I searched on another forum and it might be about using "load->" somewhere but I'm not sure why and how.

function New_Mail($email,$firstname,$surname,$body, $subject, $altBody, $wordwrap){

$mail             = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the server
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port

$mail->Username   = "xxxxxxx@gmail.com";  // GMAIL username
$mail->Password   = "xxxxx";            // GMAIL password

$mail->From       = "xxxxxxx@gmail.com";
$mail->FromName   = "Admin";

$mail->Subject    = $subject;
$mail->AltBody    =  $altBody;//Text Body
$mail->WordWrap   = $wordwrap; // set word wrap
$mail->AddAddress($email,$firstname." ".$surname);

$mail->MsgHTML($body);
$mail->AddReplyTo("replyto@yourdomain.com","Admin");
$mail->SMTPDebug = 1;

$mail->IsHTML(true); // send as HTML

}

$mail=New_Mail($email,$firstname,$surname,'This is the body','Welcome','this is the alternate body',100);

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
// nothing is displayed
}

推荐答案

您会收到错误消息,因为代码底部的$mail不是对象.您有以下代码段:

You're getting the error because $mail at the bottom of your code is not an object. You have this snippet of code:

$mail=New_Mail($email,$firstname,$surname,'This is the body','Welcome','this is the alternate body',100);

但是,New_Mail()实际上没有返回任何内容,因此$mail是一个空变量.

However, New_Mail() doesn't actually return anything, so $mail is an empty variable.

解决此问题的一种方法是在New_Mail()中返回$mail对象.

One way to fix this is to return the $mail object in New_Mail().

另一个注意事项:选择变量名称时要小心.您正在New_Mail()函数内部和函数外部使用变量名$mail.这样做很好,但请记住,在您调用Send()时,New_Mail()中的$mail对象不再在作用域内.这就是PHP抛出的原因.

Another note: be careful when choosing variable names. You're using the varname $mail within the New_Mail() function and outside of the function. This is perfectly fine, but just remember that the $mail object from within New_Mail() is no longer in scope by the time you call Send(). That's why PHP is throwing up.

这篇关于PHPMailer,致命错误:在非对象上调用成员函数Send()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆