search

Home  >  Q&A  >  body text

PHP mail sends empty email

I am similar to php, I don’t understand where the problem lies.

Sometimes php functions send me empty messages like

Parent’s Name

too late:

Lynn:

telephone number:

e-mail:

date of birth:

Message text:

But it should be filled with values ​​like this

Parent Name Test

Too Many Mistakes: Test

Lynn: Test

Phone Number: Test

Email: test@test

Date of birth: 21313

Message text: Test

This is my php code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Обратная Связь</title>
  </head>
  <body>
    <?php
      if (isset($_POST['parent'])) {$parent = $_POST['parent'];}
      if (isset($_POST['child'])) {$child = $_POST['child'];}
      if (isset($_POST['contacts'])) {$contacts = $_POST['contacts'];}
      if (isset($_POST['email'])) {$email = $_POST['email'];}
      if (isset($_POST['bbd'])) {$bbd = $_POST['bbd'];}
      if (isset($_POST['city'])) {$city = $_POST['city'];}
      if (isset($_POST['mess'])) {$mess = $_POST['mess'];}
 
      $to = "info@test.ee"; /*Укажите ваш адрес электоронной почты*/
      $headers = "Content-type: text/plain; text/html; charset=utf-8";
      $subject = "Kontakti Info";
      $message = "Vanema nimi $parent \n Lapse nimi: $child \nLinn: 
      $city \nTelefoninumber: $contacts \nEmail: $email \nSünnikuupäev: $bbd \nSõnumi tekst: $mess";
      $send = mail ($to, $subject, $message, $headers);
      if ($send == 'true')
        {
        echo "<b>Спасибо за отправку вашего сообщения!<p>";
        echo "<a href=index.php>Нажмите,</a> чтобы вернуться на главную страницу";
        }
      else 
        {
        echo "<p><b>Ошибка. Сообщение не отправлено!";
        } 
    ?>
  </body>
</html>

<?php
  header('Location: https://test.ee/aitah.html ');
?>

Please give me advice, what went wrong.

P粉225961749P粉225961749255 days ago495

reply all(1)I'll reply

  • P粉769045426

    P粉7690454262024-04-01 09:38:47

    If your script is just a form handler, you can e.g. add if(empty($_POST)) { die('No form data!'); } to the top to prevent it from running, Unless the response form is submitted.

    If you need to fill in all fields, you must check each field before processing the email. You could cram all of these isset into one giant if(isset(...) statement. However, there is a simpler, more readable way to do it .First, let’s set up a few variables:

    // Your required fields:
    $fields = ['parent', 'child', 'contacts', 'email', 'bbd', 'city', 'mess'];
    // Your data is collected here:
    $data = [];
    // Any errors are collected here:
    $errors = [];

    We then loop over the fields and if the value exists, add it to $data, otherwise we add an error comment.

    // Loop to check your required fields:
    
    foreach($fields as $field) {
        // If value exists, add to $data:
        if(!empty($_POST[$field])) {
            $data[$field] = $_POST[$field];
        }
        // Else add error:
        else {
            $errors[] = 'Missing field: ' . $field;
        }
    }
    
    if(empty($errors)) {
        // No errors, send your email
        // You can use "Vanema nimi {$data['parent']}...",
        // ... otherwise: extract($data) to use $parent etc.
    }
    else {
        // You could report those errors, or redirect back to the form, or whatever.
    }

    If an error occurs (= missing field), the email will not be sent. As a bonus, you now have a reusable piece of code that can be used in other forms with similar functionality by simply modifying the $fields array. (If you really need to reuse it, it's a good idea to wrap it into a function; don't copy-paste the code. function x($post, $fields) { ... } Used for basic manipulation helpers function.)

    Please note that here we use empty instead of isset. If a blank form is submitted, field will be set (to the empty string ""). Also note that empty returns true for anything equal to false (i.e. "", 0, false, <代码>null, []). ( If "0" is an expected and acceptable value, please note its "emptiness"! ) On the other hand, isset for anything that is not # The content of ##null returns true.

    Side note If the above code is complete and your script just handles form data and redirects, then you don't need the HTML wrapper at all. It never displays.

    reply
    0
  • Cancelreply