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 Building Websites with PHP Contact Form & Sending Email Sending Our Email

William McManus
William McManus
14,819 Points

Email wont send ( Fatal error: Call to a member function send() on a non-object )

I an getting this error when I try to submit this form / email.

I've done enough research to figure out that the error means that PHP doesn't recognize $message as an object, but I can not figure out why. If anyone has any input please let me know. Thanks!

    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail');
    $mailer - \Swift_Mailer::newInstance($transport);

    $message = \Swift_Message::newInstance();
    $message->setSubject('Hello From Liam');
    $message->setFrom(array(
        $cleanEmail => $cleanName

    ));
    $message->setTo(array('billy@oh-billy.com'));
    $message->setBody('$cleanMsg');

    $result = $mailer->send($message);

    if($result > 0) {
        $app->redirect('/');

    } else {

        $app->redirect('/contact');
    }

});

when I run the code it says the error is on this line specifically:

$result = $mailer->send($message);

because I am calling a function (send()) on a non object ($message). so..... Help!

1 Answer

Erik McClintock
Erik McClintock
45,783 Points

William,

Your issue may be a simple syntax error:

On line two of the code that you pasted here, where you declare your $mailer variable, you have a hyphen in place of an equals sign when you're attempting to assign a value to it.

You have:

$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail');
$mailer - \Swift_Mailer::newInstance($transport); // notice that you say "$mailer - \Swift_Mailer" instead of "$mailer = \Swift_Mailer"

That would make sense of the error message that you're getting, which states that you're trying to call the send() method on a non-object, as $mailer isn't assigned to anything, and would thus be undefined.

Try replacing the hyphen with an equals sign, like this...

$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail');
$mailer = \Swift_Mailer::newInstance($transport); // notice we now say "$mailer = \Swift_Mailer"

...and see if you make any forward progress!

*SIDE NOTE: to make sure you're following along with everything correctly, it's important to understand that the object you're attempting to call the send() method on is actually $mailer, and $message is the argument that you're passing into the send() method.

$mailer->send( $message ), when rewritten with their types, would read: object->method( argument ).

Erik

William McManus
William McManus
14,819 Points

Hey Eric,

------Edit------ adding -bs worked :) Thanks again! ------Edit------

Thanks for catching that, I was going insane, should probably take a break.

It did let me make some forward progress, right into another error!

Message: Unsupported sendmail command flags [0]. Must be one of "-bs" or "-t" but can include additional flags.

I saw the part whereHampton talked about having to add -bs to his code but I thought that was because he was on the treehouse server. I am doing this all locally on my machine. I did try to ad the -bs to my code ( the first line of code shown here), to no avail. any ideas?

Erik McClintock
Erik McClintock
45,783 Points

Great, sounds like you've found the other bug and you're moving along!

Happy coding!

Erik