Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 235
Suffii Avatar asked Dec 15 '14 06:12

Suffii


People also ask

Why my mail function is not working in PHP?

If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient). Upload the modified php file and retry. Contact your provider if it still does not work. Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

What is the correct syntax of mail () function in PHP?

"\r\n"; mail($to,$subject,$message,$headers);

How do I set the name of an email sender via PHP?

Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string.


2 Answers

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);
like image 81
hardik solanki Avatar answered Sep 20 '22 00:09

hardik solanki


try to send headers as string not an array

$headers = "From: [email protected]\r\n"; 
$headers.= "MIME-Version: 1.0\r\n"; 
$headers.= "Content-Type:text/html;charset=UTF-8\r\n"; 

or using array convert array in string using implode() and send it to mail()

$headers = implode("\r\n", $headers);
mail($email, "Call Back Requert Confirmation", $msg, $headers);
like image 44
Rakesh Sharma Avatar answered Sep 19 '22 00:09

Rakesh Sharma