Why mail doesn't work in php?

This is my code:

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\n\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com";
//send the email
$mail_sent = @mail( $to, $subject, $message);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>

- he does not return mail

Please help me

+3
source share
1 answer

There are several reasons why this may fail. The main obstacle to finding the cause is the use of the error management operator (@) before calling the mail () function.

From. $, mail(). , From , . , . mail(), , -f, .

, . mail() SMTP, , .

, , . PHP "\ r\n", "\n\n".

, , :

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\r\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com"; // must be a genuine address
//send the email
$mail_sent = mail($to, $subject, $message, $header);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>
+10

Source: https://habr.com/ru/post/1784230/


All Articles