xref: /PHP-5.4/sapi/milter/milter.php (revision bf16a54f)
1<?php
2/**
3 * example milter script
4 *
5 * run: php-milter -D -p /path/to/sock milter.php
6 *
7 * for details on how to set up sendmail and configure the milter see
8 * http://www.sendmail.com/partner/resources/development/milter_api/
9 *
10 * for api details see
11 * http://www.sendmail.com/partner/resources/development/milter_api/api.html
12 *
13 * below is a list of all callbacks, that are available through the milter sapi,
14 * if you leave one or more out they simply won't get called (e.g. if you secify an
15 * empty php file, the milter would do nothing :)
16 */
17
18/**
19 * this function is called once on sapi startup,
20 * here you can specify the actions the filter may take
21 *
22 * see http://www.sendmail.com/partner/resources/development/milter_api/smfi_register.html#flags
23 */
24
25function milter_log($msg)
26{
27	$GLOBALS['log'] = fopen("/tmp/milter.log", "a");
28	fwrite($GLOBALS['log'], date("[H:i:s d.m.Y]") . "\t{$msg}\n");
29	fclose($GLOBALS['log']);
30}
31
32function milter_init() {
33	milter_log("-- startup --");
34	milter_log("milter_init()");
35	smfi_setflags(SMFIF_ADDHDRS);
36}
37
38/**
39 * is called once, at the start of each SMTP connection
40 */
41function milter_connect($connect)
42{
43	milter_log("milter_connect('$connect')");
44}
45
46/**
47 * is called whenever the client sends a HELO/EHLO command.
48 * It may therefore be called between zero and three times.
49 */
50function milter_helo($helo)
51{
52	milter_log("milter_helo('$helo')");
53}
54
55/**
56 * is called once at the beginning of each message,
57 * before milter_envrcpt.
58 */
59function milter_envfrom($args)
60{
61	milter_log("milter_envfrom(args[])");
62	foreach ($args as $ix => $arg) {
63		milter_log("\targs[$ix] = $arg");
64	}
65}
66
67/**
68 * is called once per recipient, hence one or more times per message,
69 * immediately after milter_envfrom
70 */
71function milter_envrcpt($args)
72{
73	milter_log("milter_envrcpt(args[])");
74	foreach ($args as $ix => $arg) {
75		milter_log("\targs[$ix] = $arg");
76	}
77}
78
79/**
80 * is called zero or more times between milter_envrcpt and milter_eoh,
81 * once per message header
82 */
83function milter_header($header, $value)
84{
85	milter_log("milter_header('$header', '$value')");
86}
87
88/**
89 * is called once after all headers have been sent and processed.
90 */
91function milter_eoh()
92{
93	milter_log("milter_eoh()");
94}
95
96/**
97 * is called zero or more times between milter_eoh and milter_eom.
98 */
99function milter_body($bodypart)
100{
101	milter_log("milter_body('$bodypart')");
102}
103
104/**
105 * is called once after all calls to milter_body for a given message.
106 * most of the api functions, that alter the message can only be called
107 * within this callback.
108 */
109function milter_eom()
110{
111	milter_log("milter_eom()");
112  /* add PHP header to the message */
113  smfi_addheader("X-PHP", phpversion());
114}
115
116/**
117 * may be called at any time during message processing
118 * (i.e. between some message-oriented routine and milter_eom).
119 */
120function milter_abort()
121{
122	milter_log("milter_abort()");
123}
124
125/**
126 * is always called once at the end of each connection.
127 */
128function milter_close()
129{
130	milter_log("milter_close()");
131}
132?>
133