Skip to content

How To Make PHP Mail Work on Linux

In WAMP, you can setup an external SMTP server for PHP to send mail, but in LAMP, it's not the same story. You have to configure it manually to make it functional. Here are my steps to resolve the mailing problem. I assumed that you already have the PHP code for testing the mailing function.

  1. Comment out "SMTP" entry in php.ini.
  2. Search for an entry called "SMTP" and comment it out as below:

    [root@test ~]# vi /etc/php.ini
    ...
    ;SMTP = localhost
  3. Make sure that "sendmail" has been installed. If not, please install it.
  4. [root@test ~]# rpm -q sendmail
    [root@test ~]# yum install sendmail
    Loaded plugins: fastestmirror, refresh-packagekit, security
    Loading mirror speeds from cached hostfile
     * base: mirrors.neusoft.edu.cn
     * epel: mirrors.hust.edu.cn
     * extras: mirrors.stuhome.net
     * updates: mirrors.grandcloud.cn
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package sendmail.x86_64 0:8.14.4-8.el6 will be installed
    --> Processing Dependency: procmail for package: sendmail-8.14.4-8.el6.x86_64
    --> Processing Dependency: libhesiod.so.0()(64bit) for package: sendmail-8.14.4-8.el6.x86_64
    --> Running transaction check
    ---> Package hesiod.x86_64 0:3.1.0-19.el6 will be installed
    ---> Package procmail.x86_64 0:3.22-25.1.el6 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    =============================================================================================================================
     Package                      Arch                       Version                              Repository                Size
    =============================================================================================================================
    Installing:
     sendmail                     x86_64                     8.14.4-8.el6                         base                     717 k
    Installing for dependencies:
     hesiod                       x86_64                     3.1.0-19.el6                         base                      20 k
     procmail                     x86_64                     3.22-25.1.el6                        base                     163 k

    Transaction Summary
    =============================================================================================================================
    Install       3 Package(s)

    Total download size: 900 k
    Installed size: 1.9 M
    Is this ok [y/N]: y
    Downloading Packages:
    (1/3): hesiod-3.1.0-19.el6.x86_64.rpm                                                                 |  20 kB     00:00
    (2/3): procmail-3.22-25.1.el6.x86_64.rpm                                                              | 163 kB     00:00
    (3/3): sendmail-8.14.4-8.el6.x86_64.rpm                                                               | 717 kB     00:02
    -----------------------------------------------------------------------------------------------------------------------------
    Total                                                                                         83 kB/s | 900 kB     00:10
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : procmail-3.22-25.1.el6.x86_64                                                                             1/3
      Installing : hesiod-3.1.0-19.el6.x86_64                                                                                2/3
      Installing : sendmail-8.14.4-8.el6.x86_64                                                                              3/3
      Verifying  : hesiod-3.1.0-19.el6.x86_64                                                                                1/3
      Verifying  : procmail-3.22-25.1.el6.x86_64                                                                             2/3
      Verifying  : sendmail-8.14.4-8.el6.x86_64                                                                              3/3

    Installed:
      sendmail.x86_64 0:8.14.4-8.el6

    Dependency Installed:
      hesiod.x86_64 0:3.1.0-19.el6                                procmail.x86_64 0:3.22-25.1.el6

    Complete!
  5. Restart the server to make sendmail to take over the mailing function.
  6. [root@test ~]# shutdown -r now
  7. Check the status of sendmail.
  8. [root@test ~]# service sendmail status
    sendmail (pid  2496) is running...
    sm-client (pid  2495) is running...

    It seems functional, we can try to execute the PHP code again, but in fact, it failed.

  9. Check the maillog and see what happened.
  10. [root@test ~]# tail -f /var/log/maillog
    ...
    Jan  6 20:38:11 test sendmail[2703]: NOQUEUE: SYSERR(apache): can not chdir(/var/spool/clientmqueue/): Permission denied

    There was an error due to permission issue. It may be blocked by SELinux.

  11. Check SELinux boolean value of sendmail.
  12. [root@test ~]# getsebool -a | grep sendmail
    httpd_can_sendmail --> off
    logging_syslogd_can_sendmail --> off

    Oh, "httpd_can_sendmail" is turned off. Let's enable it by "setsebool".

  13. Set and check SELinux boolean value of sendmail. It may take a while.
  14. [root@test ~]# setsebool -P httpd_can_sendmail on
    [root@test ~]# getsebool -a | grep mail
    httpd_can_sendmail --> on
    logging_syslogd_can_sendmail --> off
  15. Don't forget to restart httpd after enabling.
  16. [root@test ~]# service httpd restart
    Stopping httpd:                                            [  OK  ]
    Starting httpd:                                            [  OK  ]

    Now, let's try to execute the PHP code again.

  17. Check the maillog again.
  18. [root@test ~]# tail -f /var/log/maillog
    ...
    Jan  6 21:06:46 test sendmail[2752]: s06D6kSB002752: from=apache, size=812, class=0, nrcpts=1, msgid=<201401061306.s06D6kSB002752@test.example.com>, relay=apache@localhost
    Jan  6 21:06:47 test sendmail[2753]: s06D6kF9002753: from=<apache@test.example.com>, size=1035, class=0, nrcpts=1, msgid=<201401061306.s06D6kSB002752@test.example.com>, proto=ESMTP, daemon=MTA, relay=example [127.0.0.1]
    Jan  6 21:06:47 test sendmail[2752]: s06D6kSB002752: to=Ed Chen's Logic <admin@howtosop.com>, ctladdr=apache (48/48), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30812, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (s06D6kF9002753 Message accepted for delivery)
    Jan  6 21:06:51 test sendmail[2755]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=RC4-SHA, bits=128/128
    Jan  6 21:07:06 test sendmail[2755]: s06D6kF9002753: to=<admin@howtosop.com>, ctladdr=<apache@test.example.com> (48/48), delay=00:00:19, xdelay=00:00:19, mailer=esmtp, pri=121035, relay=gmail-smtp-in.l.google.com. [173.194.79.27], dsn=2.0.0, stat=Sent (OK 1389013626 r7si54762570pbk.207 - gsmtp)

Now, it works.

Leave a Reply

Your email address will not be published. Required fields are marked *