使用php和if-statement将某些html变量添加到邮件程序变量中

问题描述:

While writing my website I encountered the following Problem: On the first page, you can enter some data like names, dates, addresses etc. In addition, there are a few checkboxes with fixed data. After filling out the form I want it to get sent to my e-mail, which is working for the most part.

<?php
//subject of the e-mail
$subject = "Test";

// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
    "Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
sendMailInt($subject, $msg);
?>

Now I want to add some input with checkboxes on my website, which also works up to a certain point.

<?php
//subject of the e-mail
$subject = "Test";

// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
    "Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>" .
    "checkbox1: " . $_POST['checkbox1'] . "<br>" .
    "checkbox2: " . $_POST['checkbox2'] . "<br>" .
    "checkbox3: " . $_POST['checkbox3'] . "<br>" .
    "checkbox4: " . $_POST['checkbox4'] . "<br>" .
    "checkbox5: " . $_POST['checkbox5'] . "<br>" .
    "checkbox6: " . $_POST['checkbox6'] . "<br>" .
    "checkbox7: " . $_POST['checkbox7'] . "<br>" .
    "checkbox8: " . $_POST['checkbox8'] . "<br>" .
    ;

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
sendMailInt($subject, $msg);
?>

Specifically for the checkbox input, I want to have something like an if-statement to check if the box has been checked or not. If a box is unchecked, the value is empty, which means I have an empty line in my e-mail. Since I have around 20 checkboxes, it can get a bit messy in the e-mail, if 10 boxes in a row are unchecked.

I tried writing the $msg = as HTML mail and adding PHP code into it, but then my website stopped working (went completely blank, an error I encountered a few times before, when using wrong code)

<?php
//subject of the e-mail
$subject = "Test";

// the message
$msg = "
<html>
<head>
<title>Test Mail</title>
<body>
<?php
    echo "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
    "Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>"; 
    if ($_POST['checkbox1'] == true)
        echo $_POST['checkbox1'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox2'] == true)
        echo $_POST['checkbox2'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox3'] == true)
        echo $_POST['checkbox3'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox4'] == true)
        echo $_POST['checkbox4'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox5'] == true)
        echo $_POST['checkbox5'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox6'] == true)
        echo $_POST['checkbox6'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox7'] == true)
        echo $_POST['checkbox7'] . "<br>";
    else
        echo "";

    if ($_POST['checkbox8'] == true)
        echo $_POST['checkbox8'] . "<br>";
    else
        echo "";
?>
</body>
</html>
";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
sendMailInt($subject, $msg);
?>

Is there any way to skip empty variables in the mailer variable, so that the mail that gets sent looks clean without holes?

Looking forward to hearing from you guys and thanks in advance.

First thing you can do is in the file you're working with to force errors to show on the page (only use this in development environments):

error_reporting(E_ALL);
ini_set('display_errors', 1);

Second, multiple form inputs of the same data type can be greatly simplified by putting them in an array. Here's what the syntax would look like:

<input name="checkbox[]" value="1" />
<input name="checkbox[]" value="2" />
<input name="checkbox[]" value="3" />
<input name="checkbox[]" value="4" />
<input name="checkbox[]" value="5" />

Then in PHP you could do something like this that will only print out the checkboxes that were selected:

foreach ($_POST['checkbox'] as $checkbox) {
  echo $checkbox . "<br>";
}