dongzeao5047 2017-03-10 22:31
浏览 34

PHP电子邮件表单不发送电子邮件[重复]

This question already has an answer here:

I have only dabbled in PHP a little, I am trying to remember what I did last year. I am having trouble getting this to work - the user is redirected to index.php but then nothing happens. No email is recieved and no verification 'email sent/not sent' etc.

I am sure it is probably a silly mistake.

Any help would be appreciated.

contact.html

<form method="post" action="index.php">


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

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

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

<label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="Submit">

</form>

index.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; // GET EMAIL ADDRESS FROM FORM
$to = 'annie.palmer@outlook.com'; 
$subject = 'Website enquiry from' .$name;
$body = "From: $name
 E-Mail: $email
 Message:
 $message";

$headers = "From: Annie<$from>
Return-path: $from" . "
";
$headers .= "Content-type:text/text;charset=ISO-8859-1"; 

?>
<?php
if ($_POST['submit'] && ($_POST['human'] == '4') {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $body, $headers, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>
</div>
  • 写回答

2条回答 默认 最新

  • douchuxun4162 2017-03-10 22:53
    关注

    I'm posting this as a community wiki; I don't feel any rep should come of this, nor do I want rep.

    mail() uses 4 arguments, not 5.

    There is a 5th but it doesn't do what you're expecting your 5th to do.

    So remove the , $from from this:

    if (mail ($to, $subject, $body, $headers, $from))
                                            ^^^^^^^
    

    The From: belongs in the header and expects an email address.

    Example from the manual:

    <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "
    " .
        'Reply-To: webmaster@example.com' . "
    " .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等