Removed dependency on imported merjislib.
[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.2 2004/09/07 14:58:34 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 (* This is where we coordinate email notification from various
13  * scripts which create or update the wiki.
14  *)
15 let email_notify ~subject ~body (dbh : Dbi.connection) hostid =
16   (* Is anyone listed for email notification at this host? *)
17   let sth = dbh#prepare_cached "select email, name from email_notify
18                                  where hostid = ?" in
19   sth#execute [`Int hostid];
20
21   let to_addr = sth#map (function
22                            | [`String email; `String name] ->
23                                name ^ " <" ^ email ^ ">"
24                            | [`String email; `Null] ->
25                                email
26                            | _ -> assert false) in
27
28   if to_addr <> [] then (
29     (* Prepare the body of the message.  The assumption is that
30      * this takes time and database access, so we defer the creation
31      * of the body until we know that someone needs to be notified.
32      *)
33     let body = body () in
34
35     let subject = "Wiki notice: " ^ subject in
36
37     (* Send the email. *)
38     Sendmail.send_mail ~subject ~to_addr ~body ()
39   )