Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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";
}
?>