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 trialEduardo Mejía
36,816 PointsUnsent email.
I'm working with MAMP. When I submit the form, it redirect me to home page but I open my inbox and the mail is not there. This is my code:
<?php
$app->post('/contact', function () use($app){
$name = $app->request->post("name");
$email = $app->request->post("email");
$msg = $app->request->post("msg");
if (!empty($name) && !empty($email) && !empty($msg)) {
$cleanName = filter_var($name, FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
//echo $cleanName . "/" . $cleanEmail . "/" . $cleanMsg;
//exit();
} else {
//Message the user that there was a problem
$app->redirect("/newphp/contact");
}
$transport = Swift_SendmailTransport::newInstance("/usr/sbin/sendmail -bs");
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance();
$message->setSubject("Email from our website");
$message->setFrom(array(
$cleanEmail => $cleanName
));
$message->setTo(array("eduardoamf12@gmail.com"));
$message->setBody($cleanMsg);
$result = $mailer->send($message);
#var_dump($result);
#exit();
if ($result > 0) {
$app->redirect("/newphp");
} else {
$app->redirect("contact");
}
});
$app->run();
?>
Thank you in advance.
2 Answers
David Tonge
Courses Plus Student 45,640 PointsYou won't be able to sent emails unless it's on a server with email handling capabilities. Won't work in MAMP.
Eduardo Mejía
36,816 PointsThanks for answering David. How can I make this possible? Can I send emails from a local environment?
Carlos Mendez
6,757 PointsCarlos Mendez
6,757 PointsI am using MAMP to serve my PHP environment and I was able to send email to a public email account successfully. I suppose I could have something else running on my machine that makes this work but I generally use MAMP for dev work and don't think the native PHP install is running in the background? Thoughts?