如何找出PHP和sendmail是否在一起工作? [英] how to find out if PHP and sendmail are working together?

查看:606
本文介绍了如何找出PHP和sendmail是否在一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用终端发送电子邮件时,工作正常。我使用以下命令:

  echoSubject:test| / usr / lib / sendmail -v you@domain.com 

但是当我在PHP中尝试:



PHP文件:

 <?php bool mail $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])?> 

我的实施:

 <?php mail(you@domain.com,我的主题,我的消息内容,发件人:me@domain.com); ?> 

,那么它不工作。这是为什么?我想这是与PHP的php.ini文件?



所有我配置:




  • 打开php.ini:



    nano /etc/php5/apache2/php.ini


  • 取消注释sendmail_path并写入:



    sendmail_path = / usr / sbin / sendmail


  • 取消注释sendmail_from并写入:



    sendmail_from = me@domain.com


  • 重新启动apache:



    /etc/init.d/apache2 restart




可能是什么情况?要让这个更清楚:我希望PHP能够电子邮件!



编辑:




  • PHP mail()函数返回FALSE。

  • 将php.ini:将 / usr / sbin / sendmail 更改为 / usr / lib / sendmail (或其他方式)似乎没有任何区别

  • 对于终端命令:更改 / usr / sbin / sendmail / usr / lib / sendmail (或相反)似乎没有任何区别




Sendmail的日志文件: / p>

  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 =< 201103200920.p2K9K09k012277@machine.domain.com> relay = root @ localhost
