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

Nick Davies
Nick Davies
7,972 Points

PHP error but no error code shown.

I have followed this course and watch the videos numerous times but have been unable tof ind out why this PHP function isn't sending any mail.

I am using XAMPP and when running the below PHP code:

<?php
        $app -> post('/about', function() use($app){
           $name = $app->request->post('user_name');
           $email = $app->request->post('user_email');
           $job = $app->request->post('user_job');
           $msg = $app->request->post('user_text');

    if(!empty($name) && !empty($email) && !empty($job) && !empty($msg)){
        $cleanName = filter_var($name, FILTER_SANITIZE_STRING);
        $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
        //$cleanJob = filter_var($job, FILTER_SANITIZE_STRING);
        $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
    } else {
        //error
        $app->redirect('/');
    }

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

    $message = \Swift_Message::newInstance();
    $message -> setSubject('Email from website');
    $message -> setFrom(array(
         $cleanEmail => $cleanName
    ));
    $message -> setTo(array('MyOfficalEmailAddressisHere'));
    $message -> setBody($cleanMsg);

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

    if($result>0){
        $app->redirect('/');
    } else {
        // Message fails to send
        $app->redirect('/');
    }


});

$app -> run();

?>

It takes me back (or even just to the top of the page) with the URL as: http://localhost/about?user_name=Nick&user_email=username%40live.co.uk&user_job=Ask+a+question&user_text=hi

I changed the page my errors would take me to my home so I can see a difference.

Any suggestions?

Edited to format code and add syntax highlighting.

1 Answer

I need some clarification of what is happening. Is there no error but also no email being sent? Or, is there some other error?

XAMPP does not have support for actually sending the email. There are ways to configure it, but I don't know what they are. It may be that you have to configure the PHP mailer to use something like Gmail to send the email. When I did this course it would not send for me, but I went to a live environment and it worked perfectly.

Nick Davies
Nick Davies
7,972 Points

I was not aware of that, I thought XAMPP mimicked the full features of a web server. I will check this in a live environment and see if it works. Thank you for your help