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 trialMarco Perricone
4,831 PointsSwiftMailer
Hello I am trying to use to send e swiftmailer externally but I only get the message body . No emails , no name or other field etc. What's wrong? this is code:
//swiftmailer
$app->post('/contatti', function() use($app){ $name = $app->request->post('name'); $email = $app->request->post('email'); //$azienda = $pp->request->post('azienda'); $msg = $app->request->post('msg');
if(!empty($name) && !empty($email) && !empty($msg)) { $cleanName = filter_var($name, FILTER_SANITIZE_STRING); // $cleanAzienda = filter_var($azienda, FILTER_SANITIZE_STRING); $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL); $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING); } else { //message the user that there was a problem $app->redirect('/contact'); }
// Create the Transport $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") ->setUsername('xxxx@xxxxx.com') ->setPassword('xxxx') ;
/* You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail $transport = Swift_MailTransport::newInstance(); */
// Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport);
// Create a message $message = Swift_Message::newInstance(); $message->setSubject('email dal sito!'); $message->setFrom(array( $cleanEmail => $cleanName )); $message->setTo(array('xxxxx29@xxxx.com')); $message->setBody($cleanMsg);
// Send the message $result = $mailer->send($message);
if($result > 0) { //messaggio inviato $app->redirect('/contatti');
}else { //messaggio non inviato $app->redirect('/contatti');
}
}); //fine swiftmailer
Brandon Berger
3,947 PointsBrandon Berger
3,947 PointsIs it possible that it is not working because most of the code is commented out?