I'm trying to send an email through the PHP function: mail()
.
It doesn't work and when I'm trying to see what the error message is, by using the error_get_last()
=> error_get_last()['message']
function, I only get: Undefined offset: 3
.
Of what I can see, the $headers
is the error, since it is the 3rd key in the mail()
function. I've tried multiple variants of headers. Examples I've tried:
I even tried to copy and only use the very simple example from this post:
Here is the full code I've been using:
$toEmail = "Receiver Example < receiver@example.com >";
$subject = "Test email";
$text = "
<html>
<head>
<title>Email title</title>
</head>
<body>
<h1>Thank you for reading this!</h1>
<table cellspacing='0' style='width: 100%;'>
<tr>
<th>Test</td>
</tr>
</table>
</body>
</html>";
$headers = array(
"From" => "Test Example < test@example.com >",
"Reply-To" => "Reply < reply@example.com >",
"X-Sender" => "Reply < reply@example.com >",
"Return-Path" => "reply@example.com",
"Content-Type" => "text/html; charset=UTF-8",
"MIME-Version" => "1.0",
"X-Mailer" => "PHP/" . phpversion()
);
$sendEmail = mail($toEmail, $subject, $text, $headers);
if(!$sendEmail){
return "Error: ".error_get_last()['message'];
}
Alternative header I've also tried, but with no success:
$headers = "From: Test Example < test@example.com >\n";
$headers .= "Reply-To: Reply < reply@example.com >\n";
$headers .= "X-Sender: Reply < reply@example.com >\n";
$headers .= "Return-Path: reply@example.com\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
Do you have an idea of what can be wrong?