繁体   English   中英

没有收到表格邮件

[英]not receiving form emails

我之前尝试过没有解决方案,所以我添加了所有代码以尝试解决此问题。 我没有在收件箱中收到此表单,但是我的Ajax函数正在正常运行。 我有Godaddy托管,并且正在Mac的wampp上本地运行。 我尝试了多个不同的电子邮件地址,但无济于事。 我不想使用php mailer。 我只是希望此功能正常运行,而不是将电子邮件发送到太空

html

<div class="block">
<div class="done">
<h1>Thank you! I have received your message.</h1>
</div>
<div class="form">
<form method="post" action="scripts/process.php">
<input name="name" class="text" placeholder="Name" type="text" /> 
<input name="email" class="text" placeholder="Email" type="text" /> 
<textarea name="comment" class="text textarea" placeholder="Comment"></textarea> 
<input name="submit" value="Let's Go!" id="submit" type="submit" />
<div class="loading"></div>
</form></div>
</div>
<div class="clear"></div>
</div>
</div>

jQuery验证和Ajax

$('#submit').click(function () {        

        //Get the data from all the fields
        var name = $('input[name=name]');
        var email = $('input[name=email]');
        var comment = $('textarea[name=comment]');
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 
        var emailVal = email.val();
        //Simple validation to make sure user entered something
        //If error found, add hightlight class to the text field
        if (!emailReg.test(emailVal)){
        email.addClass('hightlight');
        return false;
        } else email.removeClass('hightlight');

        if (name.val()=='') {
            name.addClass('hightlight');

        }else{name.removeClass('hightlight');}
        if (email.val()=='') {
            email.addClass('hightlight');

        }else{email.removeClass('hightlight');}
        if (comment.val()=='') {
            comment.addClass('hightlight');
        }else{email.removeClass('hightlight');}

        if (name.val()=='' || email.val()=='' || comment.val()=='') {
            return false;
            }



        //organize the data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + '&website='
        + website.val() + '&comment='  + encodeURIComponent(comment.val());

        //disabled all the text fields
        $('.text').attr('disabled','true');

        //show the loading sign
        $('.loading').show();

        //start the ajax
        $.ajax({
            //this is the php file that processes the data and send mail
            url: "scripts/process.php",                
            //GET method is used
            type: "GET",
            //pass the data            
            data: data,                    
            //Do not cache the page
            cache: false,
            //success
            success: function (html) {                
                //if process.php returned 1/true (send mail success)
                if (html==1) {                    
                    //hide the form
                    $('.form').fadeOut('slow');                    
                    //show the success message
                    $('.done').fadeIn('slow');
                //if process.php returned 0/false (send mail failed)
                } else alert('Sorry, unexpected error. Please try again later.');                
            }        
        });

        //cancel the submit button default behaviours
        return false;
    });    

和php,我很遗憾地说我目前对此知之甚少

<?php
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, 
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 
//if the errors array is empty, send the mail
if (!$errors) {
    //recipient - change this to your name and email
    $to = 'myemail@gmail.com';    
    //sender
    $from = $name . ' <' . $email . '>';

    //subject and the html message
    $subject = 'Comment from ' . $name;    
    $message = '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
    <table>
        <tr><td>Name</td><td>' . $name . '</td></tr>
        <tr><td>Email</td><td>' . $email . '</td></tr>
        <tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';
    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! Matt has received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;    
    }
//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="form.php">Back</a>';
    exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}
?>

将PHP邮件发送到不同的邮件服务器有很多问题。 例如,当我想向GMail主机发送电子邮件时,我永远不会在新行中包含\\r ,而只会使用\\n 也许您的问题出在您的标头上,我将代码放在这里,看看您能得到什么:

function send($address, $subject, $content, $from = '', $html = FALSE, $encoding = 'utf-8')
    {
      if(empty($address) || empty($content)) return;

      $headers = "";

      if($from != '')
      {
        $headers .= "From: {$from}\n";
        $headers .= "Reply-To: {$from}\n";
      }

      $headers .= "To: {$address}\n";

      if($html)
      {
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: text/html; charset={$encoding}\n";
        $headers .= "Content-Transfer-Encoding: 8bit\n";
      }

      $headers .= "X-Priority: 1 (Highest)\n";
      $headers .= "X-MSMail-Priority: High\n";
      $headers .= "Importance: High\n";
      $headers .= "X-Mailer: PHP/" . phpversion();

      set_time_limit(30);

      mail($address, $subject, $content, $headers);
    }

send('myemail@myhost.com', 'title', 'content', 'myemail <myemail@myhost.com>', FALSE);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM