Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mail takes long time in localhost

I am using Ubuntu.I installed sendmail in my local host using the following command

sudo apt-get install sendmail

Now I would like to check whether mail goes from my localhost using following php code.

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

When i execute the code, It takes very long time and finally echo the message as Mail Sent. Is there any possibilities to fix this ?

like image 818
mymotherland Avatar asked Sep 28 '11 06:09

mymotherland


People also ask

Why does PHP mail take so long?

It is the SMTP mail delivery (which PHP hands off the message to) which is taking time. Possibly, the delay you see is greylisting on the receiving server, meaning that the receiving mail server refuses to accept the message until the sending server (which your PHP script handed it to) tries a few times.

Can we send mail from localhost in php?

We can send mail from our localhost using some mail configuration by XAMPP/LAMP/WAMP server, First we need to enable php_openssl php extensions from php. ini file. I am using GMAIL SMTP server to send mail from localhost and sendmail package,It is a mail transport agent which can be found in php.


2 Answers

Edit the file /etc/hosts and make sure the first line is the following:

127.0.0.1 localhost.localdomain localhost myhostname

Edit the sendmail configuration file ( /etc/mail/sendmail.cf in Ubuntu) and Uncomment the line #O:

O HostsFile=/etc/hosts

Restart the computer, or run sudo service sendmail restart.

The computer should boot up much faster now and the mail() function should return almost immediately.

HOWEVER, the emails won't actually be sent unless you follow step 5.

You must new use the sendmail -f option whenever using the mail function.

For example:

mail('[email protected]', 'the subject', 'the message', null, '[email protected]');
like image 58
Nibu Joshwa Avatar answered Sep 22 '22 03:09

Nibu Joshwa


I know this question has already been answered, but I'm posting this in the hope it may help someone else looking for a different solution to this issue.

For me, I just needed to put my server's Fully Qualified Domain Name (FQDN) into /etc/mailname. For example: server.example.com.

Restart Sendmail to apply the changes.

$ sudo service sendmail restart

like image 39
Wireblue Avatar answered Sep 23 '22 03:09

Wireblue