dtnpzghys01643322 2014-05-29 01:02
浏览 78
已采纳

PHP联系表单没有返回错误,但我仍然没有收到任何电子邮件

Forgive me if this is a stupid question, but I am pretty new to PHP and I am running into some issues. I am trying to build a contact form with HTML, CSS, and PHP, but I can't seem to get my PHP form to send the contents of the form to my email address. This is what the code looks like for the HTML:

<div id="contact-form">
    <ul>
            <li><button id="quote" class="button1">Project Quote</button></li>
    </ul>

    <form class= "emai" action="mailer.php" method="post">
                    <p>Have a project in mind? Fill in the form for a quote!</p>
            <div>
                    <p><label for="name">What can I call you? <span>*</span></label></p>
                <input type="text" id="name" name="name">
            </div>
            <div>
                        <p><label for="email">What is your email? <span>*</span></label></p>
                <input type="email" name="email" id="email">
            </div>
            <div>
                        <p><label for="type">Type of Project? <span>*</span></label></p>
                <select id="type" name="type">
                        <option value="logo">Logo Design</option>
                    <option value="web dev">Website/WebApp Dev</option>
                        <option value="other">Other</option>    
                </select>
            </div>
            <div>
                    <p><label for="purpose">What is the main purpose of your project? <span>*</span></label></p>
                  <textarea id="purpose" name="purpose"></textarea>
            </div>
            <div>
                    <p><label for="features">Any extra features?</label></p>
                  <textarea id="features" name="features"></textarea>
            </div>

            <input class="button1" type="submit" value="submit">
    </form>

</div>

And in a separate doc called "mailer.php" this is what the code looks like:

<?php
/* Set e-mail recipient */
$myemail = "christopher.kenrick@gmail.com";
$subject = "Project Request";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$type = check_input($_POST['type'], "Select a type of project");
$purpose = check_input($_POST['purpose'], "What is the purpose of your project?");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Type: $type
Purpose: $purpose
Features: $features

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

Here is a link to my website if it will help. Can someone tell me what it is I am doing wrong?

  • 写回答

2条回答 默认 最新

  • duandai3964 2014-05-29 01:33
    关注

    Your code lacks proper mail headers. (and originally had a missing subject variable which you now added).

    Add and modify your present mail() function with the following code, otherwise mail will be sent directly to Spam as it did for my test.

    $headers = 'From: ' . $email . "
    ";
    mail($myemail, $subject, $message, $headers);
    

    with a conditional statement:

    if(mail($myemail, $subject, $message, $headers)){ 
       echo "Success"; } else{ echo "There was a problem.";}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测