提交网络表单后无法发送电子邮件

时间:2019-06-05 07:40:02

标签: php email

我有一个Web表单,用户可以填写该表单,我的目标是要发出一条通知电子邮件,告知您已提出票证。我有2个单独的php页面,其中一个名为index.php,它托管表单,而sendEmail.php则包含电子邮件详细信息。

如果我将sendEmail.php包含在index.php中,则可以正常工作,但是每次页面刷新时我都会发现发送的电子邮件,因此,我一直在尝试修改代码,使其仅在用户单击“提交”时运行< / p>

  <?php
  if(isset($_POST["submit"])){
         $to  = 'email1@domain.com';
         $subject = 'test subject';
         $message = 'test message';
         $headers  = 'Cc: email2@Sdomain.com';
         mail($to,$subject,$message,$headers);
       }
       else
       {
         echo 'Error';
       }
  ?>

使用上面的代码没有任何反应,我没有收到错误,也没有收到电子邮件。我的按钮的输入类型和名称都称为“提交”

编辑:下面的HTML表单...

    <div id="NewTicket" class="tabcontent">
        <form id="newForm" action="">
            <p style='font-weight: bold'>Which area do you want to raise?
            <select id='businessDropdown'; name='businessDropdown'; onchange='businessChanged(this.value)'; style='background-color: #ffff99; margin-left: 15px';>
            <option value="">- Select business -</option>
    <?
    if (isset($business)) {
        if ($business=="PSM") {
            echo "<option value='PSM' selected>PSM</option>\n";
            echo "<option value='CMS'>CMS</option>\n";
        } else {
            echo "<option value='PSM'>PSM</option>\n";
            echo "<option value='CMS' selected>CMS</option>\n";

        }
    } else {
        echo "<option value='PSM'>PSM</option>\n";
        echo "<option value='CMS'>CMS</option>\n";
    }
    ?>
    </select>
    </p>

    <p style='font-weight: bold'>Filter by a particular model?
    <select style='min-width: 50%; max-width: 90%; background-color: #ffff99; margin-left: 50px' id='modelDropdown' name='modelDropdown' onchange='modelChanged(this.value)'>
    <option value="">- Select business first -</option>
    </select>
    </p>


    <p style='font-weight: bold'>What board do you want to look at?
    <select style='min-width: 50%; max-width: 90%; background-color: #ffff99' id='boardDropdown' name='boardDropdown' onchange='boardChanged(this.value)'>
    <option value="">- Select business first -</option>
    </select>
    </p>
    <p>
    <input id='showAllBoardsCheckbox' type='checkbox' onclick='toggleShowAllBoards()'><label for="showAllBoardsCheckbox">Show all boards</label>
    </p>
    <p style='font-weight: bold'>What side do you want to see?
    <select id='sideDropdown' name='sideDropdown' onchange='sideChanged(this.value)' style='background-color: #ffff99; margin-left: 35px';>
    <option value="">- Select board first -</option>
    </select>
    </p>

    <p style='font-weight: bold'>Which revision do you want to use?
    <select id='revisionDropdown' name='revisionDropdown' onchange='revisionChanged(this.value)' style='background-color: #ffff99';>
    <option value="">- Select board first -</option>
    </select>
    </p>

    <div id='lineDropdownContainer' name='lineDropdownContainer'>
    <p style='font-weight: bold'>Which line are they running down<br>or where have they been built?
    <select id='lineDropdown' name='lineDropdown'  onchange='lineChanged(this.value)' style='background-color: #ffff99; margin-left: 32px';>
    <option value="">- Select board first -</option>
    </select>
    </p>
    </div>


    <label for="Category"><b>Category:</b></label>
        <select id="categoryDropdown" name="categoryDropdown" style='background-color: #ffff99; margin-left: 175px';>
        </select><BR>
<script>
  function validateCategory() {
    var x = document.forms["newForm"]["categoryDropdown"].value;
    if (x == "") {
      alert("A Category must be provided");
      return false;
    }
  }
</script>

    <label for="Reason"><b>Reason for Change/Action:</b></label>
        <select id="reasonDropdown" name="reasonDropdown" style='background-color: #ffff99; margin-left: 57px';>
        </select><BR>
<script>
  function validateReason() {
    var x = document.forms["newForm"]["reasonDropdown"].value;
    if (x == "") {
      alert("A Reason must be provided");
      return false;
    }
  }
</script>

    <label for="problem"><b>Problem Description:</b></label><br>
    <textarea id="problemdesc" name="problemdescript" style='background-color: #ffff99'; cols="100" rows="4" style="resize:none" placeholder="Please provide as much information as possible..."></textarea><br>

<script>
  function validateDescription() {
    var x = document.forms["newForm"]["problemdescript"].value;
    if (x == "") {
      alert("A description of the problem must be provided");
      return false;
    }
  }
</script>

    <div>
        <label><strong>Add Attachment</strong>(optional)</label><br /> <input type="file"
            name="attachmentFile" id="attachmentFile"
            class="InputBox">
    </div><br>

    <label for="Status1"><b>Status:</b></label>
        <select id="statusDropdown1" name="statusDropdown" style='background-color: #ffff99';>
        </select>
<script>
  function validateStatus() {
    var x = document.forms["newForm"]["statusDropdown"].value;
    if (x == "") {
      alert("A status must be provided");
      return false;
    }
  }
</script>

    <label for="requiredaction"><b>Action Required?</b></label>
        <select id="requiredaction" name="requireaction" style='background-color: #ffff99';>
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select>

    <label for="SubmittedBy"><b>Submitted By:</b></label>
    <input type="text" id="SubmittedBy" name="SubmittedBy"  style='background-color: #ffff99'; value = "<?php echo $_SESSION['smtRequestForAction']['user_name'];?>" readonly>

    <label for="DateSubmitted"><b>Date Submitted:</b></label>
    <input input type="date" id="DateSubmitted" name="DateSubmitted" style='background-color: #ffff99'; value="<?php echo date('Y-m-d');?>" readonly><br>

    <input type="submit" value="Submit" name="submit" onclick="validateCategory();validateReason();validateDescription();validateStatus();sendEmail()";>

1 个答案:

答案 0 :(得分:0)

好吧,所以我设法通过使用Javascript函数来解决它,我的sendEmail.php已修改为以下内容...

<?php
       $to  = 'email1@domain.com';
       $subject = 'test subject';
       $message = 'test message';
       $headers  = 'Cc: email2@domain.com';
       mail($to,$subject,$message,$headers);
?>

在index.php中,我使用了以下功能...

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function sendMail() {
  $.get("sendEmail.php");
  return false;
}
</script>

我在onclick选项上添加了sendMail()函数,现在刷新时,仅当用户填写并提交表单时,电子邮件才发送。希望这对某人有帮助。