简体   繁体   中英

Email form sends blank emails

I have an email form in my website, when I try to send an email I receive it but it is blank, just nothing in there.

I am new to php so I have little (none) experience on it.

I have been looking but cannot find a solution to my problem.

Please help.

This is my code:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
    <div class="col-sm-5 col-sm-offset-1">
        <div class="form-group">
            <label>Name *</label>
            <input type="text" name="name" class="form-control" required>
        </div>
        <div class="form-group">
            <label>Email *</label>
            <input type="email" name="email" class="form-control" required>
        </div>
        <div class="form-group">
            <label>Phone</label>
            <input type="number" class="form-control">
        </div>
        <div class="form-group">
            <label>Company Name</label>
            <input type="text" class="form-control">
        </div>                        
    </div>
    <div class="col-sm-5">
        <div class="form-group">
            <label>Subject *</label>
            <input type="text" name="subject" class="form-control" required>
        </div>
        <div class="form-group">
            <label>Message *</label>
            <textarea name="message" id="message" required class="form-control" rows="8"></textarea>
        </div>                        
        <div class="form-group">
            <button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
        </div>
    </div>
</form> 

PHP File:

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Thank you for contact us. As early as possible  we will contact you '
);

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 's.design.mg@gmail.com';//replace with your email

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;
?>
<?php
$to = "somebody@example.com";
$subject = "HTML email";

$message = "";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

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

view a mail function and set the variable in pramiter and remove @.

view a content....

set the variable of headers see it above...

Try this

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. As early as possible  we will contact you '
    );

    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $subject = stripslashes($_POST['subject']);
    $message = stripslashes($_POST['message']);

    $email_from = $email;
    $email_to = 's.design.mg@gmail.com';//replace with your email

    $body = 'Name: ' . $name . "\n\n" .
        'Email: ' . $email . "\n\n" .
        'Subject: ' . $subject . "\n\n" .
        'Message: ' . $message;

    $header = "MIME-Version: 1.0" . "\r\n";
    $header .= 'From:'.$email_from . "\r\n";

    $success = @mail($email_to, $subject, $body,$header );

    echo json_encode($status);
    die;
?>

and you can use escape_string function too

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