使用PHP邮件时不会发送变量

时间:2015-09-06 12:08:38

标签: php email

我使用以下代码作为php文件,而我将信息发布为HTML文件,用户在填写数据后单击按钮。目前,在测试变量 ($ name,$ email,$ subject,$ message) 时,除了其他字符串外,不会在电子邮件内容中发送。

的index.html

 <form id="main-contact-form" class="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="Name"/>
        </div>
        <div class="form-group">
            <input type="email" name="email" class="form-control" required="required" placeholder="Email ID">
        </div>
        <div class="form-group">
            <textarea name="subject" id="message" required="required" class="form-control" rows="4" placeholder="Enter your message"></textarea>
        </div> 
         <div class="form-group">
            <textarea name="message" id="message" required="required" class="form-control" rows="4" placeholder="Enter your message"></textarea>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary pull-right">Send</button>
        </div>
    </form>

sendmail.php

$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 = 'dhananjayan322@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.'>');

输出 enter image description here

1 个答案:

答案 0 :(得分:0)

您必须在主题之前添加一个单引号:

$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 = 'dhananjayan322@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;