Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Murat Hasdemir
Front End Web Development Techdegree Graduate 20,968 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";
}
?>