电子邮件表单会发送空白电子邮件

时间:2015-09-16 07:48:59

标签: php forms email

我的网站上有一个电子邮件表单,当我尝试发送一封电子邮件时,我收到了它,但它是空白的,只是没有。

我是php的新手,所以我很少(没有)经验。

我一直在寻找,但无法找到解决问题的方法。

请帮忙。

这是我的代码:

<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文件:

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

2 个答案:

答案 0 :(得分:1)

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

查看邮件功能并在pramiter中设置变量并删除@。

查看内容....

设置标题的变量见上面......

答案 1 :(得分:0)

试试这个

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

您也可以使用 escape_string 功能