【问题标题】:Contact form doesnt send the message [duplicate]联系表格不发送消息[重复]
【发布时间】:2019-08-14 16:20:51
【问题描述】:

我在 php 中的联系表没有将消息发送到邮件,所以我需要知道这里的问题是什么 您将在此处找到带有文件名的 html 表单:index.php 和名称为 php 的表单:mail.php

<form class="form" action="mail.php" method="post" name="contactform">
    <input class="name" type="text" placeholder="Name" name="name">
    <input class="email" type="email" placeholder="Email" name="email" >
    <input class="phone" type="text" placeholder="Phone No:" name="phone">
    <textarea class="message" id="message" cols="30" rows="10" placeholder="Message"name="message"  ></textarea>
    <input class="submit-btn" type="submit" value="Submit">
</form>

<?php
if (isset($_POST['submit']) ) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'From:  phone'; 
    $to = 'modysaid26@gmail.com'; 
    $subject = 'message';

    $body = "From: $name\n E-Mail: $email\n Phone Number: $phone\n Message:\n $message";

    if (isset($_POST['submit'])) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been submitted</p>';
        } else { 
            echo '<p>Something went wrong, please try again!</p>'; 
        }
    }
}
?>

【问题讨论】:

标签: php html


【解决方案1】:

第一个提交按钮名称丢失,请使用一个

<input class="submit-btn" type="submit" value="Submit" name="submit">

您发送电子邮件的第二个命令 (mail ($to, $subject, $body, $from)) 没有正确的电子邮件标题。插入您的$from 请使用以下参数定义标题

$email_headers = "From: ".$from_name." <".$from_email.">\r\n".
"Reply-To: ".$reply_to."\r\n" ;
if ($cc) $email_headers.="Cc: ".$cc."\r\n";
if ($bcc) $email_headers.="Bcc: ".$bcc."\r\n";
$email_headers.="MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";

$email_body=$_POST['message'];

然后使用发送它

mail($to, $subject, $email_body, $email_headers);

然后您的电子邮件应该会正确发送。

【讨论】:

  • 定义代码中的 email_body 在哪里?
  • 我的错误......,在 $email_body 只是把你的消息输入的内容像 $email_body=$_POST['message'];
【解决方案2】:

你也不需要把名字放在表单标签上删除类: &lt;form action="mail.php" method="post"&gt;

【讨论】:

    【解决方案3】:
    <input class="submit-btn" name='submit' type="submit" value="Submit">
    

    您缺少将名称添加到提交按钮,因此您的案例 if (isset($_POST['submit']) ) { 失败

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      相关资源
      最近更新 更多