Tuesday, 28 August 2018

PHP contact form does not send e-mail

This question already has an answer here:

  • PHP mail function doesn't complete sending of e-mail 23 answers
Ive made a Contact form for my Club. here And now i got stuck with the issue that the form won't deliver the Information from the form which had been inputted to the email address. I don't know if there is a Problem with the html Code or PHP so ill post both. HTML:
<form action="#" method="post" style="max-width: 75%;">
  <table width="100%" border="0" cellpadding="3" cellspacing="1">
    <tr>
      <td align="right">*Anrede:</td>
      <td align="left">
        <input type="radio" name=Anrede value="Hr.">Herr
        <input type="radio" name=Anrede value="Fr.">Frau</td>
    </tr>
    <tr>
      <td width="140" align="right">*Nachname:</td>
      <td align="left">
        <input type="text" name="nachname" value="" style="width:100%;" />
      </td>
    </tr>
    <tr>
      <td align="right">*Vorname:</td>
      <td align="left">
        <input type="text" name="vorname" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">Firma:</td>
      <td align="left">
        <input type="text" name="firma" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Postanschrift:</td>
      <td align="left">
        <input type="text" name="strasse" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Telefon:</td>
      <td align="left">
        <input type="text" name="telefon" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*E-Mail:</td>
      <td align="left">
        <input type="text" name="email" value="" style="width:100%" />
      </td>
    </tr>
    <tr>
      <td align="right">*Buchungstag:</td>
      <td align="left">
        <input type="text" id="datepicker" />
      </td>
    </tr>
    <tr>
      <td align="right">*Erwachsene:</td>
      <td align="left">
        <select name="Erwachsene" style="width: 145px;">
          <option selected="selected" disabled="disabled">Bitte ausw&auml;hlen</option>
          <?php for ($i=0 ; $i <=3 0; $i++) : ?>
          <option value="<?php echo $i; ?>">
            <?php echo $i; ?>
          </option>
          <?php endfor; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right">*Jugend:</td>
      <td align="left">
        <select name="Jugend" style="width: 145px;">
          <option selected="selected" disabled="disabled">Bitte ausw&auml;hlen</option>
          <?php for ($i=0 ; $i <=2 5; $i++) : ?>
          <option value="<?php echo $i; ?>">
            <?php echo $i; ?>
          </option>
          <?php endfor; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right">*Event:</td>
      <td align="left">
        <input type="radio" name=myradio value="extern" checked>extern
        <input type="radio" name=myradio value="intern">intern</td>
    </tr>
    <tr>
      <td colspan="2">
        <textarea rows="12" style="width:100%" name="text" placeholder="zus&auml;tzliche Informationen ..."></textarea>
      </td>
      <td></td>
    </tr>
    <tr>
      <td colspan="2" align="left" nowrap>
        <br />
        <input type="reset" value="Formular l&ouml;schen" style="width:49%" />
        <input type="submit" name="submit" value="Formular absenden" style="width:49%" />
      </td>
    </tr>
  </table>
</form>
<p>*Pflichtfelder</p>
<br />

PHP:
<?php
if(isset($_POST['submit'])){

$anrede = $_POST['Anrede'];
$last_name = $_POST['Nachname'];//required
$first_name = $_POST['Vorname'];//required
$Firma = $_POST['Firma'];
$Anschrift = $_POST['Postanschrift'];//required
$tel = $_POST['telefon'];//required
$from = $_POST['email'];//required
$tag = $_POST['buchungstag'];//required
$erwach = $_POST['Erwachsene'];//required
$jugend = $_POST['Jugend'];//required
$zusatz = $_POST['text'];

$to= "kontakt@acherner-bogenclub.de , $from";
$subject = "Anfrage auf belegung des Bogenplatzes";

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$from)) {

$error_message .= 'bitte einen gultige email eingelben.<br />';

}

$string_exp = "/^[A-Za-z .']+$/";

if(!preg_match($string_exp,$F_name)) {

$error_message .= 'Dein vorname ist nicht gültig.<br />';

}

$string_exp = "/^[A-Za-z ']+$/";

if(!preg_match($string_exp,$Anschrift)) {

$error_message .= 'bitte eine gültige addresse eingeben.<br />';

} 

if(!preg_match($string_exp,$L_name)) {

$error_message .= 'Dein nachname ist nicht gültig<br />';

}

if(isset($_REQUEST['Erwachsene']) && $_REQUEST['Erwachsene'] == '0') {
echo 'Bitte anzahl der Erwachsene auswählen.<br /> '; 

}
if(isset($_REQUEST['Jugend']) && $_REQUEST['Jugend'] == '0') {
echo 'Bitte anzahl der Jugendlichen auswählen.<br /> '; 

}
  //datepicker validation????

if(strlen($error_message) > 0) {

die($error_message);

}

$email_message = "Kontaktformular details siehe unten.\n\n";

function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

$email_message .= "Anrede: ".clean_string($anrede)."\n";

$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Firma: ".clean_string($Firma)."\n";

$email_message .= "Postanschrift: ".clean_string($Anschrift)."\n";

$email_message .= "Telefonnummer: ".clean_string($tel)."\n";

$email_message .= "Email: ".clean_string($from)."\n";

$email_message .= "Buchungstag: ".clean_string($tag)."\n";

$email_message .= "Anzahl der Erwachsenen: ".clean_string($erwach)."\n";

$email_message .= "Anzahl der Jugendlichen: ".clean_string($jugend)."\n";

$email_message .= "Zusätzliche Informationen: ".clean_string($zusatz)."\n";

// create email headers

$headers = 'From: '.$from."\r\n".

@mail($to, $email_subject, $email_message, $headers);  

?>

<!-- include your own success html here -->

Vielen dank. Wir werden sobald wie möglich mit ihnen in verbindung treten. 

<?php

}

?>

I hope that somebody could help me with that and also the Validation does not work for some reason and I've tried so many different ways but never worked.

Because your <form action='' tab is empty, the form is not submitting data when sending and reloading the page. By simply setting action='#' I have made your page work in as much as the print_r($_POST); now correctly displays the form content.
P.S> PHPMailer is absolutely the way to go, regarding PHP emails.
Further Notes:

 if(strlen($error_message) > 0) {
   died($error_message);
 }

The Die funtion should not be in the past tense.

$email_subject as seen in the mail(); function is an undefined variable.

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Anzahl der Erwachsenen: ".clean_string($erwach)."\n";

These three variables ($first_name,$ast_name,$erwach) are also undefined.

PLEASE read and check your own code before coming to Stack Overflow. We love to help but we can't be expected to be your typo- and error- checkers.

Debug:
if (mail($to, $email_subject, $email_message, $headers)){
    die("Message was sent ok!!");
}


Your clean_string function looks pretty pointless and should be expanded into a full string cleaning function rather than an ad-hoc removal system.

0 comments:

Post a Comment