首页 > 解决方案 > 我正在尝试使用 HTML 和 PHP 与我联系,但在发送消息时出现错误

问题描述

我正在尝试使用 HTML 和 PHP 创建一个与我联系的表单,但是每当有人尝试向我发送消息时,它都会出错。

这是我的 HTML 代码:

<div class="col-lg-9">
    <form class="row contact_form" action="contact_process.php" method="post" id="contactForm" novalidate="novalidate">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" id="name" name="name" placeholder="Enter your name">
            </div>
            <div class="form-group">
                <input type="email" class="form-control" id="email" name="email" placeholder="Enter email address">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" id="subject" name="subject" placeholder="Enter Subject">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <textarea class="form-control" name="message" id="message" rows="1" placeholder="Enter Message"></textarea>
            </div>
        </div>
        <div class="col-md-12 text-right">
            <button type="submit" value="submit" class="primary_btn">
                <span>Send Message</span>
            </button>
        </div>
    </form>
</div>

它与一个 PHP 文件 contact_process.php 相关联,它是:

<?php

    $to = "mygmail.com";
    $from = $_REQUEST['email'];
    $name = $_REQUEST['name'];
    $subject = $_REQUEST['subject'];
    $number = $_REQUEST['number'];
    $cmessage = $_REQUEST['message'];

    $headers = "From: $from";
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $from . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $subject = "You have a message from your Bitmap Photography.";

    $logo = 'img/logo.png';
    $link = '#';

    $body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
    $body .= "<table style='width: 100%;'>";
    $body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
    $body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
    $body .= "</td></tr></thead><tbody><tr>";
    $body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
    $body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
    $body .= "</tr>";
    $body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
    $body .= "<tr><td></td></tr>";
    $body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
    $body .= "</tbody></table>";
    $body .= "</body></html>";

    $send = mail($to, $subject, $body, $headers);

?>

所以你能告诉我我在哪里做错了吗

标签: phphtml

解决方案


更改代码(所有变量):

$number = $_REQUEST['number'];

$number = $_POST['number'];

对于检查邮件发送的更改:

$send = mail($to, $subject, $body, $headers);

if(mail($to, $subject, $body, $headers)){

echo 'done';
}else{
echo 'mail not sent';
}

推荐阅读