You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将HTML/CSS表单提交结果发送至指定邮箱?

解决联系表单通过mailto发送数据失败的问题

原表单使用mailto:作为action的方式存在兼容性问题,依赖用户本地邮件客户端,不同浏览器和客户端对method="post"、enctype="text/plain"的解析不一致,导致表单数据无法正确传递到指定邮箱。另外提交按钮不需要添加href属性,input[type=submit]仅需配合form的action属性工作。

以下是两种可行的解决方案:

方案1:优化mailto方式(临时应急用,不推荐生产环境)

调整form的属性,改用method="get",移除或调整enctype,同时可以在action中预设邮件主题,避免字段名冲突:

<form action="mailto:kasenlindquist@gmail.com?subject=汽车旅馆客户留言" method="get" name="EmailForm">
    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstname" placeholder="Your name..">

    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name..">

    <label for="email">Email</label>
    <input type="email" id="email" name="email" placeholder="Your Email.."> <!-- 改用email类型更规范 -->

    <label for="subject">Write a message</label>
    <textarea id="subject" name="message" placeholder="Send us a message.." style="height:200px"></textarea> <!-- 字段名改为message,避免和subject参数冲突 -->

    <input type="submit" value="Submit">
</form>

注意:这种方式仍依赖用户本地邮件客户端,用户需要手动确认发送,数据格式可能混乱,仅适合临时测试。

方案2:服务器端脚本处理(生产环境推荐)

使用PHP编写简单的邮件处理脚本,这是最可靠的方式,无需依赖用户本地邮件客户端:

步骤1:修改HTML表单

将form的action指向PHP脚本:

<form action="send-message.php" method="post" name="EmailForm">
    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstname" placeholder="Your name.." required>

    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name.." required>

    <label for="email">Email</label>
    <input type="email" id="email" name="email" placeholder="Your Email.." required>

    <label for="subject">Write a message</label>
    <textarea id="subject" name="message" placeholder="Send us a message.." style="height:200px" required></textarea>

    <input type="submit" value="Submit">
</form>

步骤2:创建PHP处理脚本

在网站根目录创建send-message.php文件,内容如下:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取并清理表单数据
    $firstname = trim($_POST["firstname"]);
    $lastname = trim($_POST["lastname"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);

    // 配置邮件参数
    $to = "kasenlindquist@gmail.com";
    $subject = "汽车旅馆客户留言:{$firstname} {$lastname}";
    $email_content = "姓名:{$firstname} {$lastname}\n";
    $email_content .= "联系邮箱:{$email}\n\n";
    $email_content .= "留言内容:\n{$message}\n";
    $email_headers = "From: {$firstname} {$lastname} <{$email}>";

    // 发送邮件并反馈结果
    if (mail($to, $subject, $email_content, $email_headers)) {
        echo "<div style='padding:20px;background:#f2f2f2;border-radius:4px;'>留言已成功发送,我们会尽快回复您!</div>";
    } else {
        echo "<div style='padding:20px;background:#f2f2f2;border-radius:4px;'>发送失败,请稍后重试或直接联系我们。</div>";
    }
}
?>

注意:需确保网站服务器支持PHP和mail()函数,大部分虚拟主机默认支持。若发送不稳定,可使用PHPMailer库优化邮件发送逻辑。

原CSS代码可直接保留使用,无需修改。

内容的提问来源于stack exchange,提问作者Kasen Lindquist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 03:36:31