Search code examples
phpemailemail-headers

Having Issue on Setting Array of $headers in PHP Mail Function


I am not able to send email through PHP mail function while I specify an array of $headers as

$headers = array (
               'From' => "[email protected]",
               'Content-type' => "text/html;charset=UTF-8"
               );

or

$headers=array(
    'From: "[email protected]',
    'Content-Type:text/html;charset=UTF-8',
    'Reply-To: "[email protected]'
);

and here is the code

 <?php
   $email = '[email protected]';
   $headers=array(
    'From: "[email protected]',
    'Content-Type:text/html;charset=UTF-8',
    'Reply-To: [email protected]'
);

$msg= 'This is a Test';

 mail($email, "Call Back Requert Confirmation", $msg, $headers);
?>

can you please let me know why this is happening? and how I can fix this?


Solution

  • If you want to send $headers through array, then you need to add \r\n to the end of each header value and convert the array into a string.

    Your code should be:

    $headers = array(
        'From: <[email protected]>',
        'Content-Type:text/html;charset=UTF-8',
        'Reply-To: <[email protected]>'
    );
    $headers = implode("\r\n", $headers);