【问题标题】:PHP and Ajax form send email but the email not deliver [duplicate]PHP和Ajax表单发送电子邮件但电子邮件未发送[重复]
【发布时间】:2018-11-14 14:51:46
【问题描述】:

我使用 HTML 和 Ajax 从我的网站发送电子邮件,有时会发送电子邮件,但 90% 的时间不会发送。这些消息甚至不在电子邮件的垃圾邮件中。这是我的代码,我在我的网站上保存了大约 2 年,但我从不认为它不起作用,请帮忙:

html:

       <form id="contact-form" class="contact-form" action="#">
            <p class="contact-name">
                <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
            </p>
            <p class="contact-email">
                <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
            </p>
            <p class="contact-message">
                <textarea id="contact_message" placeholder="Your Message" name="message" rows="10" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" href="#">Send Your Email</a>
            </p>

            <div id="response">

            </div>
        </form>

Ajax (fichier main.js):

$("#contact-submit").on('click',function() {
    $contact_form = $('#contact-form');

    var fields = $contact_form.serialize();

    $.ajax({
        type: "POST",
        url: "http://musamr.com/wp-content/themes/musamr/include/php/contact.php",
        data: fields,
        dataType: 'json',
        success: function(response) {

            if(response.status){
                $('#contact-form input').val('');
                $('#contact-form textarea').val('');
            }

            $('#response').empty().html(response.html);
        }
    });
    return false;
});

联系人.php

<?php
/*
* Contact Form Class
*/
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'monemail@gmail.com'; // Your Email
$message_min_length = 5; // Min Message Length

class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Contact from Your Website'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }

    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }

    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid e-mail address</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }
    }

    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = '<p>Thank You!</p>';
        }
    }

    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}

$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>

【问题讨论】:

  • 是,假设您在使用它们之前已经设置了您的 php 电子邮件设置......那么您的 http 服务器日志错误文件说明了什么?
  • 您查看过您的邮件日志吗?如今,许多邮件提供商只会无缘无故地将来自私人服务器的邮件列入黑名单 - 您的邮件很有可能只是被收件人拒绝。如果有些人正在经历但其他人没有经历,这种情况尤其可能发生。软件通常不会在没有某些已知因素的情况下间歇性失败 - 在这种情况下,最有可能是收件人邮件服务器

标签: php ajax email


【解决方案1】:

邮件功能极不可靠。您应该考虑使用 SMTP 服务器和 PHPMAILER 之类的库来简化集成。

phpmailer:https://github.com/PHPMailer/PHPMailer

邮件功能会尝试发送,但更多次会进入用户的垃圾邮件文件夹,否则目的地可能会拒绝它。

您也可以使用该正则表达式来代替 filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)

【讨论】:

  • 你的意思是 phpmailer : github.com/PHPMailer/PHPMailer ??
  • 是的,我的意思是说 PHPMailer,答案已更新。
  • 非常感谢,我会调查的 :)
  • 你好,我终于使用 SMTP (PHPMailer) 让它工作了,但我不断收到来自谷歌的安全通知,我的表单中有一个错误:“发生错误。请稍后再试。”
  • 谷歌这个错误,我不记得它到底是什么,但我很确定你需要启用一些类似于禁用第三方应用程序安全性的东西。或者,您可以注册 Zoho 并免费使用他们的 SMTP。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2015-08-10
  • 2011-02-22
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多