【问题标题】:PHP mail function not working on Centos serverPHP邮件功能在Centos服务器上不起作用
【发布时间】:2012-01-10 14:49:26
【问题描述】:

我正在使用 centos 服务器,必须将邮件发送给用户,所以我从一台服务器复制了我的运行代码并在这里使用它,但它不发送邮件。

代码是:

                $to = $email; //writing mail to the user
                $subject = "Hii";
                $message = "<table>
                <tr><td> Hello ".$email.",</td></tr>
                <tr><td> Some Text </td></tr>
                <tr><td> Some Text </td></tr>
                <tr><td> Some Text </td></tr>
                <tr><td> Some Text </td></tr>
                </table>" ;
                $from = "example@domain.com";
                // To send HTML mail, the Content-type header must be set
                    $headers  = 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                // Additional headers
                $headers .= 'From: Team <example@domain.com>' . "\r\n";

                if(mail($to,$subject,$message,$headers))
                {
                    echo "0";// mail sent Successfully.
                }
                else
                {
                    echo "1";
                }

它总是打印 1。相同的代码在其他项目上运行良好。请指导我在这里也能做些什么来启用它? 任何帮助将不胜感激!

【问题讨论】:

  • 你的盒子上安装了“sendmail”吗?
  • yum install sendmail 应该帮忙,因为如果你还没有安装它,很可能它不在系统中。
  • google 一下,你会发现很多配置的帮助。
  • 如果您不想一次安装 sendmail(由于某种原因,这里的大多数答案都建议),您可以尝试“/etc/init.d/sendmail status”或“/etc/ init.d/postfix status”和“cat /var/log/maillog”

标签: php email centos sendmail


【解决方案1】:

我建议使用 SwiftMailer 来完成很多工作。

require_once('../lib/swiftMailer/lib/swift_required.php');
function sendEmail(){
  //Sendmail
  $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

  //Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  $body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n";


  //Create a message
  $message = Swift_Message::newInstance('Subject goes here')
    ->setFrom(array($email => "no-reply@yourdomain.com"))
    ->setTo(array($email => "$fname $lname"))
    ->setBody($body);

  //Send the message
  $result = $mailer->send($message);
}

【讨论】:

  • 整天处理邮件,但错误从未消失,这在 30 分钟内解决了我的问题!
【解决方案2】:

希望对你有帮助

ini_set('sendmail_from', 'example@domain.com'); 

