我不知道为什么我的脚本不能正常工作。我正在使用bootstrap表单。当我的表单填写完毕并单击“提交”按钮后,我将重定向到空白网页,并且没有发送任何内容:
http://mywebpageaddress.com/send_form_email.php?comments=Hi%2C%0D%0Afirst+message%0D%0AJohn
bootstrap index.html代码表单:
<form action="send_form_email.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="first_name" class="col-lg-2 control-label">First name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Enter you first name">
</div>
</div><!-- End form group -->
<div class="form-group">
<label for="last_name" class="col-lg-2 control-label">Last name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Enter you last name">
</div>
</div><!-- End form group -->
<div class="form-group">
<label for="email" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter you Email Address">
</div>
</div><!-- End form group -->
<div class="form-group">
<label for="telephone" class="col-lg-2 control-label">Telephone</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="Enter you phone number">
</div>
</div>
<div class="form-group">
<label for="comments" class="col-lg-2 control-label">Any Message</label>
<div class="col-lg-10">
<textarea name="comments" id="comments" name="comments" class="form-control"
cols="20" rows="10" placeholder="Enter your Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP文件:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "myemailt@gmail.com";
$email_subject = "website html form submissions";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
输出:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "myemailt@gmail.com";
$email_subject = "website html form submissions";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
答案 0 :(得分:2)
您没有在form
元素中设置方法。所以默认设置为get
。变化
<form action="send_form_email.php" method="post" class="form-horizontal">
并更改输入字段并设置名称,如:
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Enter you first name">
等等。
答案 1 :(得分:1)
我认真不建议使用php邮件功能,因为有时在我的情况下不起作用...另外你没有在你的表单中设置方法,你必须设置它:
<form method="post" action="ha.php>
同样,你发送带有php无法获取的字段的表单!例如:
<input type="text" class="form-control" id="last_name" placeholder="Enter you last name"> <!-- Where is the name="last_name" ? Otherwise you can't retrieve it with php -->
有一个php库可以让你的生活变得简单:
你可以在SwiftMailer官方文档中看到很多有用的文档
例如,您可以尝试使用
发送邮件 require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) // the smtp of google or live.
//google for example neeeds : Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('your username')
->setPassword('your password')
;
//live for example
$transport = \Swift_SmtpTransport::newInstance()
->setUsername('blablaba@outlook.com')->setPassword('blablablabla!')
->setHost('smtp-mail.outlook.com')
->setPort(587)->setEncryption('tls');
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);