【问题标题】:Issue with very basic contact form (HTML,PHP) [duplicate]非常基本的联系表格(HTML,PHP)的问题[重复]
【发布时间】:2018-03-20 12:59:01
【问题描述】:

我正在尝试用 PHP 制作一个非常基本的联系表格。我正在使用 MAMP 来测试它。但是,由于某种原因,该表格不起作用:当我填写表格并按“提交”按钮时,我被重定向到 index.html?mailsend 但我没有收到任何电子邮件!我做错了什么?

我的 HTML 代码:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="contactform.php" method="POST">
        <input type="text" name="name" placeholder="Full Name">
        <input type="text" name="mail" placeholder="Your e-mail">
        <input type="text" name="subject" placeholder="Subject">
        <textarea name="message" placeholder="Message"></textarea>
        <button type="submit" name="submit">Send e-mail</button>
    </form>

</body>
</html>

我的 PHP 代码:

if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];


$mailTo = "tomms1998@gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;

mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsend");
}

?>

【问题讨论】:

  • 你是在本地服务器上测试这个吗? mail() 无法在本地主机上工作
  • 尝试使用 SMTP 发送邮件。
  • 我明白了。如果我在线托管网站,我的代码会起作用吗?

标签: php html forms mamp contact


【解决方案1】:

您必须在您的服务器上配置 SMTP。您可以免费使用 Google 提供的 G Suite SMTP:

<?php

$mail = new PHPMailer(true);

// Send mail using Gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "your-gmail-account@gmail.com"; // GMAIL username
    $mail->Password = "your-gmail-password"; // GMAIL password
}

// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    // Something went bad
    echo "Fail :(";
}

?>

【讨论】:

  • 感谢您的帮助。我试过你的代码,但由于某种原因它不起作用。现在我知道函数 mail() 在 localhost 中不起作用,我想知道:如果我要在线托管网站,我的代码会起作用吗?跨度>
  • @tommsyeah 是的,在您的代码可以工作的实时服务器上。
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2016-06-23
  • 1970-01-01
相关资源
最近更新 更多