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' => "info@mysite.com",
               'Content-type' => "text/html;charset=UTF-8"
               );

or

$headers=array(
    'From: "info@mysite.com',
    'Content-Type:text/html;charset=UTF-8',
    'Reply-To: "info@mysite.com'
);

and here is the code

 <?php
   $email = 'test@test.com';
   $headers=array(
    'From: "info@mysite.com',
    'Content-Type:text/html;charset=UTF-8',
    'Reply-To: info@mysite.com'
);

$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: <info@mysite.com>',
        'Content-Type:text/html;charset=UTF-8',
        'Reply-To: <info@mysite.com>'
    );
    $headers = implode("\r\n", $headers);