【问题标题】:PHP - mail only sending on ChromePHP - 仅在 Chrome 上发送邮件
【发布时间】:2013-08-16 18:34:11
【问题描述】:

我遇到了一个奇怪的问题,PHP mail 函数只能从 Chrome 发送电子邮件。这是 HTML 表单:

我已经检查了 HTML 的验证,但我可以验证只有在用户使用 Chrome 时才能接收电子邮件。这是验证码:

if (isset($_POST['email_from']) && isset($_POST['contact-name']))
{
    function set_email_recipent($contact_name) {
        $result = "";
        switch ($contact_name) {
            default:
                $result = "info@mysite.com";
                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._%-]+@[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. yourname@gmail.com">
        <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 而不是其他浏览器中有效?我该如何解决这个问题?

【问题讨论】:

  • mail() 不在乎用户使用什么浏览器。它的工作是发送电子邮件,而不是因为有人使用 Chrome 而不是 IE 或 Firefox 而改变它的行为。如果它在不同的浏览器中表现不同,那么造成这种差异的是您的代码,而不是邮件本身。
  • 是什么让您认为用户必须在 Chrome 上才能运行脚本? PHP 是一种服务器端语言。
  • 添加了 HTML。我很清楚mail 是跨浏览器的,但我发现其他人也有类似的问题:stackoverflow.com/questions/11993046/…
  • 刚刚在 Browserstack 中运行了一些测试:在 IE、Windows 的 FF、Android 的 FF Mobile 中工作……但在我的本地机器或 Safari 上没有 FF……想法?
  • 你的 php 输出什么?

标签: php google-chrome sendmail


【解决方案1】:

在进行了一些严格的测试后,我们确定是客户端的邮件服务器出了问题。通过联系他们的技术支持并索取邮件服务器日志的副本,我们可以确定有大量电子邮件被视为垃圾邮件而被拒绝。

遇到类似问题的其他人应咨询其客户负责管理相应邮件服务器的技术部门,以便在假定浏览器端出现任何问题之前先确定电子邮件是否被正确接收。

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多