【问题标题】:PHP Contact Form Not Sending EmailPHP联系表不发送电子邮件
【发布时间】:2021-01-11 22:20:00
【问题描述】:

我写信是为了在我的网站上创建一个联系表格,然后将信息发送到我的收件箱,但是它无论如何都不起作用。请看看我下面的代码(PHP 不是我的事),让我知道我哪里出错了。

这是 PHP 脚本:

<?php

$to = 'example@gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$project = $_POST['project'];
$range1 = $_POST['range1'];
$range2 = $_POST['range2'];

$body = <<<EMAIL

Hi, my name is $name.

I'd like to discuss the possibility of working together on a $project.

My budget for the project is £$range1 and I would like to complete the project within $range2 Month(s).

Additional Information:
$message

Regards, $name

<hr>
$name
$email
$tel

EMAIL;

$header = "From: $email";

if($_POST['send']){
mail($to, $subject, $body, $header);
$feedback = 'Thank you for your message, we will get back to you shortly.';
}

?>

这是 HTML 表单:

<form id="form_id" name="form_name" action="" method="post">

  <div>
<input type="text" name="name" id="name" placeholder="Name" required/>
  </div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
  </div>
<div>
<input type="tel" name="tel" id="tel" placeholder="Phone" required/>
  </div>

<div>
  <select required id="project">
<option selected>Select type of project…</option>
<option value="Responsive Website">Responsive Web Design</option>
<option value="Graphic Design">Graphic Design</option>
<option value="Motion Graphics">Motion Graphics</option>
  </select> 
</div>

  <div>
<label for="range1">Budget: </label>
<input type="range" name="range1" id="range1" min="400" max="2000" step="50" value="6" required onchange="rangevalue.value=value"><output id="rangevalue">400</output>
  </div>

<div>
<label for="range2">Timeframe: </label>
<input type="range" name="range2" id="range2" min="1" max="12" step=".5" value="1" required onchange="rangevalue1.value=value"><output id="rangevalue1">1</output>
  </div>

<div>
<label for="message">Additional Information: </label><br/>
<p>(Please use this space to tell us about your company, the main objectives of the proposed website and anything else you think might be useful)</p>
<textarea name="message" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
  </div>

<div>
<input type="submit" name="submit" value="submit" />
  </div>

</form>
<p id="feedback"><?php echo $feedback; ?></p>

感谢您的帮助。仅供参考,这可以通过 WordPress 通过 Contact Form 7(或类似插件)轻松实现。

【问题讨论】:

  • 您的表单中的 $_POST['send'] 在哪里?
  • 看我的回答,去这里w3schools.com/php/default.asp,然后你应该回来,你应该理解的你缺少的基本PHP基础,这是为了你自己好。
  • @Steve,这是为什么呢? W3Schools 对 PHP 有很好的介绍,我敢肯定,如果做这个问题的用户已经阅读了该页面,那么这个问题根本不存在。但与其做那些 cmets,不如试着帮助他有更好的学习节奏!
  • 为什么是什么? W3Schools 因其错误而臭名昭著,这也是 Web 开发人员和 PHP 被视为编程世界的狗的主要原因之一。糟糕的代码架构、糟糕的标准以及对任何一个都不在乎的程序员。 StackOverflow 的创始人似乎同意:codinghorror.com/blog/2008/05/…codinghorror.com/blog/2012/06/the-php-singularity.html。如需更好的学习场所,请访问此网站:stackoverflow.com/questions/1610543/…
  • @Yvette,在我看来,您可以从那里毫无问题地学习基础知识,但您可能还想看看 Steve 链接以及其他资源,它只会有帮助你;)

标签: php html-email


【解决方案1】:
<input type="submit" name="submit" value="submit" />

您必须将此行更改为:

<input type="submit" name="send" value="submit" />



  if($_POST['send']){ 

实际检查提交按钮是否被点击...

而且,是的 - 如果 html 和 php 在不同的页面上,您必须设置正确的表单操作链接...

【讨论】:

    【解决方案2】:

    我认为您应该看看 thisthis,请注意,尽管 W3Schools 可能因为友好的用户示例而作为基础教程,但始终与其他资源补充 look here why

    那你可以看看这个答案,这只是因为你需要更多的基础来理解你所做的一切。

    我在表单中看不到你的 $_POST['send'] 名字,或者太晚了,我累了:

    不知道,但也许,您希望在提交之前将其包含在您的表单中:

    <input type="hidden" name="send" >
    

    然后:

    我已经发布了这个答案HERE,但这里是:

    首先我认为你缺少一些标题看看那些

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Additional headers
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
    

    调用邮件函数时第二次检查是真还是假

    if( mail($email_to, $email_subject, $email_message, $headers)!==true)
        {
            die('Fail to send');
    
    
        }
        die('Success');
    
        }
    

    您应该查看isset() 函数,以确保您的所有变量都是从表单中设置的。

    【讨论】:

    • 好的,感谢您的帮助,表单可以正常工作,但我有一个
    • 在select
    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多