gpt4 book ai didi

php - 新 MySQL 数据库条目的电子邮件通知

转载 作者:行者123 更新时间:2023-11-29 04:59:10 30 4
gpt4 key购买 nike

我一直在使用网络教程创建博客,并且我有一个可用的评论系统,但我想要的是,如果当用户添加评论时我会收到一封电子邮件。如果您能解释一下我究竟可以如何实现通知,我将非常高兴。非常感谢任何帮助。

当前形式:

<form id="commentform" method="post" action="process.php">

<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />

<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">

<input type="text" name="name" id="name" title="Name (required)" /><br />

<input type="text" name="email" id="email" title="Mail (will not be published) (required)" /><br />

<input type="text" name="url" id="url" title="Website" value="http://" /><br />

<br />
<textarea title="Your Comment Goes Here" name="comment" id="comment"></textarea></p>

<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>

</form>

进程.php:

<?php
if (isset($_POST['submit_comment'])) {

if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
}

$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);

if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}

if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}

mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('ultankc');

$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");

header("Location: post.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>

最佳答案

如果您正在寻找可以发送电子邮件的 PHP 代码,下面是一个来自 here 的代码给你看。插入代码以在您将评论INSERT 到您的数据库之前或之后发送电子邮件。

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Requirements :

For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.

关于php - 新 MySQL 数据库条目的电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3463524/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com