我无法将PHP表单中的信息发送到电子邮件地址。我是PHP的新手。代码如下:
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$myEmail = "shivambh28@gmail.com";
if (empty($name) || empty($subject) || empty($message)) {
$error = 'Please make sure to double check the fields for errors.';
} elseif (!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
$error = 'Email is incorrect';
} else {
$headers .= "From: $email\r\n";
$headers .= "Reply-To: $myEmail\r\n";
$headers .= "Return-Path: $myEmail\r\n";
$headers .= "CC: $email\r\n";
$headers .= "BCC: $myEmail\r\n";
if ( mail($to,$subject,$message,$headers) ) {
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
echo "Mail Sent.";
} else {
echo 'failure to send email';
}
}
}
HTML:
<form id="contactForm" class="form-horizontal" action="<?php echo get_option('home'); ?>/email/" method="POST">
<input id="name" name="name" placeholder="Full Name" type="text">
<input id="subject" name="subject" placeholder="Subject" type="text">
<input id="email" name="email" placeholder="Email Address" type="email">
<textarea placeholder="Your Message" id="message" name="message" rows="10"></textarea>
<input type="submit" value="SEND" class="btn btn-primary btn-block">
</form>
注意:我使用的是WP CMS。
答案 0 :(得分:1)
您的表单缺少method
属性。编辑代码,以便您的表单具有method
POST。
<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="POST">
其次删除一个mail
函数调用。如果不是,您的电子邮件将被发送两次
答案 1 :(得分:0)
更改您的代码。它发送邮件2次
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if ( mail($to,$subject,$message,$headers) ) {
echo "Mail Sent.";
} else {
echo 'failure to send email';
}
您的表单方法与POST
<form id="contactForm" class="form-horizontal" action="contact.php" method="post">
您的文件名主要是contact.php
或contact.tpl
不 contact.tpl.php
答案 2 :(得分:0)
第一个.
变量声明中缺少$headers
(句号)。可能有帮助。
答案 3 :(得分:0)
表单标记中缺少表单方法 POST 。
<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="post">
答案 4 :(得分:0)
$to
mail()
错误
尝试
....
....
//// use $email here not $to which is not initialised yet
if ( mail($email,$subject,$message,$headers) ) {
是
的地方 if ( mail($to,$subject,$message,$headers) ) {
答案 5 :(得分:0)
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if(@mail($to, $subject, $message, $headers)) {
echo "Mail Sent";
} else {
echo "Fail";
}