milter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * this function is called once on sapi startup,
  19. * here you can specify the actions the filter may take
  20. *
  21. * see http://www.sendmail.com/partner/resources/development/milter_api/smfi_register.html#flags
  22. */
  23. function milter_log($msg)
  24. {
  25. $GLOBALS['log'] = fopen("/tmp/milter.log", "a");
  26. fwrite($GLOBALS['log'], date("[H:i:s d.m.Y]") . "\t{$msg}\n");
  27. fclose($GLOBALS['log']);
  28. }
  29. function milter_init() {
  30. milter_log("-- startup --");
  31. milter_log("milter_init()");
  32. smfi_setflags(SMFIF_ADDHDRS);
  33. }
  34. /**
  35. * is called once, at the start of each SMTP connection
  36. */
  37. function milter_connect($connect)
  38. {
  39. milter_log("milter_connect('$connect')");
  40. }
  41. /**
  42. * is called whenever the client sends a HELO/EHLO command.
  43. * It may therefore be called between zero and three times.
  44. */
  45. function milter_helo($helo)
  46. {
  47. milter_log("milter_helo('$helo')");
  48. }
  49. /**
  50. * is called once at the beginning of each message,
  51. * before milter_envrcpt.
  52. */
  53. function milter_envfrom($args)
  54. {
  55. milter_log("milter_envfrom(args[])");
  56. foreach ($args as $ix => $arg) {
  57. milter_log("\targs[$ix] = $arg");
  58. }
  59. }
  60. /**
  61. * is called once per recipient, hence one or more times per message,
  62. * immediately after milter_envfrom
  63. */
  64. function milter_envrcpt($args)
  65. {
  66. milter_log("milter_envrcpt(args[])");
  67. foreach ($args as $ix => $arg) {
  68. milter_log("\targs[$ix] = $arg");
  69. }
  70. }
  71. /**
  72. * is called zero or more times between milter_envrcpt and milter_eoh,
  73. * once per message header
  74. */
  75. function milter_header($header, $value)
  76. {
  77. milter_log("milter_header('$header', '$value')");
  78. }
  79. /**
  80. * is called once after all headers have been sent and processed.
  81. */
  82. function milter_eoh()
  83. {
  84. milter_log("milter_eoh()");
  85. }
  86. /**
  87. * is called zero or more times between milter_eoh and milter_eom.
  88. */
  89. function milter_body($bodypart)
  90. {
  91. milter_log("milter_body('$bodypart')");
  92. }
  93. /**
  94. * is called once after all calls to milter_body for a given message.
  95. * most of the api functions, that alter the message can only be called
  96. * within this callback.
  97. */
  98. function milter_eom()
  99. {
  100. milter_log("milter_eom()");
  101. /* add PHP header to the message */
  102. smfi_addheader("X-PHP", phpversion());
  103. }
  104. /**
  105. * may be called at any time during message processing
  106. * (i.e. between some message-oriented routine and milter_eom).
  107. */
  108. function milter_abort()
  109. {
  110. milter_log("milter_abort()");
  111. }
  112. /**
  113. * is always called once at the end of each connection.
  114. */
  115. function milter_close()
  116. {
  117. milter_log("milter_close()");
  118. }
  119. ?>