2013-08-16 31 views
0

我有一個奇怪的問題,即PHP mail功能將只從Chrome中發送電子郵件。下面是HTML表單:PHP - 只有郵件發送在Chrome

我已經檢查驗證的HTML,但我可以驗證只能在用戶使用Chrome時,要接收電子郵件。下面是驗證代碼:

if (isset($_POST['email_from']) && isset($_POST['contact-name'])) 
{ 
    function set_email_recipent($contact_name) { 
     $result = ""; 
     switch ($contact_name) { 
      default: 
       $result = "[email protected]"; 
       break; 
     } 
     return $result; 
    } 

    $email_to = set_email_recipent($_POST['contact-name']); 
    $email_subject = "Contact Inquiry"; 


    function died($error) 
    { 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below."; 
     echo $error; 
     echo "Please go back and fix these errors."; 
     die(); 
    } 

    // validation-expected data exists 
    if (!isset($_POST['name']) || !isset($_POST['email_from']) || !isset($_POST['phone']) || !isset($_POST['comments'])) 
    { 
     died('We are sorry, but there appears to be a problem with the form you submitted.'); 
    } 

    $name = $_POST['name']; // required 
    $email_from = $_POST['email_from']; // required 
    if (isset($_POST['email_subject'])) { 
     $email_subject = "mysite.com Contact Inquiry" . $_POST['email_subject']; 
    } 
    $telephone = $_POST['phone']; // not required 
    $comments = $_POST['comments']; // required 
    $company_name = $_POST['company-name']; //not 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.'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if (!preg_match($string_exp, $name)) 
    { 
     $error_message .= 'The First Name you entered does not appear to be valid.'; 
    } 
    if (strlen($comments) < 2) 
    { 
     $error_message .= 'Please type in a message longer than two characters.'; 
    } 
    if (strlen($error_message) > 0) 
    { 
     died($error_message); 
    } 
    $email_message = "There has been an inquiry from a customer at mysite.com. The user's form details are included 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 .= "Subject: " . clean_string($email_subject) . "\n"; 
    $email_message .= "Company name: " . clean_string($company_name) . "\n"; 
    $email_message .= "Email: " . clean_string($email_from) . "\n"; 
    $email_message .= "Telephone: " . clean_string($telephone) . "\n"; 
    $email_message .= "Comments: " . clean_string($comments); 

// create email headers 
    $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 
    if (mail($email_to, $email_subject, $email_message, $headers)) { 
     ?> 
     Your inquiry has been successfully sent. We will be in touch with you shortly. Recipent was <?php echo $email_to ?>, message was <?php echo $email_message; ?>, headers were <?php echo $headers; 
     } 
    else { 
     ?> Error! Please try again... Recipent was <?php echo $email_to ?>, message was <?php echo $email_message; ?>, headers were <?php echo $headers; 
    } 
} 

else { 
    echo "<h1>Error</h1><p>There was an error.</p>"; 
} 

這裏是我的HTML:

<form id = "contact" action = "validation.php" method = "post"> 
    <fieldset> 
     <legend>Contact Info</legend> 
     <label for = "contact-name">Contact:<em>*</em></label> 
     <select name = "contact-name" required = "required" title = "Contact" id = "contact-name" autofocus = "autofocus"> 
      <option value = "" selected = "">Choose one:</option> 
      <optgroup label = "Sales"> 
       <option value = "justin">Justin - Account Rep</option> 
       <option value = "sherri">Sherri - Account Rep</option> 
       <option value = "sean">Sean - Account Rep</option> 
      </optgroup> 
      <optgroup label = "Accounting"> 
       <option value = "shauna">Shauna - Accounts Payable</option> 
       <option value = "kristi">Kristi - Accounts Receivable</option> 
      </optgroup> 
      <optgroup label = "Administration"> 
       <option value = "matt">Matt - Operations Manager</option> 
      </optgroup> 
      <optgroup label = "Art Department"> 
       <option value = "cassidy">Cassidy - Graphic Designer</option> 
      </optgroup> 
     </select> 

     <label for = "name">Name:<em>*</em></label> 
     <input type = "text" name = "name" id = "name" title = "Name" required = "required" placeholder = "Your Name"> 

     <label for = "company-name">Company:</label> 
     <input type = "text" name = "company-name" id = "company-name" title = "Company" placeholder = "Your company name - ex. My Company Inc."> 

     <label for = "email_from">Email:<em>*</em></label> 
     <input type = "email" name = "email_from" id = "email_from" title = "Email" required = "required" placeholder = "Your Email - ex. [email protected]"> 
     <label for = "phone">Phone:<em>*</em></label> 
     <input type = "tel" name = "phone" id = "phone" title = "Phone" required = "required" placeholder = "(403) 555-5555"> 


     <label for = "email_subject">Subject:<em>*</em></label> 
     <input type = "text" name = "email_subject" id = "email_subject" title = "Subject" required = "required" placeholder = "Subject - ex. 555 Main Street, Graffiti, etc."> 


     <label for = "comments">Comments:<em>*</em></label> 
     <textarea name = "comments" cols = "10" rows = "15" id = "comments" title = "Comments" required = "required" placeholder = "Got anything you want to tell us? Put it here."></textarea> 

    </fieldset> 
    <div class = "row form-actions"> 
     <button class = "btn large" type = "submit" id = "validate">Send <i class = "icon-mail"></i></button> 
    </div> 

</form> 

爲什麼說這個作品在Chrome而不是其他的瀏覽器?我怎樣才能解決這個問題?

+0

郵件()不關心用戶正在使用的瀏覽器。它的工作是發送電子郵件,而不是改變它的行爲,因爲有人使用Chrome而不是IE或Firefox。如果它在不同瀏覽器中的行爲不同,那麼它就是你的代碼造成了這種差異,而不是郵件本身。 –

+0

什麼原因導致您認爲用戶必須在Chrome上才能使腳本正常工作? PHP是一種服務器端語言。 – djheru

+0

添加了HTML。我很清楚'mail''是跨瀏覽器的,但是我發現其他人有類似的問題:http://stackoverflow.com/questions/11993046/why-does-this-php-script-work-fine- in-firefox-but-not-safari-or-opera – user1429980

回答

0

做一些嚴格的測試後,我們認爲這是在演戲了客戶端的郵件服務器。聯繫他們的技術支持並申請郵件服務器日誌的副本,我們可以確定有大量電子郵件被拒絕爲垃圾郵件。

其他遇到類似問題,應諮詢自己的客戶的責任,以管理相應的郵件服務器技術部門首先確定是否該電子郵件被正確之前,假設在瀏覽器端的任何問題歡迎。