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("test@test.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:
O HostsFile=/etc/hosts
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”
Post comment
About Me
I am an Opensource Developer. Currently I am working in a company as a Solution Architect. I have around 7 years of Web development experience in Advanced and Core PHP Programming and MySQL and PgSQL database as back end. It has been long time that I have nott been involved with RoR, Perl and ASP, Visual Basic but I had also worked with them. Specialties: PHP, Zend, YII, CodeIgniter, Joomla, Magento, JavaScript, MySQL, PgSQL, HTML, CSS, RoR, Perl, ASP!
Categories
- About Me (2)
- Apache (1)
- Facebook (1)
- JavaScript (6)
- Joomla (10)
- jQuery (5)
- Magento (10)
- MySQL (2)
- Opensource (2)
- PHP (18)
- phpmyadmin (2)
- Programming (19)
- Ubuntu (2)
- Zend Framework (1)
- कच्याक कुचुक (1)



Dmitry Kozlovich says:
It really helped to me, thank you.
Brendan says:
Thanks! Worked like a charm