问:创建传输变量后出现服务器错误500
<?php
需要'vendor / autoload.php' ; date_default_timezone_set ('America / New_York' ); $ app = new \ Slim \ Slim (array ('view' => new \ Slim \ Views \ Twig ())); $ view = $ app- > view (); $ view- > parserOptions = array ('debug' => true );复制代码 $ view- > parserExtensions = array (new \ Slim \ Views \ TwigExtension (),
);
$ app- > get ('/' ,function ()use ($ app ){ $ app- > render ('about.twig' ); })-> name (home ); $ app- > get ('/ contact' ,function ()use ($ app ){ $ app- > render ('contact.twig' ); })->名称(contact ); $ app- >发布
('/ contact' , function () use ($ app ){
$ name = $ app- > request- > post ('name' ); $ email = $ app- > request- > post ('email' ); $ msg = $ app- > request- > post ('msg' ); if (!空($ name )&& !空($ email )&&
!空($ MSG )) {
$ cleanName = filter_var ($名称,FILTER_SANITIZE_STRING ); $ cleanEmail = filter_var ($电子邮件,FILTER_SANITIZE_EMAIL ); $ cleanMsg = filter_var ($ MSG ,FILTER_SANITIZE_STRING ); } else { //向用户发送消息$ app- > redirect ('/ contact' );} });
//此代码有误吗?当我注释掉下面的代码时,一切工作正常//
$ transport = Swift_SendmailTransport :: newInstance ('/ usr / sbin / sendmail -bs' ); $ mailer = \ Swift_Mailer :: newInstance ($ transport ); $ message = \ Swift_Message :: newInstance (); $ message- > setSubject ('来自我们网站的电子邮件' ); $ message- > setFrom (array ($ cleanEmail => $ cleanName ;
));
$ message- > setTo (array ('treehouse @ localhost' ));
// sendmail必须在服务器内部运行,否则您可以输入任何内容^
$ message- > setBody ($ cleanMsg );
$ result = $ mailer- > send ($ message ); if ($ result > 0 ){ $ app- >重定向('/' ); } else { $ app- > redirect ('/ contact' ); }
if ($ result > 0 ) {
$ app- >重定向('/' ); } else { $ app- > redirect ('/ contact' ); } $ app- > run ();
返回->
服务器错误
500详细信息(该网站在检索http://port-80-tzgn9yrnis.treehouse-app/时遇到错误。该网站可能由于维护原因而关闭或配置不正确。)
我已经加载并重新加载了所有依赖项,slim,twig,swiftmailer,slim view等...,但仍然看到此错误。与本教程的代码相比,我还检查并重新检查了我的代码,没有发现任何差异。
如果可能,请告知为什么呈现此错误消息。
答:是的,您的代码放置不正确。这是正常工作的代码。请注意}); 您所编写内容的位置,使该代码在任何路由之外都悬而未决。另外,请查看我如何编辑您的代码引号,以便突出显示语法以供将来参考。
<?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 )&& !
空($ email ) && !空($ MSG )) {
$ cleanName = filter_var ($名称,FILTER_SANITIZE_STRING ); $ cleanEmail = filter_var ($电子邮件,FILTER_SANITIZE_EMAIL ); $ cleanMsg = filter_var ($ MSG ,FILTER_SANITIZE_STRING ); } else { //向用户发送消息$ app- > redirect ('/ contact' ); }
$ transport = Swift_SendmailTransport :: newInstance ('/ usr / sbin / sendmail -bs' );
$ mailer = \ Swift_Mailer :: newInstance ($ transport ); $ message = \ Swift_Message :: newInstance (); $ message- > setSubject ('来自我们网站的电子邮件' ); $ message- > setFrom (array ($ cleanEmail => $ cleanName )); $ message- > setTo (array (
'teds@biblewordstudy.net' ));
$ message- > setBody ($ cleanMsg ); $ result = $ mailer- > send ($ message ); if ($ result > 0 ){ //发送一条消息,说谢谢$ app- > redirect ('/' ); } else { //向用户发送一条消息,消息发送失败// //记录错误$ app- > redirect ('/ contact' ); } });
$ app- >运行();