简体   繁体   English

为什么我的提交按钮不能将信息发送到我想要的 email? (PHP)

[英]Why won't my submit button send info to my desired email? (PHP)

I'm trying to have my submit button send name, email and message to my desired address.我正在尝试让我的提交按钮将名称 email 和消息发送到我想要的地址。 When I hit send, i get a page that says "This page isn't working - HTTP error 405."当我点击发送时,我得到一个页面,上面写着“此页面无法正常工作 - HTTP 错误 405。” Some notes: I'm using visual studio code and using their live server extension to test it.一些注意事项:我正在使用 Visual Studio 代码并使用他们的实时服务器扩展来测试它。 I have script tags in HTML linking it to my php file (not sure if this matters as it's already linked under form element?) The "contact.php" is my php file.我在 HTML 中有脚本标签,将其链接到我的 php 文件(不确定这是否重要,因为它已经在表单元素下链接?)“contact.php”是我的 php 文件。 I'm a noobie so I'm hoping I'm making a simple mistake.我是个菜鸟,所以我希望我犯了一个简单的错误。

// MY PHP

<?php

$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];

$to = "stackoverflowexample@gmail.com";
$body = "";

$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";

mail($to,$body);

?>

// MY HTML

              <form action="contact.php" method="POST">
                

                <h2>Send Message</h2>
                <div class="inputbox">
                    <input type="text" name="name" required="required">
                    <span>Full Name</span>
                </div>

                <div class="inputbox">
                    <input type="text" name="email" required="required">
                    <span>Email</span>
                </div>

                <div class="inputbox">
                    <textarea required="required" name="message"></textarea>
                    <span>Type your Message...</span>
                </div>

                <div class="inputbox">
                    <input type="submit" name="" value="Send">
                </div>

            </form>
  • If you write both HTML and PHP code in the same file then you need to save that single file as contact.php .如果您在同一文件中同时编写HTMLPHP代码,则需要将该单个文件保存为contact.php
  • Also in PHP code Please set the condition to avoid warning like Undefined index同样在 PHP 代码中请设置条件以避免像未定义索引这样的警告
  • Also mail function must have at least 3 parameters 1. to 2. Subject 3. Body另外邮件function 必须至少有 3 个参数 1. 到 2. 主题 3. 正文

See below code见下面的代码

<?php

// set condition 
if(!empty($_POST)) {

$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];

$to = "stackoverflowexample@gmail.com";
$body = "";

$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";


// set subject name as second param
mail($to,"test",$body);

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM