错误的php电子邮件表单

时间:2017-02-12 20:06:32

标签: php html email

我已经在这个简单的代码上花了好几个小时。电子邮件到达,但名称电子邮件简报等字段为空。它一定是愚蠢的东西,但是当它只是简单的形式时,它让我很蠢,它不起作用。 另外,如果您知道,可以帮助我阻止用户点击表单两次并发送多封电子邮件。非常感谢!

<?php
    $name = strip_tags(htmlspecialchars($_GET['name']));
    $email_address = strip_tags(htmlspecialchars($_GET['email']));
    $message = strip_tags(htmlspecialchars($_GET['message']));
    $newsletter = strip_tags(htmlspecialchars($_GET['newsletter']));

    // Create the email and send the message
    $to = 'xxx@xxx.com'; 
    $email_subject = "Message from from :  $name";
    $email_body = "Message from form arrived.\n\n"."Message details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $newsletter\n\nMessage:\n$message";
    $headers = "From: form@xxx.com\n"; 
    $headers .= "Reply-To: $email_address"; 
    mail($to,$email_subject,$email_body,$headers);          
?>



<form method="GET">
    <span>Name</span>
    <input type="text" name="name" placeholder="Your name"><br />

    <span>E-mail</span>
    <input type="email" name="email" placeholder="E-mail"><br />

    <div>Message</div>
    <textarea type="text" name="message" rows="8" cols="80"></textarea><br />

    <input type="checkbox" name="newsletter" id="newsletter">
    <label for="newsletter">I wold like to get email infromations</label><br />

    <button type="submit">Send</button>
    <span class="message"></span>
</form>

1 个答案:

答案 0 :(得分:-3)

在处理包含变量的冗长字符串时,您可能更喜欢HEREDOC语法,就像您用来生成电子邮件内容一样。如果你的变量正确填充,那么这应该可行。

$email_body  = <<<EOT
 Message from form arrived.
 Message details:
 Name: $name
 Email: $email_address
 Phone: $newsletter
 Message:
 $message
EOT;