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 ?
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.
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.
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]');
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With