Mar 20 10:20:00 machine sm-mta [12279]:p2K9K0Ge012279:< -r@machine.domain.com> ...用户不明
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 =用户未知
Mar 20 10:20:00 machine sm-mta [12279]:p2K9K0Ge012279:from =< root@machine.domain .com,size = 290,class = 0,nrcpts = 2,msgid =< 201103200920.p2K9K09k012277@machine.domain.com> proto = ESMTP,守护进程= 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 =< you@domain.com> ctladdr =< root@machine.domain。 com> (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 = < you2@domain.com> ctladdr =< root@machine.domain.com> (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:to = you2 @ domain.com @ domain.com,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消息被接受交货)
Mar 20 10:20:04机器sendmail [12277]:p2K9K09k012277:p2K9K09l012277:DSN:用户未知
3月20日10: 20:04 machine sm-mta [12279]:p2K9K0Gg012279:from =",size = 1921,class = 0,nrcpts = 1,msgid =< 201103200920.p2K9K09l012277@machine.domain.com> proto = ESMTP ,daemon = MTA-v4,relay = localhost [127.0.0.1]
Mar 20 10:20:05 machine sm-mta [12279]:p2K9K0Gg012279:to =< root@machine.domain.com> 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 =发送(p2K9K0Gg012279邮件接受交货)
3月20日10:25:11机器sendmail [12321]:p2K9PBC3012321:从= root,size = 14,class = 0,nrcpts = 1,msgid =< 201103200925.p2K9PBC3012321 @ machine.domain.com> relay = root @ localhost
Mar 20 10:25:11 machine sm-mta [12323]:p2K9PBLe012323:from =< root@machine.domain.com> size = 318 ,class = 0,nrcpts = 1,msgid =< 201103200925.p2K9PBC3012321@machine.domain.com>,prod = 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 =< you@domain.com> ctladdr =< root@machine.domain.com> (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:to=you@domain.com,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消息接受交付)
3月20日11:00:17机器sendmail [12567]:p2KA0HX9012567:from = root,size = 20,class = 0,nrcpts = 1,msgid = 201103201000.p2KA0HX9012567@machine.domain.com> relay = root @ localhost
Mar 20 11:00:17 machine sm-mta [12568]:p2KA0HNn012568:from = size = 324,class = 0,nrcpts = 1,msgid =< 201103201000.p2KA0HX9012567@machine.domain.com>,prod = 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 =< you@domain.com> ctladdr =< root@machine.domain.com> (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:to=you@domain.com,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消息接受交货)
Mar 20 11:16:23 machine sendmail [12675]:p2KAGNg8012675:from = www-data,size = 188,class = 0,nrcpts = 0,msgid = < 201103201016.p2KAGNg8012675@machine.domain.com> relay = www-data @ localhost
root @ machine:/ var / log#

EDIT2:



我找到了解决方案!



这是php.ini中的配置,如我所料。



在php.ini中:



sendmail_path = / usr / sbin / sendmail * * -t -i **



现在,让我们试着理解为什么,因为我在这个网站,然后我发现-i在php.ini文件本身。 -t和-i是什么意思?



-t
从邮件标题中提取收件人。这些将添加到在命令行上指定的任何收件人。对于2.1之前的Postfix版本,此选项要求在命令行上不指定收件人地址。



-i
从标准输入读取消息时,不要只使用一行处理。字符作为输入的结束。



来源



希望我能帮助那些遇到类似问题的人。

解决方案


可能是什么情况?要使这个更清楚:我想让PHP能够电子邮件!


在这种情况下或更好的原因是,即mail()函数和sendmail是两个不同的鞋子。



因为你可以使用sendmail程序发送电子邮件,这并不意味着php mail()函数会发送电子邮件。



您面临的是您的php安装的配置问题。 PHP默认发送电子邮件是相当不错,但是PHP无法知道所有的设置,所以如果mail()失败,你需要检查你是否正确配置PHP发送电子邮件。



一个好的开始,了解更多配置,获得吨的笔记和通知以及相关信息是PHP手册: mail()function



第一个健全性检查mail()函数的返回值。如果是false,那么PHP知道发送邮件失败了。



如果是真的,还是没有收到电子邮件,有助于进一步传输电子邮件。你需要检查整个链的运输和 - 只是为了使它大胆 - 你可以有多个错误在整体配置,阻止你通过PHP发送电子邮件,即使你能够从shell发送一些。 p>

如果这太复杂了,我建议您联系系统管理员,帮助您解决问题。



您可以使用shell命令编写自己的包装程序并发送电子邮件: shell_exec ,请小心。


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 you@domain.com

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("you@domain.com", "my topic", "my message contents", "From: me@domain.com"); ?>

, 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 = me@domain.com

  • 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=<201103200920.p2K9K09k012277@machine.domain.com>, relay=root@localhost
Mar 20 10:20:00 machine sm-mta[12279]: p2K9K0Ge012279: <-r@machine.domain.com>... 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=<root@machine.domain.com>, size=290, class=0, nrcpts=2, msgid=<201103200920.p2K9K09k012277@machine.domain.com>, 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=<you@domain.com>, ctladdr=<root@machine.domain.com> (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=<you2@domain.com>, ctladdr=<root@machine.domain.com> (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: to=you2@domain.com,you@domain.com, 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=<201103200920.p2K9K09l012277@machine.domain.com>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 20 10:20:05 machine sm-mta[12279]: p2K9K0Gg012279: to=<root@machine.domain.com>, 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=<201103200925.p2K9PBC3012321@machine.domain.com>, relay=root@localhost
Mar 20 10:25:11 machine sm-mta[12323]: p2K9PBLe012323: from=<root@machine.domain.com>, size=318, class=0, nrcpts=1, msgid=<201103200925.p2K9PBC3012321@machine.domain.com>, 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=<you@domain.com>, ctladdr=<root@machine.domain.com> (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: to=you@domain.com, 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=<201103201000.p2KA0HX9012567@machine.domain.com>, relay=root@localhost
Mar 20 11:00:17 machine sm-mta[12568]: p2KA0HNn012568: from=<root@machine.domain.com>, size=324, class=0, nrcpts=1, msgid=<201103201000.p2KA0HX9012567@machine.domain.com>, 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=<you@domain.com>, ctladdr=<root@machine.domain.com> (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: to=you@domain.com, 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=<201103201016.p2KAGNg8012675@machine.domain.com>, 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.

source

Hope I could help those of you that ran in to similar problems.

解决方案

What can be the case? To make this extra clear: I want PHP to be able to e-mail!

The case or better the cause 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 wether 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.

BTW you can write your own wrapper and send emails with a shell command: shell_exec, take care.

这篇关于如何找出PHP和sendmail是否在一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