简体   繁体   中英

Automatic e-mail sending through PHP within HTML form

(TL;DR at the end)

I'm trying to integrate my HTML form into my PHP file so that a mail gets automatically sent to the desired e-mail once the customer hits the "Send" button on my page. I have a lot of other code on my HTML page, so I just included the form.

<form method ="post"action="indexphp.php" id="mailform" name="mailform">

                Navn:               <input type="text" name="Navn" id="name" required><br>

        Kontakt e-post:     <input type="email" name="Epost" id="email" required><br>

        Kontakt telefon:    <input type="tel" name="Telefon" id="phone"><br>

        Fest:<select name="Fest" form="mailform"><option value="Lokalfest">Lokalfest</option><option value="Hjemmefest">Hjemmefest</option></select id="subject"><br>

        Kommune:            <input type="text"name="Kommune"style="width:150px;" required id="message"><br>

        Deltagere:          <input type="text"name="Deltagere"style="width:40px;" required id="message"><br>

        Rydding:            <input type="checkbox"name="Rydding"style="width:40px;" id="message"><br>

        Vasking:            <input type="checkbox"name="Vasking"style="width:40px;" id="message"><br>

        Dorvakt:            <input type="checkbox"name="Dorvakt"style="width:40px;" id="message"><br>

        Noe annet?:         <input type="text"name="Ekstra"style="width:150px;" id="message"><br>

        <input type="submit" value="Send">

Here is my PHP code:

<?php
if(isset($_POST['mailform']))

  $email = $_POST['email'] ;
  $name = $_POST['name'] ;
  $message = $_POST['message'] ;
  $from = $_POST['email'] ;
  $to = "desired mail adress, removed for privacy purposes";
  $subject = $_POST['subject'] ;
  $body = "From: $name\n E-mail: $email\n Message:\n $message";

  $headers = "From: $email" . "\r\n" .
  "Reply-To: $email" . "\r\n" .
  "X-Mailer: PHP/" . phpversion();

  mail ($to, $subject, $body, $headers);
    echo "<p>Bestillingen din har blitt sendt, du vil høre tilbake fra oss straks!</p>";
?>

What happens is that everything gets properly sent to my desired e-mail, BUT, the only content of the mail is: "From: - E-mail: - Message:".

Also there's no subject on the mail.

I am a complete noob in PHP and very basic in HTML so I would appreciate all help and tips!

Thanks in advance.

Have you translated the $_POST values for our sake, or is that your actual code? In your form, you have no field with a name of subject, for example. But in the code you've provided, you're referencing $_POST['subject'], amongst others.

The keys in $_POST match the name attribute on all elements submitted via the form. You can see this easily for yourself via:

print_r($_POST);

Also, though unrelated, IDs in HTML are meant to be unique. You have repeatedly assigned an ID of message throughout many of your elements. This isn't a factor in the issue at hand though, just an aside.

I ended up solving my issue bar one detail. My HTML post-answers:

<form method ="post"action="indexphp.php" id="mailform" name="mailform">
        Navn:               <input type="text" name="Navn" id="name" required><br>

        Kontakt e-post:     <input type="email" name="Epost" id="email" required><br>

        Kontakt telefon:    <input type="tel" name="Telefon" id="phone"><br>

        Fest:<select name="Fest" form="mailform"><option value="Lokalfest">Lokalfest</option><option value="Hjemmefest">Hjemmefest</option></select id="subject"><br>

        Kommune:            <input type="text"name="Kommune"style="width:150px;" required id="message"><br>

        Deltagere:          <input type="text"name="Deltagere"style="width:40px;" required id="message"><br>

        Rydding:            <input type="checkbox"name="Rydding"style="width:40px;" id="message"><br>

        Vasking:            <input type="checkbox"name="Vasking"style="width:40px;" id="message"><br>

        Dorvakt:            <input type="checkbox"name="Dorvakt"style="width:40px;" id="message"><br>

        Noe annet?:         <input type="text"name="Melding"style="width:150px;" id="message"><br>

        <input type="submit" value="Send">

My PHP post-answers:

<?php
if(isset($_POST['mailform']))

  $email = $_POST['Epost'] ;
  $name = $_POST['Navn'] ;
  $phone = $_POST['Telefon'] ;
  $message = $_POST['Melding'] ;
  $kommune = $_POST['Kommune'] ;
  $deltagere = $_POST['Deltagere'] ;
  $rydding = $_POST['Rydding'] ;
  $vasking = $_POST['Vasking'] ;
  $dorvakt = $_POST['Dorvakt'] ;
  $to = "example@example.com";
  $subject = $_POST['Fest'] ;
  $body = "From: $name\n E-mail: $email\n Telefon: $phone\n Kommune: $kommune\n Deltagere: $deltagere\n Rydding: $rydding\n Vasking: $vasking\n Dorvakt: $dorvakt\n Message: $message\n";

  $headers = "From: $email" . "\r\n" .
  "Reply-To: $email" . "\r\n" .
  "X-Mailer: PHP/" . phpversion();

  mail ($to, $subject, $body, $headers);
    echo "<p>Bestillingen din har blitt sendt, du vil høre tilbake fra oss straks!</p>";
?>

My E-mail is still not showing up. I test it and it works perfectly, it's just that on my received e-mails I don't get a sender, but that's not big issue as I get the telephone number.

However, I would prefer the e-mail to show up in the text bar for my e-mail line.

I could fix this but I don't want to screw up the whole e-mailing process that works great aside from what I mentioned.

I'm thinking there's got to be something wrong with the $headers part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM