-3
<?php

$method = $_SERVER['REQUEST_METHOD'];

//Script Foreach
$c = true;
if ( $method === 'POST' ) {

    $project_name = "[www]";
    $admin_email  = "example@gmail.com";
    $form_subject = "xxx";

    foreach ( $_POST as $key => $value ) {
        if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
            $message .= "
            " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
(((( error here ))))<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
        </tr>
        ";
        }
    }
}

$message = "<table style='width: 100%;'>$message</table>";

function adopt($text) {
    return '=?UTF-8?B?'.Base64_encode($text).'?=';
}

$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
'Reply-To: '.$admin_email.'' . PHP_EOL;

mail($admin_email, adopt($form_subject), $message, $headers );

The error message from the. Error log from cPanel contains the following:

[17-Dec-2022 16:31:13 Asia/Jerusalem] PHP Warning:  Undefined variable $message in /home/drawdepc/public_html/mail.php on line 18

Before this, the code always worked fine, now it gives some kind of error that I can't understand. I use cPanel.

Jordan
  • 1,390
  • 2
  • 9
  • 24

2 Answers2

0

This is happening because the $message variable is being used before it is defined. To fix this, you can just do a simple trick, define the $message variable at the beginning of your code with an empty string.

<?php

$method = $_SERVER['REQUEST_METHOD'];

// initialize the message variable
$message = "";

//Script Foreach
$c = true;
if ( $method === 'POST' ) {

    $project_name = "[www]";
    $admin_email  = "example@gmail.com";
    $form_subject = "xxx";

    foreach ( $_POST as $key => $value ) {
        if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
            $message .= "
            " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
        </tr>
        ";
        }
    }
}

$message = "<table style='width: 100%;'>$message</table>";

function adopt($text) {
    return '=?UTF-8?B?'.Base64_encode($text).'?=';
}

$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
'Reply-To: '.$admin_email.'' . PHP_EOL;

mail($admin_email, adopt($form_subject), $message, $headers );

One reason for this code working before could be is, maybe recently you have turned the "Display Errors" from the PHP INI editor. Just simply turn that off, and this Warning will be hidden. But, its recommended to fix the code as the way I shown above.

TECH FREEKS
  • 188
  • 8
  • Do you know why I have html code in gmail. I used this file before and I had a table with form data https://prnt.sc/kIU-z2YjNHe6 – Никита Тыцкий Dec 17 '22 at 16:24
  • I would recommend you create another Question for this so that the one who can help can answer. Currently, without seeing the full code, I am unable to assist. – TECH FREEKS Dec 18 '22 at 06:40
-1

Variables still need to be declared in PHP, although they can be declared and set in the same line.

In your code you are "appending" to the $message variable by using .=. PHP is therefore trying to append your code to the $message variable which doesn't exist yet.

In order to fix this, you either need to declare the $message variable first (and set to an empty string):

$form_subject = "xxx";
$message = "";

or change the first declaration of $message to set it rather than append to it:

if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
    $message = ( ($c = !$c) ? '<tr>' : '<tr style="background-color: #f8f8f8;">' ) . "
    <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
    <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td></tr>";
}
Jordan
  • 1,390
  • 2
  • 9
  • 24