如何确定 PHP 和 sendmail 是否一起工作?
当我使用终端发送电子邮件时,一切正常。我使用以下命令:
echo "Subject: test" | /usr/lib/sendmail -v [email protected]
但是当我在 PHP 中尝试此操作时:
PHP DOCUMENTATION:
<?php bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) ?>
MY IMPLMENTATION:
<?php mail("[email protected]", "my topic", "my message contents", "From: [email protected]"); ?>
,那么它不起作用。这是为什么?我猜这与 PHP 的 php.ini 文件有关?
我配置的所有内容:
打开 php.ini:
nano /etc/php5/apache2/php.ini
取消注释sendmail_path并写入:
sendmail_path = /usr/sbin/sendmail
取消注释 sendmail_from 并写入:
sendmail_from = [电子邮件受保护]
重新启动apache:
/etc/init.d/apache2 restart
可能是什么情况?为了更清楚地说明这一点:我希望 PHP 能够发送电子邮件!
编辑:
- PHP mail() 函数返回 FALSE。
- 对于 php.ini:将
/usr/sbin/sendmail
更改为/usr/lib/sendmail
(或相反)似乎 - 对终端命令 没有任何影响:将
/usr/sbin/sendmail
更改为/usr/lib/sendmail
(或相反)似乎没有任何区别, - 我也向我的两个邮箱发送了消息自己的域和 gmail,垃圾邮件文件夹中都没有邮件。
Sendmail的日志文件:
root@machine:/var/log# cat mail.warn
root@machine:/var/log# cat mail.err
root@machine:/var/log# cat mail.log
Mar 20 10:17:09 machine sendmail[12249]: p2K9H2R9012249: from=root, size=0, class=0, nrcpts=1, relay=root@localhost
Mar 20 10:17:20 machine sendmail[12253]: p2K9HImw012253: from=root, size=0, class=0, nrcpts=0, relay=root@localhost
Mar 20 10:17:25 machine sendmail[12255]: p2K9HOAC012255: from=root, size=0, class=0, nrcpts=1, relay=root@localhost
Mar 20 10:20:00 machine sendmail[12277]: p2K9K09k012277: from=root, size=14, class=0, nrcpts=3, msgid=<[email protected]>, relay=root@localhost
Mar 20 10:20:00 machine sm-mta[12279]: p2K9K0Ge012279: <[email protected]>... User unknown
Mar 20 10:20:00 machine sendmail[12277]: p2K9K09k012277: to=-r, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=90014, relay=[127.0.0.1] [127.0.0.1], dsn=5.1.1, stat=User unknown
Mar 20 10:20:00 machine sm-mta[12279]: p2K9K0Ge012279: from=<[email protected]>, size=290, class=0, nrcpts=2, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:20:00 machine sm-mta[12279]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:20:02 machine sm-mta[12279]: p2K9K0Ge012279: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:02, xdelay=00:00:02, mailer=esmtp, pri=60290, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300612802 w59si3196382eeh.92)
Mar 20 10:20:02 machine sm-mta[12279]: STARTTLS=client, relay=aspmx.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:20:04 machine sm-mta[12279]: p2K9K0Ge012279: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:04, xdelay=00:00:02, mailer=esmtp, pri=60290, relay=aspmx.l.google.com. [74.125.79.27], dsn=2.0.0, stat=Sent (OK 1300612804 p50si3196335eei.44)
Mar 20 10:20:04 machine sendmail[12277]: p2K9K09k012277: [email protected],[email protected], ctladdr=root (0/0), delay=00:00:04, xdelay=00:00:04, mailer=relay, pri=90014, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9K0Ge012279 Message accepted for delivery)
Mar 20 10:20:04 machine sendmail[12277]: p2K9K09k012277: p2K9K09l012277: DSN: User unknown
Mar 20 10:20:04 machine sm-mta[12279]: p2K9K0Gg012279: from=<>, size=1921, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:20:05 machine sm-mta[12279]: p2K9K0Gg012279: to=<[email protected]>, delay=00:00:01, xdelay=00:00:01, mailer=local, pri=31921, dsn=2.0.0, stat=Sent
Mar 20 10:20:05 machine sendmail[12277]: p2K9K09l012277: to=root, delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=31038, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9K0Gg012279 Message accepted for delivery)
Mar 20 10:25:11 machine sendmail[12321]: p2K9PBC3012321: from=root, size=14, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
Mar 20 10:25:11 machine sm-mta[12323]: p2K9PBLe012323: from=<[email protected]>, size=318, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:25:11 machine sm-mta[12323]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:25:12 machine sm-mta[12323]: p2K9PBLe012323: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:01, xdelay=00:00:01, mailer=esmtp, pri=30318, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300613112 u12si3204796eeh.99)
Mar 20 10:25:12 machine sendmail[12321]: p2K9PBC3012321: [email protected], ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30014, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9PBLe012323 Message accepted for delivery)
Mar 20 11:00:17 machine sendmail[12567]: p2KA0HX9012567: from=root, size=20, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
Mar 20 11:00:17 machine sm-mta[12568]: p2KA0HNn012568: from=<[email protected]>, size=324, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 11:00:17 machine sm-mta[12568]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 11:00:18 machine sm-mta[12568]: p2KA0HNn012568: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:01, xdelay=00:00:01, mailer=esmtp, pri=30324, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300615218 w59si3268114eeh.65)
Mar 20 11:00:18 machine sendmail[12567]: p2KA0HX9012567: [email protected], ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30020, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2KA0HNn012568 Message accepted for delivery)
Mar 20 11:16:23 machine sendmail[12675]: p2KAGNg8012675: from=www-data, size=188, class=0, nrcpts=0, msgid=<[email protected]>, relay=www-data@localhost
root@machine:/var/log#
EDIT2:
我找到了解决方案!
正如我所料,这是 php.ini 中的配置。
在 php.ini 中:
sendmail_path = /usr/sbin/sendmail **-t -i**
现在,让我们尝试理解为什么,因为我在 此站点 然后我在 php.ini 中找到了 -i 。 ini 文件本身。 -t 和 -i 是什么意思?
-t
从邮件标头中提取收件人。这些将添加到命令行上指定的任何收件人。对于 2.1 之前的 Postfix 版本,此选项要求不在命令行上指定收件人地址。
-i
从标准输入读取消息时,不要处理仅包含 .字符作为输入的结束。
希望我可以帮助那些遇到类似问题的人。
When I use the terminal to send an e-mail things work correctly. I use the following command:
echo "Subject: test" | /usr/lib/sendmail -v [email protected]
But when I try this in PHP:
PHP DOCUMENTATION:
<?php bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) ?>
MY IMPLEMENTATION:
<?php mail("[email protected]", "my topic", "my message contents", "From: [email protected]"); ?>
, then it is not working. Why is that? I guess it has to do with PHPs php.ini file?
All I configured:
open php.ini:
nano /etc/php5/apache2/php.ini
uncomment sendmail_path and write:
sendmail_path = /usr/sbin/sendmail
uncomment sendmail_from and write:
sendmail_from = [email protected]
restart apache:
/etc/init.d/apache2 restart
What can be the case? To make this extra clear: I want PHP to be able to e-mail!
EDIT:
- The PHP mail() function returns a FALSE.
- for php.ini: changing
/usr/sbin/sendmail
into/usr/lib/sendmail
(or the other way around) does not seem to make any difference - for terminal command: changing
/usr/sbin/sendmail
into/usr/lib/sendmail
(or the other way around) does not seem to make any difference either - I sent messages to both my own domain and to gmail, both without messages in the spam folder.
The logfiles of Sendmail:
root@machine:/var/log# cat mail.warn
root@machine:/var/log# cat mail.err
root@machine:/var/log# cat mail.log
Mar 20 10:17:09 machine sendmail[12249]: p2K9H2R9012249: from=root, size=0, class=0, nrcpts=1, relay=root@localhost
Mar 20 10:17:20 machine sendmail[12253]: p2K9HImw012253: from=root, size=0, class=0, nrcpts=0, relay=root@localhost
Mar 20 10:17:25 machine sendmail[12255]: p2K9HOAC012255: from=root, size=0, class=0, nrcpts=1, relay=root@localhost
Mar 20 10:20:00 machine sendmail[12277]: p2K9K09k012277: from=root, size=14, class=0, nrcpts=3, msgid=<[email protected]>, relay=root@localhost
Mar 20 10:20:00 machine sm-mta[12279]: p2K9K0Ge012279: <[email protected]>... User unknown
Mar 20 10:20:00 machine sendmail[12277]: p2K9K09k012277: to=-r, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=90014, relay=[127.0.0.1] [127.0.0.1], dsn=5.1.1, stat=User unknown
Mar 20 10:20:00 machine sm-mta[12279]: p2K9K0Ge012279: from=<[email protected]>, size=290, class=0, nrcpts=2, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:20:00 machine sm-mta[12279]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:20:02 machine sm-mta[12279]: p2K9K0Ge012279: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:02, xdelay=00:00:02, mailer=esmtp, pri=60290, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300612802 w59si3196382eeh.92)
Mar 20 10:20:02 machine sm-mta[12279]: STARTTLS=client, relay=aspmx.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:20:04 machine sm-mta[12279]: p2K9K0Ge012279: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:04, xdelay=00:00:02, mailer=esmtp, pri=60290, relay=aspmx.l.google.com. [74.125.79.27], dsn=2.0.0, stat=Sent (OK 1300612804 p50si3196335eei.44)
Mar 20 10:20:04 machine sendmail[12277]: p2K9K09k012277: [email protected],[email protected], ctladdr=root (0/0), delay=00:00:04, xdelay=00:00:04, mailer=relay, pri=90014, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9K0Ge012279 Message accepted for delivery)
Mar 20 10:20:04 machine sendmail[12277]: p2K9K09k012277: p2K9K09l012277: DSN: User unknown
Mar 20 10:20:04 machine sm-mta[12279]: p2K9K0Gg012279: from=<>, size=1921, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:20:05 machine sm-mta[12279]: p2K9K0Gg012279: to=<[email protected]>, delay=00:00:01, xdelay=00:00:01, mailer=local, pri=31921, dsn=2.0.0, stat=Sent
Mar 20 10:20:05 machine sendmail[12277]: p2K9K09l012277: to=root, delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=31038, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9K0Gg012279 Message accepted for delivery)
Mar 20 10:25:11 machine sendmail[12321]: p2K9PBC3012321: from=root, size=14, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
Mar 20 10:25:11 machine sm-mta[12323]: p2K9PBLe012323: from=<[email protected]>, size=318, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:25:11 machine sm-mta[12323]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 10:25:12 machine sm-mta[12323]: p2K9PBLe012323: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:01, xdelay=00:00:01, mailer=esmtp, pri=30318, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300613112 u12si3204796eeh.99)
Mar 20 10:25:12 machine sendmail[12321]: p2K9PBC3012321: [email protected], ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30014, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2K9PBLe012323 Message accepted for delivery)
Mar 20 11:00:17 machine sendmail[12567]: p2KA0HX9012567: from=root, size=20, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
Mar 20 11:00:17 machine sm-mta[12568]: p2KA0HNn012568: from=<[email protected]>, size=324, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 11:00:17 machine sm-mta[12568]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
Mar 20 11:00:18 machine sm-mta[12568]: p2KA0HNn012568: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:01, xdelay=00:00:01, mailer=esmtp, pri=30324, relay=gmail-smtp-in.l.google.com. [74.125.77.27], dsn=2.0.0, stat=Sent (OK 1300615218 w59si3268114eeh.65)
Mar 20 11:00:18 machine sendmail[12567]: p2KA0HX9012567: [email protected], ctladdr=root (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30020, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p2KA0HNn012568 Message accepted for delivery)
Mar 20 11:16:23 machine sendmail[12675]: p2KAGNg8012675: from=www-data, size=188, class=0, nrcpts=0, msgid=<[email protected]>, relay=www-data@localhost
root@machine:/var/log#
EDIT2:
I have found the solution!
It was the config in php.ini, as I expected.
In php.ini:
sendmail_path = /usr/sbin/sendmail **-t -i**
Now, lets also try to understand why, cause I found the -t at this site and then i found the -i in the php.ini file itself. What does -t and -i mean??
-t
Extract recipients from message headers. These are added to any recipients specified on the command line. With Postfix versions prior to 2.1, this option requires that no recipient addresses are specified on the command line.
-i
When reading a message from standard input, don't treat a line with only a . character as the end of input.
Hope I could help those of you that ran in to similar problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种情况或者更好,因为在您的情况下,
mail()
函数和 sendmail 是两双不同的鞋子。它们本身彼此不相关。因此,仅仅因为您可以使用 sendmail 程序发送电子邮件,并不意味着 PHP
mail()
函数也可以发送电子邮件。您面临的是 PHP 安装的配置问题。默认情况下,PHP 非常适合发送电子邮件,但 PHP 无法了解所有设置,因此如果
mail()
失败,您需要检查是否已正确配置 PHP 来发送电子邮件。PHP 手册是了解更多配置内容、获取大量注释和通知以及相关信息的良好开端:
mail()
函数。第一个健全性检查 - 正如其他人已经评论过的 - 是检查
mail()
函数的返回值。如果为 false,则 PHP 知道发送邮件失败。如果这是真的,并且您仍然没有收到电子邮件(发出),那么其他一些组件对于进一步传输电子邮件没有帮助。您需要检查整个传输链 - 只是为了使其粗体 - 您的整体配置中可能存在多个错误,这些错误会阻止您通过 PHP 发送电子邮件,即使您能够从 shell 发送一些电子邮件。
如果这对您来说太复杂,我建议您与系统管理员联系,他们可以帮助您解决问题。
顺便说一下,您可以编写自己的包装器并使用 shell 命令发送电子邮件:
shell_exec
,小心。The case or better the because in your case is, that the
mail()
function and sendmail are two different pair of shoes. They are not related to each other per-se.So only because you can send emails using the sendmail program this does not mean that the PHP
mail()
function will send emails as well.What you are facing is a configuration problem of your PHP installation. PHP is pretty good to sending emails by default but PHP is not able to know all setups so in case
mail()
fails you need to check whether or not you've properly configured PHP to send emails.A good start to learn more what to configure, get tons of notes and notices as well as related information is the PHP manual:
mail()
function.A first sanity check - as others have already commented - is to check the return value of the
mail()
function. If it's false, then PHP knows that sending mail has failed.If it's true and you still don't get the email (out), then some other component is not helpful in transporting the email further on. You need to check the whole chain of the transport and - just to make it bold - you can have multiple errors in your overall configuration that is preventing you sending emails via PHP even in case you are able to send some from the shell.
If this is too complicated for you I suggest you get in contact with a system administrator that can help you in solving your problem.
By the way, you can write your own wrapper and send emails with a shell command:
shell_exec
, take care.您可以从检查邮件日志文件开始。它的位置取决于您的系统,但可以在标准 Ubuntu 安装的
/var/log/mail.log
中找到。您应该尝试运行 PHP 代码,然后检查是否记录了任何内容。You can start by checking the mail log file. It's location depends on your system but it can be found in
/var/log/mail.log
on a standard Ubuntu installation. You should try running your PHP code then checking if anything is logged.