Mail sent with php mail () has an empty field

I am sending email with the PHP mail function. It works the way it should, except that all mail clients display an empty From field. This is how I use this function:

mail( 'mail@example.com', 'Example subject', $msg, 
       implode( '\r\n', array( 'Content-Type: text/html; charset=UTF-8', 'From: test@example.com') ) );

As I said, everything works fine, except for the From field field, everything is empty when a message arrives. Any idea why this is happening?

+3
source share
2 answers

try it

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Against your idea Check it out what you get

  <?php 
    $implode =implode( "\r\n", array( 'Content-Type: text/html; charset=UTF-8', 'From: test@example.com') );

    echo "<pre>";
    print_r($implode);
    ?>
+2
source

You are missing a quote in front of your "content type" and the error log is likely to fail, so it ignores the problem and gets confused with the code parsing.

:

mail( 'mail@example.com', "Example subject", $msg, 
       implode( "\r\n", array( 'Content-Type: text/html; charset=UTF-8', 'From: test@example.com') ) );
+1

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


All Articles