简体   繁体   English

通过HTML表单通过PHP自动发送电子邮件

[英]Automatic e-mail sending through PHP within HTML form

(TL;DR at the end) (TL; DR结尾)

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. 我正在尝试将HTML表单集成到我的PHP文件中,以便一旦客户点击我页面上的“发送”按钮,邮件就会自动发送到所需的电子邮件中。 I have a lot of other code on my HTML page, so I just included the form. HTML页面上还有很多其他代码,因此我只包含了表单。

<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代码:

<?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! 我在PHP中是一个完全菜鸟,而在HTML中却是一个非常基础的人,因此,我将感谢所有帮助和提示!

Thanks in advance. 提前致谢。

Have you translated the $_POST values for our sake, or is that your actual code? 您是否为了我们而翻译了$ _POST值,或者这是您的实际代码? 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. 但是在提供的代码中,您引用的是$ _POST ['subject']等。

The keys in $_POST match the name attribute on all elements submitted via the form. $ _POST中的键与通过表单提交的所有元素上的name属性匹配。 You can see this easily for yourself via: 您可以通过以下方式轻松地自己查看:

print_r($_POST);

Also, though unrelated, IDs in HTML are meant to be unique. 同样,尽管不相关,但HTML中的ID应当是唯一的。 You have repeatedly assigned an ID of message throughout many of your elements. 您已经在您的许多元素中反复分配了消息ID。 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: 我的HTML后答案:

<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后答案:

<?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. 我在想$ headers部分肯定有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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