关于新MySQL数据库条目的电子邮件通知

时间:2010-08-11 23:01:52

标签: php mysql email notifications

我一直在创建一个使用网络教程的博客,我有一个工作评论系统,但我想要的是,如果用户添加评论,我收到一封电子邮件。如果你能解释一下如何实现通知我真的很感激。非常感谢任何帮助。

目前的表格:

<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>

Process.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.");
}
?>

1 个答案:

答案 0 :(得分:4)

如果您正在寻找可以发送电子邮件的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

  

对于Mail函数   可用,PHP必须有权访问   在你的系统上发送sendmail二进制文件   编译时间。如果您使用其他邮件   程序,如qmail或postfix,是   一定要使用合适的sendmail   与他们一起来的包装纸。 PHP会   首先在PATH中查找sendmail,   然后在以下内容中:   在/ usr / bin中:/ usr / sbin目录:/ USR的/ etc中:/ etc:在/ usr / ucblib中:/ usr / lib目录。   强烈建议有   可以从你的PATH获得sendmail。   此外,编译PHP的用户必须   有权访问sendmail   二进制的。