【讨论】:

    【解决方案3】:

    安装 sendmail* 并运行以下命令后:

    [root@sendmail ~]# yum install sendmail*
    [root@sendmail mail]# yum install dovecot
    [root@sendmail mail]# cd /etc/mail/
    [root@sendmail mail]# vi local-host-names
    # local-host-names - include all aliases for your machine here.
    example.com
    [root@sendmail mail]# vi /etc/dovecot.conf
    protocols = imap pop3 //uncomment
    [root@sendmail mail]# m4 sendmail.mc > sendmail.cf
    [root@sendmail mail]# make
    [root@sendmail mail]# /etc/init.d/sendmail start
    [root@sendmail mail]# /etc/init.d/saslauthd start
    [root@sendmail mail]# /etc/init.d/dovecot start
    [root@sendmail mail]# chkconfig sendmail on
    [root@sendmail mail]# chkconfig dovecot on
    [root@sendmail mail]# chkconfig saslauthd on
    

    我仍然有同样的问题。我检查了我的/var/log/maillog 并看到了一个错误:

    My unqualified host name (domain) unknown; sleeping for retry
    

    经过更多搜索,我将/etc/hosts从:

    127.0.0.1     localhost localhost.localdomain domain
    ip.ip.ip.ip  domain localhost 
    

    到:

     127.0.0.1   localhost.localdomain localhost domain
     ip.ip.ip.ip  localhost domain  
    

    现在邮件功能可以正常工作了。

    【讨论】:

    • 可能是 /etc/dovecot/dovecot.conf?
    • postfix 开箱即用
    • 我使用这些说明在 Docker 容器中为 CentOS 设置 sendmail。然而,我只需要执行 yum install sendmail 即可使用 mail()。为了使邮件功能及时发送电子邮件,如上所述配置主机文件被证明是至关重要的。谢谢!
    【解决方案4】:

    我遇到了同样的问题。我在家里有一个开发服务器,在外部服务器有一个 prod 服务器,传入的邮件转到其他服务器。 PHP:s mail() 在服务器内部工作得很好,但在家里却不行。

    我进行了一些测试,并让它在家里以与服务器房相同的方式工作。 server house 和 home 方法的区别在于 sendmail 的配置。服务器家我只需要安装sendmail就可以了,但是在家里我还必须安装sendmail-cf并使用它来添加发送邮件服务器地址。

    假设您的家庭服务器上有 Centos、Apache 和 PHP,并且您想使用 PHP:s mail() 函数发送电子邮件。

    1) 将家庭服务器上的主机名设置为两个位置:/etc/sysconfig/network 和 /proc/sys/kernel/hostname 这样:

    # 纳米 /etc/sysconfig/network 网络=是 主机名=mydns157.dlinkddns.com # 纳米 /proc/sys/内核/主机名 主机名=mydns157.dlinkddns.com

    2) 安装sendmail和sendmail-cf:

    # yum install sendmail sendmail-cf

    3) 将以下行添加到 /etc/mail/sendmail.mc 中,您的 ISP:s 外发邮件服务器在其中:

    定义(`SMART_HOST',`mail.myisp.com')dnl

    4) 更新 sendmail.cf:

    # /etc/mail/make

    5) 重启 sendmail 和 apache:

    # 服务发送邮件重启 # 服务 httpd 重启

    6) 开机更新主机名:

    # 重启

    就是这样。现在以下工作:

    # php -r'mail("user@somedomain.com", "Subject", "Body", null, "-fme@mydomain.com");'

    你可以跳过 -f:

    # php -r'mail("user@somedomain.com", "Subject", "Body");'

    在这种情况下,发件人的姓名会自动变为 user@hostname,例如。 root@mydns157.dlinkddns.com.


    主机名的一些注释

    主机名的选择很关键。 Centos6 中的默认值为localhost.localdomain,但如果您在调用mail() 时跳过自己的发件人地址(例如'-fme@mydomain.com'),mail() 将无法使用它。如果您确定始终使用您的真实地址作为发件人地址调用 mail(),则主机名可以是任何内容,但是如果您对 mail() 的现有调用缺少发件人地址(我有数百个类似的调用测试目的),那么您必须有一个真实的域作为主机名,因为在这些情况下,您的服务器的主机名用作发件人地址域。从某种意义上说,域必须至少具有 DNS A 记录(由于某种原因,我的 ISP 不需要 NS 记录作为发件人地址,只需要 A 记录,但要与您的 ISP 进行测试和检查)。使用非电子邮件域作为发件人地址的缺点是回复和发送通知将发送到比特的天堂,但是如果您在代码中删除发件人地址,通常意味着您测试和调试某些东西并且不需要回复功能。域可以是例如。您从动态 dns 服务器获得的那个,例如。 mydns157.dlinkddns.com,它可以指向您的家庭路由器(但不是必须)。您可以使用dns_get_record("mydns157.dlinkddns.com") 在 PHP 中获取 DNS 记录,它会返回如下数组:

    [主机] => mydns157.dlinkddns.com [类型] => A [ip] => 92.152.214.137 [类] => 输入 [ttl] => 7

    如果上面的DNS-record中的typeNS,那么这个域就相当于一个email域,可以用自己服务器的hostname,但是效果会有些不同。如果您将现有电子邮件域设置为主机名,例如。 myexistingemaildomain.com,然后向me@myexistingemaildomain.com 发送消息,例如出于调试目的,sendmail 认为消息必须发送到此服务器上用户me 的邮件文件夹。如果me 用户不存在,则发送失败,如果用户存在,则消息转到/var/mail/me。这可能是您想要的,但是(像我一样)您可能希望所有消息都在外部而不是服务器文件夹中传递。

    您的主机名(在 DNS 记录中)不需要指向您服务器的实际外部 IP 以使 mail() 在缺少发件人地址的情况下工作,但这并没有什么害处。主要的是主机名具有有效的 A 记录,并且该域属于您。如果该域不属于您,则可能会产生安全漏洞。如果您将某些现有电子邮件域设置为主机名,例如。 microsoft.com(无论出于何种原因)并在调用 mail() 时向某人发送消息而不添加您自己的发件人地址(例如'-fme@mydomain.com'),发件人地址将自动为you@microsoft.com。如果您以 root 身份登录,则发件人地址将为root@microsoft.com。发送失败的回复和通知会转到root@microsoft.com,这可能不是您的意图。

    【讨论】:

    • 来自service sendmail restart 我收到错误Starting sendmail: 554 5.0.0 No local mailer defined 554 5.0.0 QueueDirectory (Q) option must be set
    【解决方案5】:

    我知道这已得到解答,但我遇到了类似的问题。 以防万一……

    /var/log/maillog 向我显示了 Postfix 权限问题。

    sendmail: fatal: chdir /var/spool/postfix: Permission denied
    

    跟踪错误我发现解决方案是 CentOS 上的 SELinux 策略(我使用的是版本 6)。

    快速回复:setsebool httpd_can_sendmail 1

    您可以使用 -P 使更改永久化;我只需要密码重置电子邮件,所以我的情况不需要。

    信用:http://www.spidersoft.com.au/2011/posftix-permission-denied-problem/?ModPagespeed=noscript

    编辑:我会发表评论,但我还没有足够的声誉。

    【讨论】:

    • 我得到setsebool: SELinux is disabled
    • 看来 SELinux 不是你的问题。你还需要帮助吗?
    【解决方案6】:

    我刚遇到这个问题,有两件事。

    我的电子邮件会变成垃圾邮件,一定要检查一下。可能是因为我的服务器没有正确的 PTR 和 SPF 记录。

    而且,我发现用这个来测试 sendmail 更容易:

    sendmail -s 'me@gmail.com'
    Subject:Testing!
    hey there, how ya doin?
    CTRL+D
    

    【讨论】:

      【解决方案7】:

      始终检查 /var/log/maillog 中的邮件日志以了解问题的原因。 在正确配置后缀后,我曾经遇到过类似的问题。我收到一条错误消息,提示 fatal: setrlimit: Permission denied。解决方法是通过getsebool httpd_can_sendmail 命令检查是否启用了httpd 可以发送邮件。如果 httpd 可以发送邮件是关闭的,那么通过命令启用它:setsebool -P httpd_can_sendmail 1。希望这会有所帮助。

      【讨论】:

      • 在我的情况下它被禁用了
      猜你喜欢
      • 2014-03-16
      • 2016-12-07
      • 2020-01-16
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多