简体   繁体   English

联系表格发送空白电子邮件

[英]Contact form sends blank emails

My contact form is not including message and other informations in emails. 我的联系表未在电子邮件中包含消息和其他信息。

There is 'Unknown sender' and 'No subject'. 有“未知发件人”和“无主题”。 Email contains my tags (from:, subject:, message:) but no content is written after them. 电子邮件包含我的标签(发件人:,主题:,消息:),但标签后没有任何内容。

I have tried a few other scripts, but they gave the same result. 我尝试了其他一些脚本,但是它们给出了相同的结果。

My JS code 我的JS代码

(function($){
$(document).ready(function() {
    $('#submit-form').click(function(e){

        e.preventDefault();
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var name  = $('#rp_name').val(),
            email  = $('#rp_email').val(),
            subject  = $('#rp_subject').val(),
            message  = $('#rp_message').val(),
            data_html,
            success = $('#success');

        if(name === ""){
            $('#rp_name').val('Please enter your name.');
        }

        if(subject === ""){
            $('#rp_subject').val('Please enter your name.');
        }

        if(email === ""){
            $('#rp_email').val('Your email is required.');
        }else if(reg.test(email) === false){
            $('#rp_email').val('Invalid Email Address.');
        }

        if(message === ""){
            $('#rp_message').val('Message is required.');
        }

        if(message !== "" && name !== "" && reg.test(email) !== false) {
            data_html = "name=" + name + "&email="+ email + "&message=" + message + "&subject="+ subject;

            alert(data_html);
            $.ajax({
                type: 'POST',
                url: 'php_helpers/contact_form.php',
                data: data_html,
                success: function(msg){

                    if (msg == 'sent'){
                        success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ;
                        $('#rp_name').val('');
                        $('#rp_email').val('');
                        $('#rp_message').val('');
                    }else{
                        success.html('<div class="alert alert-error">Message <strong>not</strong> sent! Please Try Again!</div>')  ; 
                    }
                }
            });

        }
        return false;
    });
});
})(jQuery);

My PHP code 我的PHP代码

<?php

$to = 'email'; // I changed it 

$subject = $_POST['subject'];

if($to) {
$name = $_POST['name'];
$email = $_POST['email'];

$fields = array(
    0 => array(
        'text' => 'Imie',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'WIADOMOSC',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

$$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '. $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();


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

if ($message){
echo 'sent';
}else{
echo 'failed';
}
} else {
echo "Don't access this file directly"; 
}
?>

I'll be glad for any hints. 任何提示我都会很高兴。

There as i check your contact.php file code is ok. 在那里,我检查您的contact.php文件代码是否正常。 But you need one thing if use firefox. 但是如果使用Firefox,则需要一件事。 please console your code.I mean when you open firebug there will be one option console. 请控制台代码。我的意思是当您打开Firebug时,将有一个选项控制台。 with help you can check is your value along with variable is going or they empty. 在帮助下,您可以检查您的值是否与变量一起使用或它们为空。

I think your value is not going to PHP file. 我认为您的价值不在于PHP文件。 use console and you can easily bug your value. 使用控制台,您可以轻松地破坏您的价值。

I hope this make sense for you. 我希望这对您有意义。

Check if your php script receives name, email and message. 检查您的php脚本是否收到名称,电子邮件和消息。 You can check it using 您可以使用检查

var_dump($_POST);

If server doesn't receive these variables, try to send data_html as an object: change 如果服务器未收到这些变量,请尝试将data_html作为对象发送:更改

data: data_html,

to

data: {name: name, email: email, message: message}

I didn't test it, but I always use this way of sending data from js to server. 我没有测试它,但是我总是使用这种方式将数据从js发送到服务器。

You are using two dollar symbols in the line: $$headers = 'MIME-Version: 1.0' . 您在该行中使用两个美元符号:$$ headers ='MIME-Version:1.0'。 "\\r\\n"; 为 “\\ r \\ n” 个;
Please remove "$" and try again. 请删除“ $”,然后重试。

Thanks for all your responses. 感谢您的所有回复。 I finally manage to find a solution. 我终于设法找到解决方案。 I haven't used firebug before, but thanks to it I could find the error message. 我以前没有使用过Firebug,但是由于有了它,我才能找到错误消息。 Now the script is working fine. 现在,脚本运行正常。

It wasn't in code, but in .htaccess rewrite rule for .php files which was couseing 301 error for ajax request (../contact instead of ../contact.php). 它不是在代码中,而是在.htaccess中对.php文件的重写规则,该规则导致ajax请求(../contact而不是../contact.php)出现301错误。

I have one .php page (file uploader) and I thought that adding another seo-friendly link rule for it would be a good idea. 我有一个.php页面(文件上传器),我认为为此添加另一个seo友好的链接规则将是一个好主意。

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

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