Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / cocanwiki_emailnotify.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: cocanwiki_emailnotify.ml,v 1.1 2004/09/07 10:14:09 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Merjisforwiki
13
14 (* This is where we coordinate email notification from various
15  * scripts which create or update the wiki.
16  *)
17 let email_notify ~subject ~body (dbh : Dbi.connection) hostid =
18   (* Is anyone listed for email notification at this host? *)
19   let sth = dbh#prepare_cached "select email, name from email_notify
20                                  where hostid = ?" in
21   sth#execute [`Int hostid];
22
23   let to_addr = sth#map (function
24                            | [`String email; `String name] ->
25                                name ^ " <" ^ email ^ ">"
26                            | [`String email; `Null] ->
27                                email
28                            | _ -> assert false) in
29
30   if to_addr <> [] then (
31     (* Prepare the body of the message.  The assumption is that
32      * this takes time and database access, so we defer the creation
33      * of the body until we know that someone needs to be notified.
34      *)
35     let body = body () in
36
37     let subject = "Wiki notice: " ^ subject in
38
39     (* Send the email. *)
40     Sendmail.send_mail ~subject ~to_addr ~body ()
41   )