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
}
You’re getting the error because
$mailat the bottom of your code is not an object. You have this snippet of code:However,
New_Mail()doesn’t actually return anything, so$mailis an empty variable.One way to fix this is to return the
$mailobject inNew_Mail().Another note: be careful when choosing variable names. You’re using the varname
$mailwithin theNew_Mail()function and outside of the function. This is perfectly fine, but just remember that the$mailobject from withinNew_Mail()is no longer in scope by the time you callSend(). That’s why PHP is throwing up.