PHP联系表格未发送电子邮件 [英] PHP Contact Form Not Sending Email

查看:59
本文介绍了PHP联系表格未发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing to create a contact form on my website and then get the information sent to my inbox, however it is not working whatsoever. Please take a look at my code below (PHP is not my thing) and let me know where i've gone wrong.

这是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通过联系表格7(或类似的插件)轻松实现.

Thanks for the help. FYI this can be achieved easily with WordPress through Contact Form 7 (or a similar plugin).

推荐答案

在继续执行任何操作之前,我认为您应该看一下,尽管尽管W3Schools因其友好的用户示例而可以作为基础教程,但还是要与其他资源互补在这里看看为什么.

Before you continue anything, I think you shold take a look at this or this, be awere that despite W3Schools may serve as a basics tutorial because of the friendly-user examples, always complement with other resources look here why.

那么您可以看一下这个答案,这仅仅是因为您需要更多的基础来了解您所做的一切.

Then you can take a look at this answers, this is just because you need some more basis to understand everything you are doing.

在表单中看不到您的$ _POST ['send']名称,或者为时已晚,我很累:

I can't see your $_POST['send'] name in the form, or it's just too late and I'm tired:

不知道,但是也许,您想要在提交之前以表格的形式提交该信息:

Don't know, but maybe, you want this in your form before the submit:

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

然后:

我已经发布了此答案此处,但是在这里它是:

I already posted this answer HERE, but here it is:

首先,我认为您缺少一些标头,看看这些标头

// 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";

2nd在调用邮件功能时检查其是真还是假

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


    }
    die('Sucess');

    }

您可以看一下 isset()函数确保所有变量都是从表单中设置的.

You shold take a look at the isset() function to make sure all your variables are set from the form.

这篇关于PHP联系表格未发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