没有收到表格电子邮件

时间:2013-09-06 15:32:20

标签: php jquery ajax

我之前尝试过没有解决方案,所以我添加了所有代码以尝试解决此问题。我没有在我的收件箱中获取此表单,但我的ajax功能正常运行。我有godaddy托管,我在本地运行它在wampp for mac。我尝试了多个不同的电子邮件地址但没有用。我不想使用php邮件程序。我只是想让它正常运行,而不是看起来正在做的是将电子邮件发送到外部空间

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;
}
?>

1 个答案:

答案 0 :(得分:1)

将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);