【问题标题】:Why is my php contact form sending blank emails? [duplicate]为什么我的 php 联系表发送空白电子邮件? [复制]
【发布时间】:2015-11-05 14:29:06
【问题描述】:

我是 PHP 新手,想知道是否有人有技能可以帮助我解决这个问题?一旦用户(在这种情况下是我)提交信息,它就会向我的收件箱发送空白电子邮件。此外,没有主题显示。我相信我的问题是不同的,因为它是带有添加无线电功能的特定代码,这有助于我更好地理解它并从我的错误中吸取教训。提前致谢。

我的 PHP 代码:

<?php

if(!isset($_POST['submit'])) {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}

$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$email_from = 'testtike@yahoo.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body =   "You have received a new message from the user $name.\n" 
                . "Here is the message:\n $message".

$to = "testtike@yahoo.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to, $subject, $body, $from, $tel);

//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str) {

    $injections = array('
        (\n+)',
        '(\r+)',
        '(\t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );

    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    } else {
        return false;
    }
}

我的 HTML:

<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Phone Number</label>
<input name="tel" type="phone" placeholder="Type Here">

<label>Message/Comments/Questions</label>
<textarea name="message" placeholder="Type Here"></textarea>



<label>Are you retired?</label> <br>

<div id="button">
<input type="radio" name="retired" value="yes"> Yes.
<input type="radio" name="retired" value="no"> No. 
<input type="radio" name="retired" value="na"> Does not apply.<br><br>
</div>


<label>Are you a veteran?</label> <br>
<div id="button">
<input type="radio" name="vet" value="yes"> Yes.
<input type="radio" name="vet" value="no"> No. 
<input type="radio" name="vet" value="na"> Does not apply.<br><br>

</div>


<label>Military Family Member?</label> <br>
<div id="button">
<input type="radio" name="family" value="yes"> Yes.
<input type="radio" name="family" value="no"> No. 
<input type="radio" name="family" value="na"> Does not apply.<br>
</div>

<label> Do You Have A Copy of Your DD214?</label> <br>
<div id="button">
<input type="radio" name="dd" value="yes"> Yes.
<input type="radio" name="dd" value="no"> No. 
<input type="radio" name="dd" value="na"> Does not apply.<br>
</div>

<input type="submit" name='submit' value="submit">


</form>       

【问题讨论】:

  • mail 函数的值不正确。
  • 简单:您的邮件参数不正确。请在发布 php.net/manual/en/function.mail.php 之前通读手册 - 请记住,From: 是这里的重要部分,您的代码没有正确使用它。
  • 谢谢你们。请原谅我重复的问题。我觉得我好笨。我从字面上开始上周学习 php。我不知道自己在做什么,但是在你们的帮助下,我正在慢慢地理解它,并且会更多地依赖手册并更好地检查我的代码,正如你们所分享的那样。再次感谢您。

标签: php forms contact


【解决方案1】:

将您的 mail() 呼叫更改为:

mail($to, $email_subject, $email_body, $headers);

您的变量引用不正确。

此外,您将第五个参数传递给mail()。虽然这是有效的,但我相信你可能在执行时弄错了。第五个参数用于将其他标志传递给 sendmail 进程。

请参阅PHP Docs 了解更多信息。

【讨论】:

  • 知道了,非常感谢。对我来说这是一个愚蠢的错误,变量引用不正确。但直到现在我才知道有限的参数。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-12-29
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多