我使用PHP和MySQL表创建了一个评论表单。我的问题是,我如何做到这一点,当有人填写评论表时,它会自动向我发送电子邮件通知我?
表格的代码如下:
<?
@$todo=$_POST['todo'];
if(isset($todo) and $todo=="post_comment"){
$name=$_POST['name'];
$name=mysql_real_escape_string($name);
$email=$_POST['email'];
$email=mysql_real_escape_string($email);
$dtl=$_POST['dtl'];
$dtl=mysql_real_escape_string($dtl);
$status = "OK";
$msg="";
// if userid is less than 3 char then status is not ok
if( strlen($name) <3 or strlen($name) > 25){
$msg=$msg."Your name does not have sufficient characters.<BR>";
$status= "NOTOK";}
if( strlen($email) <7){
$msg=$msg."Please re-enter your email.<BR>";
$status= "NOTOK";}
if( strlen($dtl) <6 ){
$msg=$msg."Your comment does not have sufficient characters.<BR>";
$status= "NOTOK";}
if($status<>"OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font>";
}else{ // if all validations are passed.
$dt=date("Y-m-d");
$status='ns'; // Change this to apv if you want all messages to be automatically approved once posted.
$query=mysql_query("insert into cmt_post(p_name,dt,name,email,dtl,status)
values('$p_name','$dt','$name','$email','$dtl','$status')");
echo mysql_error();
echo "<font face='Verdana' size='2' color='#333333'><b><br><br>Thank you for your post! <br><br>
Your comment is waiting to be approved by Admin =) </b><br><br><br></font>";
//Send simple email
$to = "thefloatingorange@outlook.com;";
$from = "$email"; //Could be the person's email stored in a variable who is submitting the form
$subject = "Notification!"; //Your email subject
//Prepare your message. You can include sender's name and other detail
//stored in variables if you wish
$message = "Someone has posted a new comment.";
//Headers
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
// now lets send the email
mail($to, $subject, $message, $headers);
echo "Your message has been sent successfully.";
}
}// Checking of if condition if form is submittted
?>
答案 0 :(得分:0)
在表格中成功插入数据后,您可以使用mail()
功能发送通知。
以下是发送电子邮件的简单演示。在测试时,如果没有收到电子邮件,请务必检查垃圾文件夹内部。
//Send simple email
$to = "receiver@email.here";
$from = "sender@email.here"; //Could be the person's email stored in a variable who is submitting the form
$subject = "Notification!"; //Your email subject
//Prepare your message. You can include sender's name and other detail
//stored in variables if you wish
$message = "Someone has posted a new comment.";
//Headers
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
// now lets send the email
mail($to, $subject, $message, $headers);
echo "Your message has been sent successfully.";
这就是你应该如何在你的代码中使用mail()函数
@$todo=$_POST['todo'];
if(isset($todo) and $todo=="post_comment"){
$name=$_POST['name'];
$name=mysql_real_escape_string($name);
$email=$_POST['email'];
$email=mysql_real_escape_string($email);
$dtl=$_POST['dtl'];
$dtl=mysql_real_escape_string($dtl);
$status = "OK";
$msg="";
// if userid is less than 3 char then status is not ok
if( strlen($name) <3 or strlen($name) > 25){
$msg=$msg."Your name does not have sufficient characters.<BR>";
$status= "NOTOK";}
if( strlen($email) <7){
$msg=$msg."Please re-enter your email.<BR>";
$status= "NOTOK";}
if( strlen($dtl) <6 ){
$msg=$msg."Your comment does not have sufficient characters.<BR>";
$status= "NOTOK";}
if($status<>"OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font>";
}else{ // if all validations are passed.
$dt=date("Y-m-d");
$status='ns'; // Change this to apv if you want all messages to be automatically approved once
posted.
$query=mysql_query("insert into cmt_post(p_name,dt,name,email,dtl,status)
values('$p_name','$dt','$name','$email','$dtl','$status')");
echo mysql_error();
echo "<font face='Verdana' size='2' color='#333333'><b><br><br>Thank you for your post! <br><br>
Your comment is waiting to be approved by Admin =) </b><br><br><br></font>";
//Send simple email
$to = "thefloatingorange@outlook.com";
$from = $email; //Could be the person's email stored in a variable who is submitting the form
$subject = "Notification!"; //Your email subject
//Prepare your message. You can include sender's name and other detail
//stored in variables if you wish
$message = "Someone has posted a new comment.";
//Headers
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
// now lets send the email
mail($to, $subject, $message, $headers);
echo "Your message has been sent successfully.";
}
}// Checking of if condition if form is submittted