简体   繁体   中英

PHP contact form sends empty emails

I can't understand why my PHP contact form sends empty emails. Please, help me to figure out where the problem is.

HTML form code:

<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
    <div class="form-group">
        <input type="text" name="name" class="form-control" required="required" placeholder="Имя">
    </div>
    <div class="form-group">
        <input type="phone" name="phone" class="form-control" required="required" placeholder="Номер телефона">
    </div>
    <div class="form-group">
        <input type="email" name="email" class="form-control" required="required" placeholder="Email">
    </div>
    <div class="form-group">
        <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Ваши пожелания"></textarea>
    </div>                        
    <div class="form-group">
    <input type="submit" name="submit" class="btn btn-submit" value="Отправить">
    </div>
</form>

PHP file:

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email']));
$phone      = @trim(stripslashes($_POST['phone']));
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'hello@helloschool.ru';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=iso-8859-1";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();

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

die;

After I removed '@' My Error Log says:

[Sun Aug 27 21:41:14 2017] [warn] [client 89.169.84.160] mod_fcgid: stderr: PHP Notice: Undefined index: name in /var/www/u0337516/data/www/helloschool.ru/sendemail.php on line 2, referer: http://helloschool.ru/

JS code (main.js):

var form = $('#main-contact-form');
    form.submit(function(event){
        event.preventDefault();
        var form_status = $('<div class="form_status"></div>');
        $.ajax({
            url: $(this).attr('action'),
            beforeSend: function(){
                form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Отправка заявки...</p>').fadeIn() );
            }
        }).done(function(data){
            form_status.html('<p class="text-success">Спасибо, что оставили заявку! Мы свяжемся с вами как можно скорее.</p>').delay(3000).fadeOut();
        });
    });

Thanks in advance and sorry for a newbie question.

1) You have an error in php code. Parameter headers must be a string.

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=iso-8859-1";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();

must be

$headers  = '';
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1"."\r\n";
$headers .= "From: {$name} <{$from}>"."\r\n";
$headers .= "Reply-To: <{$from}>"."\r\n";
$headers .= "Subject: {$subject}"."\r\n";
$headers .= "X-Mailer: PHP/".phpversion();

2) Your message is in Russian, but codepage iso-8859-1 supports only Latin alphabet and you need Cyrillic. Try use codepage of your HTML (I think it is Windows-1251 or UTF-8).

3) you have an error in your javascript .

$.ajax({
         url: form.attr('action')});

must be

$.ajax({
         url: form.attr('action'),
         data: form.serialize(),
         method: "POST"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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