简体   繁体   中英

php mail() function not working in linux server

I'm working on PHP mail() function. I have created a test file in my live site hosted in linux server. This is my test file looks like

test.php

<?php
ini_set('SMTP','smtp.mydomain.com');
ini_set('smtp_port',25);
$to      = 'xyz@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: abc@gmail.com' . "\r\n" .
'Reply-To: abc@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
    echo 'Successfully sent';
}
else {
    echo 'failed';
}
?>

But I didn't receive any mail. It always shows failed .
Is there anything missing?
I checked for a solution, but still its not working
UPDATE :

<?php
if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
    echo 'mail() has been disabled';
}
error_reporting(E_ALL); 
ini_set('display_errors', '1');

$to      = 'xyz@gmail.com';
$subject = 'the subject';
$message = 'hello';
if(mail($to, $subject, $message, "From:abc@mydomain.in")){
    echo 'Successfully sent';
}
else {
    print_r(error_get_last());
    echo '  failed';
}

?>

OUTPUT :

mail() is available failed

Try to run this and restart apache2

sudo apt-get install sendmail sudo sendmailconfig (answer Yes to everything) sudo service apache2 restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM