PHP mail function not working on Centos server

Astha picture Astha · Dec 1, 2011 · Viewed 66.4k times · Source

I am using centos Server and have to send the mail to the user so i copied running code of mine from one server and used it in here, but it is not sending mails.

Code is :

                $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 = "[email protected]";
                // 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 <[email protected]>' . "\r\n";

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

It always print 1. Same code running fine on other project. Please guide me what i can do to enable it here too? Any help will be highly appreciated!

Answer

Astha picture Astha · Dec 2, 2011

After installing sendmail* and running the following commands:

[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

I still had the same issue. I checked my /var/log/maillog and saw an error:

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

After more searching, I changed /etc/hosts from:

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

to:

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

and now the mail function is now working fine.