【问题标题】:Email Notification on new MySQL database entry新 MySQL 数据库条目的电子邮件通知
【发布时间】:2011-03-28 16:06:20
【问题描述】:

我一直在使用网络上的教程创建博客,并且我有一个可用的 cmets 系统,但我希望当用户添加评论时我会收到一封电子邮件。如果您能解释我如何准确地实施通知,我真的很高兴。非常感谢任何帮助。

当前形式:

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

【问题讨论】:

  • 不要使用加斜线来转义数据库查询的数据。使用mysql_real_escape_string,或者更好的是,使用准备好的查询...
  • 偶尔看到注射工作很有趣。 xkcd.com/327

标签: php mysql email notifications


【解决方案1】:

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

对于邮件功能 可用,PHP 必须有权访问 sendmail 二进制文件在您的系统上期间 编译时间。如果您使用其他邮件 程序,例如 qmail 或 postfix,是 确保使用适当的 sendmail 随附的包装纸。 PHP 将 首先在您的 PATH 中查找 sendmail, 然后在下面: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. 强烈建议拥有 sendmail 可从您的 PATH 获取。 此外,编译 PHP 的用户必须 有权访问发送邮件 二进制。

【讨论】:

  • 如果我希望电子邮件显示在评论表单中输入的信息怎么办?我想要它,以便它向我发送评论的姓名、电子邮件和条目。
  • $message 变量保存您的消息。使用字符串连接创建所需的消息并发送。
  • 我可以使用 Mamp 进行测试吗?当我进入 cmets 时,我似乎没有收到任何电子邮件。
  • 您需要在机器上运行邮件服务器。更多信息here。另请阅读答案的更新。
猜你喜欢
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多