很长一段时间听众,第一次来电者...PHP变量没有被传递到邮件()“排队”参数
我最近开始通过关于PHP O'Reilly的“深入浅出”的书的工作,而这是他们的练习之一 - 代码可能看起来很熟悉你们中的一些人。
课程的目标是演示如何在PHP作品的“邮件”功能,并为此他们给了下面的代码作为例子来使用(编辑为简洁/上下文):
$email = $_POST['email'];
$to = '[email protected]';
$subject = 'Abduction report';
$message = "$name . was abducted $when_it_happened and was gone for $how_long . \n" .
"Number of aliens: $how_many \n" .
"Alien description $alien_description\n" .
"What they did: $what_they_did \n" .
"Fang spotted: $fang_spotted \n" .
"Other comments: $other";
mail($to, $subject, $message, 'From:' . $email);
----------------------------------编辑-------------- ---------------------
每请求时,这里是phpinfo()
结果:
System: Linux infong 2.4 #1 SMP Thu Feb 14 13:02:49 CET 2013 i686 GNU/Linux
Build date: Apr 10 2013 13:38:50
Configure Command: '../configure' 'programsuffix=5' 'withpear=/usr/lib/php5' 'with configfilepath=/usr/lib/php5' 'withlibxmldir' 'withmysqli' 'withkerberos' 'withimapssl' 'enablesoap' 'withxsl' 'enablembstring=all' 'withcurl' 'withmcrypt' 'withgd' 'withpdomysql' 'withfreetypedir' 'withlibxmldir' 'withmysql' 'withzlib' 'enabledebug=no' 'enablesafemode=no' 'enablediscardpath=no' 'withpngdir' 'enabletrackvars' 'withdb' 'withgdbm' 'enableforcecgiredirect' 'enablefastcgi' 'withttf' 'enableftp' 'enabledbase' 'enablememorylimit' 'enablecalendar' 'enablewddx' 'withjpegdir=/usr/src/kundenserver/jpeg6b' 'enablebcmath' 'enablegdimgstrttf' 'enableshmop' 'enablemhash' 'withmhash' 'withopenssl' 'enablexslt' 'withxsltsablot' 'withdom' 'withdomxslt' 'withdomexslt' 'withimap' 'withiconv' 'withbz2' 'withgettext' 'enableexif' 'withidn' 'withsqlite' 'enablesqliteutf8' 'enablezip' 'withtidy' 'enablegdnativettf'
Server API: CGI/FastCGI
Virtual Directory Support: disabled
Configuration File (php.ini) Path: /usr/lib/php5
Loaded Configuration File: /usr/lib/php5/php.ini
Scan this dir for additional .ini files: (none)
additional .ini files parsed: (none)
PHP API: 20041225
PHP Extension: 20060613
Debug Build: no
Thread Safety: disabled
Zend Memory Manager: enabled
IPv6 Support: enabled
Registered PHP Streams: https, ftps, compress.zlib, compress.bzip2, php, file, data, http, ftp, zip
Registered Stream Socket Transports: tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
Registered Strem Filters: zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed
一切WO除了在填写和提交表格后我不会收到电子邮件。
从取出$变量后,“邮件()”,并与我没有得到正确格式的电子邮件中的静态字符串(“[email protected]”)替换它。
我还串联了“$为”变量消息的机构,它从电子邮件主体中正确显示。
所以我很茫然,为什么它不能在邮件的“到”行中使用。我在这里错过了什么?
你有没有试图把' '发件人:' $ email'成'$ headers'变量:'$头= '发件人:'。$电子邮件; ...邮件($ to,$ sub,$ msg,$ headers)'? 你的代码似乎是正确的,但这并不意味着你的服务器喜欢它。这是在运行什么类型的服务器?我也想把你的$加倍给电子邮件,只是为了排除这个问题。 – Dawson 2013-05-03 17:26:57
没有直接回答这个问题,但你有没有考虑过使用质量好的邮件类,而不是PHP的可怕内置'邮件()'函数?我建议下载[phpMailer](https://github.com/Synchro/PHPMailer)或[Swiftmailer](http://swiftmailer.org/)。他们让生活*在PHP中编写简单的电子邮件程序变得更加容易。 – Spudley 2013-05-03 18:58:54
感谢您的快速回复...... DAWSON:我只是尝试了你的“嵌套变量”的想法,并没有改变。我忽略提及我已经尝试过双引号,但这也是不成功的。服务器是托管软件包的一部分,由于这是我第一次进入服务器端脚本,也是我第一次认真编程,所以我认为你的“服务器类型”问题的最佳答案是发布(第一页)对'phpinfo()的响应;' - 查看上面的结果。 SPUDLEY:谢谢你的建议......这当然值得一看! – 2013-05-04 15:53:06