I assume that apache, PHP is running quite fine. But the sending emails from local machine is not working. You need to installl the sendmail and set the path in php.ini. To install follow the below simple steps:
1. Open the terminal and run the following command to install send mail and php mail.
sudo apt-get install sendmail php-mail postfix
2. Uncomment and change the sendmail_path in your file /etc/php5/apache2/php.ini. The line should now look like this:
sendmail_path = /usr/sbin/sendmail -i -t
Somethings without restarting the Apache it might not work so simply restart the apache server once:
$ /etc/init.d/apache2 restart
Now try to run the following PHP mail function:
if(mail("email@example.com", "This some subject", "This is some message body")) echo "Sent"; else echo "Not sent";
If the mail is sent successfully quickly then fine. But sometimes it does not send the email quickly. It takes sometime i.e. 2 minutes in my case. I googled a lot but could not find a quite proper solution. When I saw the mail log and came to know somehow that it is because not resolving the hostname. I checked my hostname “rajug” in /etc/hosts file which was missing.
1. To check the hostname of your system, run the following command if you don’t know the hostname.
$ cat /etc/hostname # rajug in my case.
2. Make sure something like following line is at your top of the /etc/hosts file.
127.0.0.1 localhost.localdomain localhost
To check it open the file in editor (I use gedit):
$ gedit /etc/hosts
And added the line at top. Actually localhost was there but my hostname “rajug” was not there. So I just added it.
3. Thirdly, edit the sendmail configuration file (/etc/mail/sendmail.cf in Ubuntu) and Uncomment the line:
4. Now restart the sendmail and apache once.
$ /etc/init.d/sendmail restart $ /etc/init.d/apache2 restart
Now try the above PHP script to send the email, it will send quite faster (2/3 seconds in my case).
Hope this will help others having the same problem !
2 Comments to “Slow email sending in Ubuntu 12.04 LTS solution”