如何从网站php代码设置HTML电子邮件?

时间:2018-02-12 14:33:30

标签: php html

我聘请了某人帮助我访问我的网站,在联系我们页面上,他们使用了您在下面链接中看到的代码(不幸的是,他们停止了工作)。唯一的问题是它不能100%工作。他做了一些更改,所以当我收到电子邮件时,用户的电子邮件是空白的。不知道下面需要改变什么,所以我可以看到它,还有任何无用的代码要删除?

以下是他从以下网站获取代码的网站:

http://www.freecontactform.com/email_form.php

这是他的所作所为:

/3b84/

以下是代码的第二部分:

<form action="send_form_email.php" method="post" name="contactform">
<div class="col-50"><input id="lname" name="last_name" placeholder="Name" required="required" type="text" size="40"/> <!--     <label for="email1" >Email</label>--> <input id="lemail" name="Email" placeholder="Email" required="required" type="email" /></div>

<div class="col-40"><!--    <label for="subject">Subject</label>--><textarea id="subject" name="comments" placeholder="Thoughts,suggestions or questions, let me know." required="required" style="height:200px"></textarea><input name="submit" type="submit" value="Submit" /></div>
</form>
</div>
<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script></body>
</html>

code

1 个答案:

答案 0 :(得分:0)

我相信$ _POST区分大小写。您还可以尝试其他一些清理工作。您正在尝试清除不存在的变量$ email_account。 “if”语句没有特定的代码块。下面我将输入的html名称从Email更改为电子邮件,因此php代码可以保持大致相同。我将'telephone'变量更新为$ email_account,因为这是您用来创建$ message变量的。

HTML格式:

<form action="send_form_email.php" method="post" name="contactform">
    <div class="col-50">
        <input id="lname" name="last_name" placeholder="Name" required="required" type="text" size="40"/> 
        <input id="lemail" name="email" placeholder="Email" required="required" type="email" />
    </div>

    <div class="col-40">
        <textarea id="subject" name="comments" placeholder="Thoughts,suggestions or questions, let me know." required="required" style="height:200px"></textarea>
        <input name="submit" type="submit" value="Submit" />
    </div>
</form>
</div>
<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous">
 </script>

PHP文件:

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "jgomez.jr@att.net";
    $email_subject = "Journal Entries Request/Comment";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['comments'])) {
         died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_account = $_POST['email']; // not required
    $comments = $_POST['comments']; // required

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_account)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>