使用 php 发送电子邮件
我正在尝试编写一个非常简单的 php 代码,将电子邮件发送到给定地址。
I have this code:
<?php
mail('[email protected]','Test','Test OK');
echo 'Message sent!';
?>
而不是 '[电子邮件受保护]'
我我用过我自己的电子邮件。
我已经按照 foundationphp 中给出的说明启用了 php,因为我正在运行带有雪的 mac豹。
php 似乎正在工作,因为我收到“消息已发送!”回声,但是我没有收到任何电子邮件。
有什么想法为什么会发生这种情况吗?我做错了什么?
提前致谢!
I'm trying to write a very simple php code that sends an e-mail to a given address.
I have this code:
<?php
mail('[email protected]','Test','Test OK');
echo 'Message sent!';
?>
Instead of '[email protected]'
I've used my own e-mail.
I´ve enabled php using the directions given in foundationphp, because I'm running a mac with snow leopard.
php seems to be working because I´m getting the "message sent!" echo, however i'm not receiving any e-mail.
Any ideas why is this happening? What am I doing wrong?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在本地计算机上运行此程序,则您的电子邮件很可能不会发送到任何地方。 PHP 的
mail
函数不知道您是否有正确的发送邮件连接,它只知道邮件是否成功进入本地队列。对于非服务器计算机,最好的选择是设置一个远程服务器SMTP 服务器(通常是您的 ISP 处的服务器)。 http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Remote_SMTP_Server_for_Sending_Mail.htm
If you're running this on your local machine, chances are your e-mails aren't going to go anywhere. PHP's
mail
function doesn't know if you've got a proper outgoing mail connection, it only knows if the mail entered the local queue successfully.Your best bet for a non-server machine is to set up a remote SMTP server (usually, the one at your ISP). http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Remote_SMTP_Server_for_Sending_Mail.htm
您会收到回显“消息已发送!”因为您没有询问 mail() 是否成功:
这应该会给您一个正确的反馈。
You receive the echo "Message sent!" becuase you did not ask if mail() was successful:
That should give you a correct feedback about it.
首先,你根本没有检查
mail
的返回值,你会回显“消息已发送!”即使它返回 false。其次,即使
mail
返回 true,也仅意味着它将邮件交给了 MTA,而不意味着邮件已到达目的地。如果 PHP 成功传递邮件,那么您需要查看邮件日志并在那里进行调试,因为问题不在 PHP 代码/配置中。First of all, you didn't check the return value of
mail
at all, you'll echo "Message sent!" even if it returned false.Second, even if
mail
returns true, that just means it handed the mail to the MTA, not that the mail reached its destination. If PHP is handing off the mail successfully, then you need to look at your mail logs and debug there, because the issue isn't in the PHP code/configuration.