我正在尝试在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");
}
?>
答案 0 :(得分:-1)
您必须在服务器上配置SMTP。您可以免费使用G Suite SMTP by Google:
<?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 :(";
}
?>