簡體   English   中英

無法使用PhP發送電子郵件

[英]Can't send Email using PhP

我正在嘗試從使用php的表單發送電子郵件,這是我的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 = 'sgg3590@rit.edu'; // 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(){

        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>';
        }
        else
        {
        $this->response_status = 0;
            $this->response_html = '<p>Sending failed</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();

?>

這就是我的稱呼

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

        var fields = $contact_form.serialize();

        $.ajax({
            type: "POST",
            url: "_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;
    });
}

這是我的表格

<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="5" 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>

驗證工作正常,因此將其轉到php文件,但在按“發送”按鈕后,我無法發送電子郵件,並且響應div也未填充(無論是謝謝還是發送失敗),我都從網站上遵循了此代碼,我真的不明白這里出了什么問題.. :(

首先,請確保將計算機主機設置為與域名相同,因為有時郵件服務器會拒絕不具有匹配域的郵件頭,例如Sender: test@test.orgFrom: localhost

然后,安裝postfix,以便它可以糾正電子郵件中任何不正確/不正確的內容,最后,您在那里缺少電子郵件標題。 這是適合我的示例:

<?php
try {
    $to      = 'user@test.org';
    $subject = 'Mail Test';
    $message = <<<TEXT
Mail request received
TEXT;
    $message = str_replace("\n.", "\n..", $message);
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Test Org. <webmaster@test.org>' . "\r\n" .
        'Reply-To: webmaster@test.org' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
    printf("Mail sent to " + $to);
} catch (Exception $e) {
    printf("Error occured: " + $e);
}
?>

如果仍然失敗,則可以嘗試從控制台echo "this is the body" | mail -s "test" "receipent@test.org"發送測試消息echo "this is the body" | mail -s "test" "receipent@test.org" echo "this is the body" | mail -s "test" "receipent@test.org" ,看看它是否有效。 如果兩者均失敗,則最好詢問服務器供應商,因為他們可能已將傳出郵件設置為禁用狀態。

好吧,我在WAMP上使用了本地主機,並且意識到wamp服務器不提供該功能。 該代碼是好的並且可以正常工作。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM