PHP联系表单提交但未收到电子邮件 [英] PHP contact form submitting but not receiving email

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

问题描述

我知道这个问题之前已经被问过无数次了,但是每个人的代码显然都不同,而且我对于php还是很陌生,所以只是看看是否有人可以给我一些帮助。

I realise this question has been asked numerous times before but everyone's code is obviously different and I am quite new to php so just looking to see if someone can give me some help.

我已经为网站创建了一个基本的联系表单,但是由于某些原因,尽管我认为该表单已提交,但信息并未发送到我的电子邮件地址?

I have created a basic contact form for a site but for some reason the information is not being sent to my email address although I believe that the form is submitted?

我的PHP代码是:

<?php
session_start();
//$to_mail = "architects@palavin.com,t.lavin@palavin.com,12yorkcourt@gmail.com";
$to_mail = "danny@enhance.ie";
//$cc="paul@enhance.ie";
$mail_sent = 0;
if(isset($_POST['submit'])){
    //echo "the form was submitted";

$error= array();

$name = trim(strip_tags($_POST['name']));
if($name == "")
    $error['name'] = 1;

$email = trim(strip_tags($_POST['email']));
if($email == "")
    $error['email'] = 1;

$phone = trim(strip_tags($_POST['phone']));

$address = trim(strip_tags($_POST['address']));

$description = trim(strip_tags($_POST['description']));

$str = trim(strip_tags($_POST['secu']));
if ( isset($_SESSION['code_']) && $_SESSION['code_'] == strtoupper($str)){} else {$error['secu'] = 1;}


if(empty($error)){
    $headers = 'From: "Euro Insulation" <no-reply@euroinsulations.ie>'."\r\n";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";



    $subject = "New contact message";

    $message = "New Contact message, received from: <br /> \n ";
    $message .= "<b>Name</b> ".$name."<br /> \n";
    $message .= "<b>Email</b> ".$email."<br /> \n";

    $message .= "<b>Phone</b> ".$phone."<br /> \n";
    $message .= "<b>Address</b> ".$address."<br /> \n";

    $message .= "<b>Description</b> ".$description."<br /> \n";

    if(@mail($to_mail,$subject,$message,$headers ))
    {
        echo "mail sent";
        $mail_sent = 1;
    }
    else echo "mail not sent";
}

}

?>

我的html表单如下:

my html form looks like this:

<table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td width="65%" valign="top"><p class="header"><br>
        Contact US <br>
                  </p>
                      <?php if($mail_sent==1){
    print "Thank you for your message.";
} else { ?>
<form class="email_sub" method="post" >

<table width="77%"  border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><label for="name" class="formtext" <?php if($error['name']==1) echo "style='color:red;'" ?> >Name:</label></td>
<td><input type="text" name="name" id="text"  <?php if($name) echo "value='".$name."'" ?>  /></td>
</tr>
<tr>
<td><label for="phone" class="formtext">Number:</label></td>
<td><input type="text" name="phone" id="phone"/><tr>
<br />

<tr>
<td><label for="email" class="textarea" <?php if($error['email']==1) echo "style='color:red;'" ?>>Email:</label></td>
<td><input type="text" name="email" id="email"  <?php if($email) echo "value='".$email."'" ?>  /></td>
</tr>
<tr>
<td><br /></td>
</tr>

<tr><td><label for="address" class="textarea">Address/Location of project:</label></td>
<td><textarea rows="3" cols="20" name="address" id="address" style="width: 400px;"><?php if($address!="") echo $address ?></textarea></td>
</tr>
<tr>
<td><br /></td>
</tr>
<br />
<tr>
<td><label for="description" class="fixedwidth">Enquiry</label></td>
<td><textarea rows="3" cols="20" name="description" id="description" style="width: 400px;"><?php if($description!="") echo $description; ?></textarea></td>
<tr>
<td><br /></td>
</tr>

<!-- form -->
<tr>
<td><label>&nbsp;</label></td>
<td><input type="submit" value="Submit" name="submit" /></td>
</tr>
</table>
</form>
<?php } ?>        

我在这里错过了明显的东西吗?

Am i missing something obvious here?? Any help will really be appreciated thanks!

推荐答案

您已经使用了此处不需要的会话,也可以使用flag变量代替

You have used sessions which is not required here, you can also use flag variable instead of arrays in this simple form, use this updated code.

<?php
//$to_mail = "architects@palavin.com,t.lavin@palavin.com,12yorkcourt@gmail.com";
$to_mail = "danny@enhance.ie";
//$cc="paul@enhance.ie";
$mail_sent = 0;
if(isset($_POST['submit'])){
    //echo "the form was submitted";

$name = trim(strip_tags($_POST['name']));
if($name == "")
    $error = true;

$email = trim(strip_tags($_POST['email']));
if($email == "")
    $error = true;

$phone = trim(strip_tags($_POST['phone']));
$address = trim(strip_tags($_POST['address']));
$description = trim(strip_tags($_POST['description']));


if($error != true){
    $headers = 'From: "Euro Insulation" <no-reply@euroinsulations.ie>'."\r\n";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";

    $subject = "New contact message";

    $message = "New Contact message, received from: <br /> \n ";
    $message .= "<b>Name</b> ".$name."<br /> \n";
    $message .= "<b>Email</b> ".$email."<br /> \n";

    $message .= "<b>Phone</b> ".$phone."<br /> \n";
    $message .= "<b>Address</b> ".$address."<br /> \n";

    $message .= "<b>Description</b> ".$description."<br /> \n";

    if(@mail($to_mail,$subject,$message,$headers))
    {
        echo "mail sent";
        $mail_sent = 1;
    }
    else echo "mail not sent";
} else {
    echo 'validation error';
}
}
?> 

您还错过了表单验证测试的else语句,因此提交时不会显示任何错误表格。

You have also missed out the else statement for your form validation test so no errors getting displayed when you submit form.

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

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