Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mail function is not working in localhost server

I am on my localhost server. I am using this code below, but I never received the email.

<?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.";
?>
like image 643
user1995927 Avatar asked Feb 10 '13 21:02

user1995927


People also ask

Does mail function work on localhost?

Generally, the email sending feature is not working with the PHP built-in functions in localhost. If the web application is built with PHP, the mail() function is used to send email from the script using PHP. But the PHP mail() function will not work in localhost.

Why is mail function not working?

Make sure the mail() function is called correctly The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>


2 Answers

I know its kinda late...

check if your localhost is configured to send email

On ubuntu terminal try executing following command

# apt-get install sendmail
like image 101
shona Avatar answered Nov 04 '22 10:11

shona


Make sure that the mailserver is configured correctly in the php.ini file.

comment out these lines like this in php.ini file and restart the Apache server.

/*[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
; smtp_port = 25
*/

/*; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost
*/
like image 39
Johannes Mittendorfer Avatar answered Nov 04 '22 12:11

Johannes Mittendorfer