【问题标题】:Html Form, when submitted, send straight to emailHtml 表单,提交后,直接发送到电子邮件
【发布时间】:2016-04-12 10:29:44
【问题描述】:

我正在使用 Dreamweaver 创建网站,我的网站有一个表单,但是当我单击提交按钮时,它会打开我的 Outlook 应用程序,其中包含我想要的信息。我只是想知道是否可以在不通过我的电子邮件客户端的情况下将电子邮件发送到我的帐户。这是我的代码...

顺便说一句,我正在使用 Dreamweaver 编辑所有代码。

<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fiction Filming</title>
<link rel="shortcut icon" href="images/favicon3.ico" type="image/x-icon" />
<style type="text/css">
body {
    background-color: #2c2c2c;
}
</style>
<link href="Css/singlePageTemplate.css" rel="stylesheet" type="text/css">
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/source-sans-pro:n2:default.js" type="text/javascript"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- Main Container -->
<div class="container"> 
  <!-- Navigation -->
  <!-- Hero Section -->
  <!-- About Section -->
  <!-- Stats Gallery Section -->
  <div class="gallery"><img src="Images/Newbannercomingsoon.png" alt="" width="1000" height="500" class="logo-pic"/> </div>
  <!-- Parallax Section -->
  <!-- More Info Section -->
  <!-- Footer Section -->
  <section class="footer_banner" id="contact">

<form class="subscribeForm form-fix" name="Subscription Form" method="post" action="mailform.php" enctype="text/plain">
            <div class="newform">
              <div> </div>
              <div>
                <input id="fname" type="text" placeholder="NAME" name="name" required>
              </div>
              <div>
                <input name="email" type="email" required id="email" placeholder="EMAIL">
              </div>
              <div>
                <select name="select" required id="myselect">
                  <option>HELP / SUPPORT</option>
                  <option>BUSINESS ENQUIRES</option>
                  <option>OTHER</option>
                </select>
              </div>
              <div class="text-form">
                <div>
                  <textarea name="textarea" required id="textarea" placeholder="TYPE YOUR TEXT HERE"></textarea>
                </div>
              </div>
              <br><input name="Send" type="submit" id="Send" value="Send">
</div>
</form>
<!-- Step 1: Add an email field here -->

<!-- Step 2: Add an address field here -->
            <!-- Step 3: add a submit button here -->

</section>
  <!-- Copyrights Section -->
<div class="copyright">&copy;2016 - <strong>Fiction filming</strong></div>
</div>
<!-- Main Container Ends -->
</body>
</html>

另外,这是我的 php 文件...如果你能修复它,那将非常有帮助。

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8">
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){
    $to = "example@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $message = $sender_name . " wrote the following:" . "\n\n" . $_POST['textarea'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>
</body>
</html>

这是新的 PHP

    <!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8">
<META HTTP-EQUIV="refresh" content="3;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){

// Or the below if using "name="Send" for the input. Uncomment and get rid of the above
// if(isset($_POST['Send'])){


    if(!empty($_POST['email']) 
    && !empty($_POST['name']) 
    && !empty($_POST['select'])
    && !empty($_POST['textarea'])) {

    $to = "mail@fictionfilming.com";
    $from = $_POST['email'];
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $textarea = $_POST['textarea'];

    $message = $sender_name . " wrote the following:" . "\n\n" . $textarea;

    if(mail($to,$subject,$message,$headers)){
    echo "Mail was sent. Check both your inbox and spam, as mail as done its job.";
}
else{
    echo "There was a problem. Check your logs.";
}

    }

}

   // $headers = "From:" . $from;
   // $headers2 = "From:" . $to;
    //echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    //}
?>
</body>
</html>

【问题讨论】:

  • 这里有很多问题
  • 是吗?你能说出它们是什么吗?
  • 请参阅我在下面为您发布的答案。 stackoverflow.com/a/36572685/1415724
  • 如果您可能已经看到我的回答,我在提交按钮方面犯了一个错误。重新加载它以查看我为它所做的更改。

标签: php html forms email submit


【解决方案1】:

您的代码存在一些问题。

首先,删除enctype="text/plain"。该类型无法通过表单发送 POST 数组。

您还应该删除&lt;META HTTP-EQUIV="refresh" content="0;URL=thankyou.html"&gt;,因为这可能会对您造成一些讨厌的伎俩。

然后您的输入 &lt;input name="Send" type="submit" id="Send" value="Send"&gt; 带有 Send 名称属性,并且您正在使用以下永远不会触发的条件语句:

if(isset($_POST['submit']))

要么将您的输入重命名为name="submit",要么将您的条件语句重命名为if(isset($_POST['Send']))选择权在你。

错误报告会让您对此有所了解。

