2016-02-28 60 views
1

好吧,我在這工作了一個小時左右,而我剛剛對這個問題沒有認識。有人可以指出我需要更改以進行下面的代碼設置,以便表單中輸入的電子郵件地址將電子郵件發送到我的服務器上的管理電子郵件帳戶,我們稱之爲[email protected]。下面是html格式和php代碼。 PHP:電子郵件功能沒有正確執行

<?php 
if(isset($_POST['email'])) { 
// EDIT THE 2 LINES BELOW AS REQUIRED 
$email_to = "[email protected]"; 
$email_subject = "Your email subject line"; 

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['telephone']) || 
    !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_from = $_POST['email']; // required 
$telephone = $_POST['telephone']; // not required 
$comments = $_POST['comments']; // required 

$error_message = ""; 

$email_exp = '/^[A-Za-z0-9._%-][email protected][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 .= "First Name: ".clean_string($first_name)."\n"; 
$email_message .= "Last Name: ".clean_string($last_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\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); 
    ?> 

<!-- include your own success html here --> 
Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 

HTML:

<form name="contactform" method="post" action="send_form_email.php"> 
    <table width="450px"> 
     <tr> 
      <td valign="top"> 
       <label for="first_name">First Name *</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="first_name" maxlength="50" size="30"> 
      </td> 
     </tr> 
     <tr> 
      <td valign="top""> 
       <label for="last_name">Last Name *</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="last_name" maxlength="50" size="30"> 
      </td> 
     </tr> 
     <tr> 
      <td valign="top"> 
       <label for="email">Email Address *</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="email" maxlength="80" size="30"> 
      </td> 
     </tr> 
     <tr> 
      <td valign="top"> 
       <label for="telephone">Telephone Number</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="telephone" maxlength="30" size="30"> 
      </td> 
     </tr> 
     <tr> 
      <td valign="top"> 
       <label for="comments">Comments *</label> 
      </td> 
      <td valign="top"> 
       <textarea name="comments" maxlength="1000" cols="25" rows="6"> </textarea> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" style="text-align:center"> 
       <input type="submit" value="Submit"> <a href="http://www.freecontactform.com/email_form.php">Email Form</a> 
      </td> 
     </tr> 
    </table> 
</form> 
+0

此外,只是添加上述表格成功完成,並在我的服務器電子郵件帳戶我沒有收到電子郵件,甚至沒有報告電子郵件沒有發送,也沒有建議收件人的電子郵件,以防我倒過來。 – dhool

+0

您使用哪個電子郵件提供商發送電子郵件? –

+0

松鼠我想,但我沒有設置電子郵件提供商,我只是使用cPanel上設置的默認設置。一個測試郵件成功的收件人地址以前電子郵件服務器正在工作 – dhool

回答

1

你不需要任何的電子郵件服務提供商或第三方軟件上的工作服務器。 我測試了代碼,它在服務器上運行時沒有問題(但不應該在本地計算機上運行)。你在本地工作還是在服務器上工作?以下是來自PHP站點的註釋:

郵件()的Windows實現與Unix實現的 在許多方面有所不同。首先,它不使用構成消息的本地二進制文件 ,但僅對直接套接字進行操作,這意味着需要在網絡套接字(可以在本地主機或遠程機器上)上偵聽MTA。其次,自定義標題如From :, Cc:,Bcc:和Date:不首先被MTA解釋, 但由PHP解析。因此,to參數不應該是「Something」形式的 地址。在與MTA交談時,郵件 命令可能無法正確解析。

的更多信息:PHP: mail - Manual

+0

引用管理員電子郵件而不是用戶電子郵件,所以沒有看到電子郵件。我在本地託管在服務器上。 – dhool

0

行,所以一段時間後,我想通了,我是引用了錯誤的電子郵件。我在我的服務器上登錄了我的管理員電子郵件帳戶,而不是我創建的用戶帳戶,因此沒有看到電子郵件。愚蠢的理由,但現在工作就更不用說了。謝謝你的幫助。

相關問題