-1

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?

Rune
  • 9
  • 1
  • Can you var_dump the entire `error_get_last()` return? I'm not convinced that error is from mail. – Jonnix Jul 29 '19 at 09:37
  • Jonnix - Thank you for your reply! I've should have used the var_dump() before posted the post, because it's showing me that the error is coming from a total different file that I've used the require_once() function. I will keep going on with the investigation :) – Rune Jul 29 '19 at 09:44
  • The code as shown does not seem to produce any such error at all. _“Of what I can see, the $headers is the error, since it is the 3rd key in the mail() function.”_ - that appears to be completely the wrong place to look - no one is trying to access any specific array element by any offset there. The error message contains file and line number of where it occurred, so go check what is actually there. – misorude Jul 29 '19 at 09:44

1 Answers1

0

Thanks to Jonnix I figured out that the problem is from some very old code I've been using, from a different place in the file directory because of the require_once() function.

The solution was simple, use var_dump() on the error_get_last() function! It showed me this:

array(4) {

["type"]=> int(8)

["message"]=> string(19) "Undefined offset: 3"

["file"]=> string(72) "*CORRUPTED FILE LOCATION*"

["line"]=> int(59)

}

Rune
  • 9
  • 1