现在,我不明白为什么你的代码中有这个 $headers2 = "From:" . $to;,因为它没有在任何地方使用,所以我不能说太多,除了摆脱它。

如果有人决定不填写任何或所有输入,您还应该检查空字段,从而导致电子邮件为空。

即,我在为 POST 数组分配时将您的 $_POST['textarea'] 替换为 $textarea

旁注:关于“提交”和“发送”,请参见上文;在此处为下面的if(isset($_POST['submit'])) 进行相应修改。

if(isset($_POST['submit'])){

// Or the below if using "name="Send" for the input. Uncomment and get rid of the above
// if(isset($_POST['Send'])){

    if(!empty($_POST['email']) 
    && !empty($_POST['name']) 
    && !empty($_POST['select'])
    && !empty($_POST['textarea'])) {

    $from = $_POST['email'];
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $textarea = $_POST['textarea'];

    $message = $sender_name . " wrote the following:" . "\n\n" . $textarea;

    // Put your other mail code here

    }

}

所有这一切都假设mail() 在您运行它的服务器上可用。

我建议你更换:

mail($to,$subject,$message,$headers);

带有条件语句,以检查是否已发送:

if(mail($to,$subject,$message,$headers)){
    echo "Mail was sent. Check both your inbox and spam, as mail as done its job.";
}
else{
    echo "There was a problem. Check your logs.";
}

error reporting 添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:只应在暂存阶段显示错误,而不是在生产阶段。


脚注:

我注意到您使用的是 ID,因此如果您使用的是原始问题中未包含的 JS/Ajax/jQuery,则超出了问题的范围。

目前尚不清楚您是如何访问文件的,因此如果您在托管站点上,那么我给您的内容应该可以工作。

如果是服务器问题,那么这也超出了问题的范围,您需要联系您的服务提供商以获得技术支持。

如果您在自己的 PC 上使用它,则需要安装 PHP、网络服务器和 mail(),并正确配置并使用 http://localhost/file.xxx 而不是 file:///file.xxx

旁注:如果没有从 &lt;option&gt; 传递,那么您可能需要给它们赋值。

即:&lt;option value="HELP / SUPPORT"&gt; 同时为其他人这样做。

我相信我已经给了你足够的东西让你继续前进。

【讨论】:

  • 只是想知道,你说要去掉 标签,但这是否意味着它不会重定向到我的感谢页面?
  • 另外,我只是想知道,您只提到了 $sender_name 和 $textarea 。这是否意味着将使用 $select,因为我希望它成为电子邮件的主题,或者只是消息中的某个地方...谢谢
  • @NicholasKlvana-Hooper 然后重新添加它。但是0 可能重定向太快并且可能无法处理您的 PHP,但请尝试一下。您还可以使用标头进行重定向。您没有尝试过我在答案中包含的内容吗?我有,而且效果很好。如果问题没有解决,那么我不知道还能说什么或做什么来帮助你。如果确实解决了问题,则考虑接受答案,否则其他人可能会认为问题仍然未解决/未解决,可能会发布更多答案。
  • 很抱歉打扰您,但我刚刚尝试了代码,它运行良好。我将您所说的 0 更改为 5。代码有一个缺陷...我没有发送电子邮件...我认为是因为它丢失了我的电子邮件...我将这个放在哪里?
  • 嗨 Fred -ii- 我刚刚更改了我的问题,所以 php 部分与当前启用的部分是最新的......也许你可以告诉我我做错了什么......
【解决方案2】:

由于您在表单标签中提到该操作为 action="mailto:mail@fictonfilming.com" 它正在尝试访问 Outlook

将操作更改为您的 php 文件的名称 action="yourphpfilename.php"

【讨论】:

  • 对不起,我实际上复制了错误的版本...它确实/确实说 mailform.php 而不是 mailto 命令。
【解决方案3】:

根据您的服务器配置,您可能无法使用简单的 php 邮件功能发送邮件。

要对其进行测试,请使用:

print mail($to,$subject,$message,$headers);

如果结果不是 1,则说明有些东西不好,修复它可能并不容易。

我建议您使用 PHPmailer 或基于 API 的服务,例如 MailGun。

【讨论】:

  • 所以我应该在它前面写 print 而不是 mail ......那么 1 会出现在哪里?顺便说一句,我正在使用 Dreamweaver 编辑代码。
  • 您使用的是本地服务器吗?你必须有一个本地服务器来运行 PHP,当你运行 PHP 文件时,你应该看到函数的输出。如果你没有本地服务器,我建议 Windows 使用 XAMPP,MAC 使用 MAMP。
  • 对不起,我对编码有点陌生。什么是本地服务器? PHP 文件运行,并重定向到我想要的页面,但它不发送电子邮件。
猜你喜欢
  • 2015-03-28
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 2013-08-15
  • 2017-09-20
相关资源
最近更新 更多