Advertisement
jennykinz

Pipe incoming mail to php script

May 28th, 2018
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. ######### ROUGH STEPS TO PIPING INCOMING MAIL TO PHP SCRIPT #############
  2. os : centos 6.6 (/w zpanel)
  3. stmpd : postfix
  4.  
  5. # add forward/virtual alias to zpanel_postfix alias table
  6.  
  7. [ address ] [ goto ]
  8.  
  9. root@mydomain.com php_recv_mail@localhost, root@mydomain.com
  10.  
  11.  
  12. # vi /etc/virtual_redirect
  13.  
  14. root@mydomain.com php_recv_mail@localhost
  15.  
  16.  
  17. Rebuild the virtual alias db with postmap
  18.  
  19. postmap /etc/virtual_redirect
  20.  
  21.  
  22. # vi /etc/postfix/main.cf
  23.  
  24. virtual_alias_maps = blah blah, hash:/etc/virtual_redirect
  25. virtual_alias_domains = mydomain.com
  26.  
  27. # vi /etc/aliases
  28.  
  29. php_recv_mail: "|/usr/bin/php -q /etc/postfix/recv_mail.php"
  30.  
  31.  
  32. /********************************************************
  33.  
  34. needed to use /etc/postfix directory ... even setting same exact owner/perms as /etc/postfix to my public_html files/dirs didn't work...still need to fix
  35.  
  36. don't have the time now
  37.  
  38. ********************************************************/
  39.  
  40. chmod 755 /etc/postfix/recv_mail.php
  41. chmod 755 /etc/postfix/co2.php (db conn)
  42. chmod 777 /etc/postfix/
  43.  
  44. newaliases
  45. postfix reload
  46. service postfix restart
  47.  
  48. # MAKE SURE OPENDMARC DOESN'T BITCH ABOUT INGORING LOCALHOST CONNECTIONS
  49.  
  50. vi /etc/opendmarc.conf
  51.  
  52. uncomment
  53.  
  54. IgnoreHosts /etc/opendmarc/ignore.hosts
  55.  
  56. and create and empty ignore.hosts file (If not specified, defaults to "127.0.0.1" only.)
  57.  
  58. # vi /etc/opendmarc/ignore.hosts
  59.  
  60. # RESTART OPENDMARC
  61.  
  62. # service opendmarc restart
  63.  
  64. if it fails
  65.  
  66. # ps aux | grep opendmarc
  67.  
  68. # kill -9 pid
  69.  
  70. # service opendmarc start
  71.  
  72. ######### send a test email ################
  73.  
  74. check for errors
  75.  
  76. cat /var/log/maillog | grep localhost
  77.  
  78. // success
  79.  
  80. postfix/local[5810]: 2A39B40B6B: to=<php_recv_mail@localhost.mydomain.com>, orig_to=<root@mydomain.com>, relay=local, delay=1.1, delays=0.82/0.01/0/0.25, dsn=2.0.0, status=sent (delivered to command: /usr/bin/php -q /etc/postfix/recv_mail.php)
  81.  
  82. // messed up (perm errors i still need to fix)
  83.  
  84. postfix/local[32184]: 7EEDF40BDB: to=<php_recv_mail@localhost.mydomain.com>, orig_to=<root@mydomain.com>, relay=local, delay=0.36, delays=0.32/0.01/0/0.03, dsn=4.3.0, status=SOFTBOUNCE (Command died with status 1: "/usr/bin/php -q /var/zpanel/hostdata/zadmin/public_html/mydomain_com/pipe/recv_mail.php". Command output: Could not open input file: /var/zpanel/hostdata/zadmin/public_html/mydomain_com/pipe/recv_mail.php
  85.  
  86.  
  87. ######### recv_mail.php ################
  88.  
  89.  
  90. #!/usr/bin/php
  91. <?php
  92.  
  93. // fetch data from stdin
  94. $incoming_mail = file_get_contents("php://stdin");
  95.  
  96. require_once('/etc/postfix/co2.php');
  97.  
  98. //$incoming_mail = trim(fgets(STDIN));
  99.  
  100. /*
  101. $fd = fopen("php://stdin", "r");
  102. $incoming_mail = "";
  103. while (!feof($fd)) {
  104. $incoming_mail .= fread($fd, 1024);
  105. }
  106. fclose($fd);
  107.  
  108. */
  109.  
  110. $send_domain = "mydomain.com";
  111.  
  112. $myFile = "/etc/postfix/incoming_mails.txt";
  113. $fh = fopen($myFile, 'a') or die("can't open file");
  114. fwrite($fh, $incoming_mail."\r\n\r\n\r\n");
  115. fclose($fh);
  116.  
  117.  
  118. if(preg_match_all('/(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))+/i', $incoming_mail, $matches)) {
  119.  
  120. for($z = 0; $z < count($matches[0]); $z++) {
  121.  
  122. echo "[".$z."] : ".$matches[0][$z]."\r\n";
  123.  
  124. $email = trim($matches[0][$z]);
  125.  
  126. if(stripos($email, $send_domain) !== false) {
  127.  
  128. continue;
  129.  
  130. }
  131.  
  132. $result = mysql_query("SELECT emails.id FROM `emails` WHERE emails.email='$email' ORDER BY emails.id ASC");
  133.  
  134. $num = mysql_num_rows($result);
  135.  
  136. if($num > 0) {
  137.  
  138. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  139.  
  140. $id = $row['id'];
  141.  
  142. $result2 = mysql_query("UPDATE emails SET emails.isvalid='3' WHERE emails.id='$id'");
  143.  
  144. }
  145. }
  146. }
  147. }
  148.  
  149. mysql_close();
  150.  
  151. exit();
  152.  
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement