-let email_notify ~subject ~body (dbh : Dbi.connection) hostid =
- (* Is anyone listed for email notification at this host? *)
- let sth = dbh#prepare_cached "select email, name from email_notify
- where hostid = ?" in
- sth#execute [`Int hostid];
+let email_notify ~subject ~body ?user (dbh : Dbi.connection) hostid =
+ (* Get own userid, if we have it. Don't want to send email
+ * notification back to the person who changed the page. If
+ * we don't have a userid, set it to 0, because no real user can
+ * have userid = 0.
+ *)
+ let own_userid =
+ match user with
+ | None
+ | Some Anonymous -> 0
+ | Some (User (userid, _, _)) -> userid in
+
+ (* Send a change email to everyone who hasn't opted out using
+ * their preferences. This behaviour replaces the old
+ * 'email_notify' table. Don't send email to invited accounts
+ * who have not yet confirmed.
+ *)
+ let sth = dbh#prepare_cached "select email, name from users
+ where hostid = ? and id <> ? and email_notify
+ and email is not null
+ and invite is null" in
+ sth#execute [`Int hostid; `Int own_userid];