PHP Build a Basic PHP Website (2018) Enhancing a Form Setting an Error Message Variable

send() method is not called

The send() method of PHP mailer object is not called. It is just used inside an if condition. To execute the action of the method we should call it like PHPMailer->send(). Am I right ?

Can anyone explain this to me please?

3 Answers

Murat Hasdemir
Murat Hasdemir
Front End Web Development Techdegree Graduate 20,967 Points

if method is used for trying an action like sending mail if you use like that it will try to send that mail and give back process result like "yup I did send this mail" or "nope I can't send it" and you make you write less code to achieve same result and trust me when you building a real life project those little saving become really important things :)

Lucas Santos
Lucas Santos
19,315 Points

It would be easier to tell if you post your code but from what you've written I think I may know your problem.

You would not call:

PHPMailer->send()

You would first have to instantiate the PHPMailer object then call it like so:

$mail = new PHPMailer;

$mail.send()

Here is the github page

I know that I should assign it to $mail variable. and my form works perfectly but I don't understand how it is possible. In the video Mrs.Alena did not call the method like u did ($mail->send()) instead she used inside a condition like this if($mail->send()) { header("location: suggest.php?status=thanks"); } and the form works fine.

She was suppose to call the method before using it inside the condition. right ?

Lucas Santos
Lucas Santos
19,315 Points

Yup that's another way of doing it and the most common with PHPMailer.

Heres and example:

<?php
$mail = new PHPMailer;

$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

$mail->addAddress("recipient1@example.com", "Recipient Name");
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}
?>