首页 > 解决方案 > 如何更正此 php 代码,以便在不生成错误代码的情况下发送电子邮件

问题描述

我正在尝试创建一个 HTML 表单,能够将包含工作申请信息的电子邮件发送到我的电子邮件地址,以便我可以回复潜在的申请人。我将提供表单代码以及 PHP 代码,当我单击提交按钮时,我希望将一封包含用户输入信息的电子邮件发送到电子邮件地址,但是我收到了错误消息,但我相信我已经检查过了对于最常见的错误,这些都没有出现,所以我猜我的代码中存在某种逻辑错误:

这是表单代码:

<div class = "form-group purple">
                    <form name = "applyhere" method = "post" action = "jobs.php">
                        <label for = "fullname">Enter Name Here:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "name">
                        </center>
                        <label for = "streetaddress">Street Address:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "address">
                        </center>
                        <label for = "city">City:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "city">
                        </center>
                        <label for = "state">State:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "state">
                        </center>
                        <label for "zip">Zip Code:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "zip" maxlength = "5">
                        </center>
                        <label for = "phone">Phone Number:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "phonenumber">
                        </center>
                        <label for = "timetocall">Best Time to Contact:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "timetocall">
                        </center>
                        <label for = "email">Email Address:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "email">
                        </center>
                        <label for = "eligible">Are you Eligible to Work In United States:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "eligible">
                        </center>
                        <label for = "speciality">Speciality:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "speciality">
                        </center>
                        <label for = "yearsexp">Years of Experience:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "yearexp">
                        </center>
                        <label for = "workexp">Work Experience:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "workexp">
                        </center>
                        <label for = "aboutyou">Tell Us About Yourself:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "aboutyou"></textarea>
                        </center>
                        <label for = "education">Enter Education History College/Years Attended/Major:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "education"></textarea>
                        </center>
                        <label for = "tradeschool">Enter Trade School History: School Attended/Years Attended/Major:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "tradeschool"></textarea>
                        </center>
                        <label for = "jobtitle">Job Title Applying For:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "jobtitle">
                        </center>
                        <label for = "availability">Enter Job Availability:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "availability">
                        </center>
                        <label for = "startdate">When Can you Start Working:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "startdate">
                        </center>
                        <label for = "workhistory">Enter Your Work History Here<i>Names,
                            Addresses, Phone Number of Previous Employee, Supervisors Name,
                            Dates of Employment, Reason for Leaving</i>:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "workhistory"></textarea>
                        </center>
                        <label for = "reference1">Enter First Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference1">
                        </center>
                        <label for = "reference2">Enter Second Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference2">
                        </center>
                        <label for = "reference3">Enter Third Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference3">
                        </center>
                        <center>
                            <input class = "TxtCustomForm BtnSubmit" type = "submit" name = "Submit Form">
                        </center>
                    </form>
                </div>

这是我的 PHP 代码,它清理了答案,并应该通过电子邮件将它们发送给我:

<?php

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "*****.***.****@gmail.com";
    $email_subject = "Job Application";

    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['fullname']) ||
        !isset($_POST['address']) ||
        !isset($_POST['city']) ||
        !isset($_POST['state']) ||
        !isset($_POST['zip'])  ||
        !isset($_POST['phone'])  ||
        !isset($_POST['timetocall'])  ||
        !isset($_POST['email'])  ||
        !isset($_POST['eligible'])  ||
        !isset($_POST['speciality'])  ||
        !isset($_POST['yearexp'])  ||
        !isset($_POST['workexp'])  ||
        !isset($_POST['aboutyou']) ||
        !isset($_POST['education']) ||
        !isset($_POST['tradeschool']) ||
        !isset($_POST['jobtitle']) ||
        !isset($_POST['availability']) ||
        !isset($_POST['startdate']) ||
        !isset($_POST['workhistory']) ||
        !isset($_POST['reference1']) ||
        !isset($_POST['reference2']) ||
        !isset($_POST['reference3'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $name = $_POST['name']; // required
    $address = $_POST['address']; // required
    $city = $_POST['city']; // required
    $state = $_POST['state']; // not required
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $timetocall = $_POST['timetocall'];
    $email = $_POST['email'];
    $eligible = $_POST['eligible'];
    $speciality = $_POST['speciality'];
    $yearsexp = $_POST['yearsexp'];
    $workexp = $_POST['workexp'];
    $aboutyou = $_POST['aboutyou'];
    $education = $_POST['education'];
    $tradeschool = $_POST['tradeschool'];
    $jobtitle = $_POST['jobtitle'];
    $availability = $_POST['availability'];
    $startdate = $_POST['startdate'];
    $workhistory = $_POST['workhistory'];
    $ref1 = $_POST['reference1'];
    $ref2 = $_POST['reference2'];
    $ref3 = $_POST['reference3'];

     // required

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

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

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

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

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

    $email_message = "Form details below.\n\n";


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



    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Address: ".clean_string($address)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "Zip: ".clean_string($zip)."\n";
    $email_message .= "Phone: ".clean_string($phone)."\n";
    $email_message .= "When To Call: ".clean_string($timetocall)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Work In US?: ".clean_string($eligible)."\n";
    $email_message .= "Speciality: ".clean_string($speciality)."\n";
    $email_message .= "Years Exp: ".clean_string($yearexp)."\n";
    $email_message .= "Work Exp: ".clean_string($workexp)."\n";
    $email_message .= "Education: ".clean_string($education)."\n";
    $email_message .= "Trade School: ".clean_string($tradeschool)."\n";
    $email_message .= "Job Applied For: ".clean_string($jobtitle)."\n";
    $email_message .= "Availability: ".clean_string($availability)."\n";
    $email_message .= "Start Date: ".clean_string($startdate)."\n";
    $email_message .= "workhistory: ".clean_string($workhistory)."\n";
    $email_message .= "Reference 1: ".clean_string($ref1)."\n";
    $email_message .= "Reference 2: ".clean_string($ref2)."\n";
    $email_message .= "Reference 3: ".clean_string($ref3)."\n";

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

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

Thank you for contacting us. We will be in touch with you very soon.

<?php

?>

请注意,我是 PHP 新手,通常只对静态网页进行编码,我不确定接下来的步骤。

标签: phpphpmailer

解决方案


推荐阅读