问题
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())
.
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
}
回答1:
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);
However, New_Mail()
doesn't actually return anything, so $mail
is an empty variable.
One way to fix this is to return the $mail
object in New_Mail()
.
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.
回答2:
show var_dump($mail) before calling $mail->Send(); May be you forgot to include the class? for example require_once($_SERVER['DOCUMENT_ROOT'].'/lib/phpmailer/class.phpmailer.php');
来源:https://stackoverflow.com/questions/10221778/phpmailer-fatal-error-call-to-a-member-function-send-on-a-non-object