2014-02-14 70 views
-1

我會先說我對PHP一無所知。我更.NET。從PHP發送電子郵件不起作用

我把這個電子郵件模板從互聯網上拉下來了 - 而且說明只是簡單地將「To」電子郵件地址和「Subject」行更改爲任何我想要的內容。在我的PHP文件的最後,我有「感謝您與我們聯繫,我們很快就會與您聯繫。」 - 當我點擊提交時,此消息會顯示在我的瀏覽器中。它想了一會兒,然後顯示消息,向我表明發送的電子郵件。但我沒有收到任何電子郵件。任何想法爲什麼?

這裏是我的HTML表單代碼:

<form name="contactform" method="post" action="MailHandler.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"> 
       <br/> 
       <input type="submit" value="Submit"> 
      </td> 
     </tr> 
    </table> 
</form> 

這裏的MailHandler.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 

    } 
?> 
+0

有時主機不允許發送eMails。你應該聯繫支持:-) – Haudegen

+0

我懷疑這是因爲你的主機沒有安裝X-Mailer。 – ElendilTheTall

回答

1

代碼看起來合法的。詢問您的主機公司是否支持PHP驅動的電子郵件...

+0

是的,我在Windows計劃中,我的主機Arvixe禁用了Windows託管帳戶上的mail()函數。 –

+0

使用smtp驅動的郵件代碼。您可以使用https://code.google.com/a/apache-extras.org/p/phpmailer/並執行以下示例:http://stackoverflow.com/questions/14456673/sending-email-with-php -from-smtp-server,如果你想讓它更容易,試試這個:http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – Ave

1

我在PHP中使用過自己的電子郵件。這些變量會自動從我的HTML文件傳遞到您要調用mailHandler.php文件的內容。他是我如何使用郵件系統的樣本。我希望這是有幫助的。

<?php 
$to = $_POST['emailaddress']; 
$subject = 'subject here'; 
$message = '<html><head><style type="text/css">Insert any css here</style></head><body>'; 
$message .= 'Any HTML like you were designing a standard screen display'; 
$from = 'emailaddress'; 
$headers = 'MIME-Version: 1.0' . "\r\n'; 
$headers .= "Content-type: text/html; charset=iso=8859-1' . "\r\n"; 
$headers .= "From: $email_from"; 
mail ($to,$subject,$message,$headers); 

echo "<input type='button' value=\"Click to Close \" onclick=\"window.open('urlHere.php','_self');\" />"; 
?> 

這是一個簡單的版本,但它允許您創建/發送模擬電子郵件本身內的網站的電子郵件。只需在額外的$消息中逐行添加任何HTML代碼。='';部分。

我希望這是有幫助的。這是我如何做電子郵件。