如何从SMTP电子邮件发送我的PHP表单数据

时间:2013-01-08 17:15:20

标签: php html forms smtp

我的网站上有表格,因此客户可以预订锅炉服务。直到最近它还在工作,但我的网络主机已经改变了垃圾邮件过滤器。脚本是从服务器发送表单数据,但我现在需要从smtp服务器发送它。几天来我一直在搞乱任何帮助。

这是我的表格代码。

 <form name="freecontactform" method="post" action="freecontactformprocess.php" onsubmit = "return validate.check(this)">
      <table width="400px" class="freecontactform">
    <tr>
     <td colspan="2">

     <div class="freecontactformheader">Boiler Service Form</div>

     <div class="freecontactformmessage">Fields marked with <span class="required_star"> * </span> are mandatory.</div>

     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="Full_Name" class="required">Full Name<span class="required_star"> * </span></label>
     </td>
     <td valign="top">
      <input type="text" name="Full_Name" id="Full_Name" maxlength="80" style="width:230px">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="Address" class="required">Address<span class="required_star"> * </span></label>
     </td>
     <td valign="top">
      <input type="text" name="Address" id="Address" maxlength="100" style="width:230px">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="postcode" class="not-required">Post Code</label>
     </td>
     <td valign="top">
      <input type="text" name="postcode" id="postcode" maxlength="100" style="width:230px">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="email" class="not-required">Email Address</label>
     </td>
     <td valign="top">
      <input type="text" name="email" id="email" maxlength="100" style="width:230px">
     </td>
     <tr>
     <td valign="top">
      <label for="Telephone_Number" class="not-required">Telephone Number</label>
     </td>
     <td valign="top">
      <input type="text" name="Telephone_Number" id="Telephone_Number" maxlength="100" style="width:230px">
     </td>
     <tr>
     <td valign="top">
      <label for="Payment" class="not-required">Payment</label>
     </td>
     <td valign="top"><p>
       <input type="radio" name="payment" value="redirect.html"/> 
         Pay Online Via PayPal<br />
         <input type="radio" name="payment" value="thankyou.html"/> 
         Card/Cash/Cheque on Day<br />
       </p></td>
    </tr>
    <tr>
     <td colspan="2" style="text-align:center" >
      <div class="antispammessage">
      To help prevent automated spam, please answer this question
      <br /><br />
          <div class="antispamquestion">
           <span class="required_star"> * </span>
           Using only numbers, what is 10 plus 15? &nbsp; 
           <input type="text" name="AntiSpam" id="AntiSpam" maxlength="100" style="width:30px">
          </div>
      </div>
     </td>
    </tr>
    <tr>
     <td colspan="2" style="text-align:center" >
     <br /><br />
      <input type="submit" value=" Submit Form "  style="width:200px;height:40px">
      <br /><br />
      <br /><br />
     </td>
    </tr>
    </table>
    </form>

这是我的PHP

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

include 'freecontactformsettings.php';

function died($error) {
    echo "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();
}
    if(!isset($_POST['Full_Name']) ||
    !isset($_POST['Address']) ||
    !isset($_POST['postcode']) ||
    !isset($_POST['email']) ||
    !isset($_POST['Telephone_Number']) ||
    !isset($_POST['payment']) || 
    !isset($_POST['AntiSpam'])      
    ) {
    died('Sorry, there appears to be a problem with your form submission.');        
}
$full_name = $_POST['Full_Name']; // required
$address = $_POST['Address']; // required
$postcode = $_POST['postcode']; // not required
$email = $_POST['email']; // not required
$telephone = $_POST['Telephone_Number']; // not required
$payment = $_POST['payment']; // required
$antispam = $_POST['AntiSpam']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(preg_match($email_exp,$email)==0) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  if(strlen($full_name) < 2) {
$error_message .= 'Your Name does not appear to be valid.<br />';
  }
    if($antispam <> $antispam_answer) {
$error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
  }
   if(strlen($error_message) > 0) {
died($error_message);
  }
    $email_message = "Form details below.\r\n";

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

$email_message .= "".clean_string($full_name)."\r\n";
$email_message .= "".clean_string($address)."\r\n";
$email_message .= "".clean_string($postcode)."\r\n";
$email_message .= "".clean_string($email_from)."\r\n";
$email_message .= "".clean_string($telephone)."\r\n";
$email_message .= "".clean_string($payment)."\r\n";

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

$payment = $_POST['payment'];
if (!empty($payment))
  header( 'Location: '.$payment ) ;
}
die();
?>

我需要通过我的虚拟主机电子邮件发送它,它是一个SMTP服务器。

提前谢谢

2 个答案:

答案 0 :(得分:2)

您可能想查看这篇方便的文章:

http://www.9lessons.info/2009/10/send-mail-using-smtp-and-php.html

答案 1 :(得分:0)

要通过SMTP服务器发送,您需要使用PHPMailer等邮件脚本。有关入门教程,请参阅here