繁体   English   中英

后缀:弹跳检查。 电子邮件通过管道传输到php,但无法正确发送和接收电子邮件

[英]Postfix: Bounce checking. Emails are piped to php but prevents emails from being sent and received properly

有人告诉我在stackoverflow中将其重新发布在这里,因为它实际上可能不是postfix问题。 但是,我对php postfix交互性了解得不够多,因此,如果脚本部分有问题,并且有人可以看到它,请告诉我。 我不确定是否有一种特殊的方法可以使邮件在通过脚本后正常(传入或传出)正常传递(即使是这样)。 谢谢!

-

在工作时,我做了一份工作指南,以备将来使用。 我一直在努力使它起作用一段时间,并且我已经收到了通过脚本传递的电子邮件。 接收到所有脚本并运行完脚本后,所有收到的邮件(无论是退回还是其他)的问题都不会放入邮箱中。 该脚本确实可以运行并在其中执行所有操作,然后退出而不会出现错误,请补充一下。

  1. 添加目录:$ sudo mkdir / usr / local / bouncehandler

  2. 将脚本文件添加到/ usr / local / bouncehandler:mybh.php

  3. 允许执行脚本:chmod a + x mybh.php

  4. 添加用户:$ sudo useradd退回

  5. (创建/ etc / postfix / virtual_aliases来添加一个全包别名-localuser必须是现有的本地用户:bounces@bounces.mydomain.com根目录:) 删除了此步骤

  6. 创建/ etc / postfix / transport以添加传输映射。 “ mytransportname”可以是您想要的任何名称; 在master.cf中使用以下代码:mydomain.com mybh:

  7. 接下来,需要将transport和virtual_aliases都编译为db文件:($ sudo postmap / etc / postfix / virtual_aliases) 删除 $ sudo postmap / etc / postfix / transport

  8. /etc/postfix/master.cf中的更改:smtp inet n----smtpd(-o content_filter = mybh:dummy)已删除

  9. 将传输添加到/etc/postfix/master.cf:mybh Unix-nn-10个管道标志= q user = bounce argv = / usr / local / bouncehandler / mybh.php $ {sender} $ {recipient}

  10. 在/etc/postfix/master.cf中进行更改:

    拾音器fi-n-60 1拾音器(-o content_filter = mybh:dummy)已删除

  11. 在/etc/postfix/main.cf中:

删除 transport_maps =哈希:/ etc / postfix / transport(virtual_alias_maps =哈希:/ etc / postfix / virtual_aliases))

  1. 连接到数据库并创建表bounce_list:

    如果不存在则创建表bounce_list(email VARCHAR(255)NOT NULL PRIMARY KEY,bounce_count INT(4)NOT NULL)ENGINE = InnoDB;

  2. 重新启动后缀:

$ sudo后缀重新加载

我通过它传递的脚本会检查收件人是否是我的域,我相信这将表明它已发送给我。 然后,如果是,我检查它是否最初发送给其他人,如果是,则检查用户,并将电子邮件计数为退回。 我没有在脚本中做任何其他事情,因此我不确定在那之后是否应该对存在的原始邮件脚本进行一些调用。

mybh.php:

#!/usr/bin/php -q
<?php

////////////////////////////////////////////////////////
//Collects sender and recipient data from email pass
////////////////////////////////////////////////////////
$sender = trim($argv[1]);
$recipient = trim($argv[2]);


$bounceProcd = FALSE;

list($name, $domain) = explode('@', $recipient);


if(strpos($recipient, 'mydomain.com') !== false)
{

    ////////////////////////////////////////////////////////
    //Database variable initialization
    ////////////////////////////////////////////////////////
    $host = "localhost";
    $user = "user";
    $pass = "password";
    $db = "database";

    ////////////////////////////////////////////////////////
    //Establish database connection
    ////////////////////////////////////////////////////////
    $con = mysqli_connect($host, $user, $pass, $db);

    ////////////////////////////////////////////////////////
    //Verify that database is connected properly
    ////////////////////////////////////////////////////////
    if(!$con)
    {
        exit(75);
    }

    ////////////////////////////////////////////////////////
    //Initialize query into variable
    ////////////////////////////////////////////////////////
    $query = "INSERT INTO bounce_list VALUES ('$recipient', 1) ON DUPLICATE KEY UPDATE bounce_count = bounce_count + 1";

    ////////////////////////////////////////////////////////
    //Run query and store in variable
    ////////////////////////////////////////////////////////
    $result = mysqli_query($con, $query);

    $bounceProcd = mysqli_affected_rows($con) > 0;

    ////////////////////////////////////////////////////////
    //Verify that query executed
    ////////////////////////////////////////////////////////
    if (!$result) {

        $con->close();

        exit(75);
    }


    $con->close();

    $dataLen = IgnoreMessageData();
}


$exitStatus = (TRUE == $bounceProcd) ? 0 : 75;

////////////////////////////////////////////////////////
//Pass email to mailbox
////////////////////////////////////////////////////////
exit($exitStatus+0);


function IgnoreMessageData()
{
    $msgLen = 0;
    $fd = fopen('php://stdin', 'r');
    while (FALSE === feof($fd))
    {
        $dunsel = fread($fd, 1024);
        $msgLen += strlen($dunsel);
    }
    fclose($fd);
    return $msgLen;
}
return;
?>

main.cf:

# See /usr/share/postfix/main.cf.dist for a commented, more complete
version
# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)

biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl­cert­snakeoil.pem

smtpd_tls_key_file=/etc/ssl/private/ssl­cert­snakeoil.key

smtpd_use_tls=yes

smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache

smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix­doc package for
# information on enabling SSL in the smtp client.

myhostname = main.mydomain.com

alias_maps = hash:/etc/aliases

alias_database = hash:/etc/aliases

smtp_generic_maps = hash:/etc/postfix/generic

myorigin = /etc/mailname

mydestination = admin.mydomain.com, main.mydomain.com,  
localhost.mydomain.com, localhost

relayhost =

mynetworks = (deleted this line)

mailbox_size_limit = 0

recipient_delimiter = +

inet_interfaces = all

transport_maps = hash:/etc/postfix/transport

master.cf:

#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd

mybh      unix  -       n       n       -       -       pipe
    flags=q user=bounce argv=/usr/local/bouncehandler/mybh.php ${sender} ${recipient}

#smtp      inet  n       -       -       -       1       postscreen
#smtpd     pass  -       -       -       -       -       smtpd
#dnsblog   unix  -       -       -       -       0       dnsblog
#tlsproxy  unix  -       -       -       -       0       tlsproxy
#submission inet n       -       -       -       -       smtpd
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#smtps     inet  n       -       -       -       -       smtpd
#  -o syslog_name=postfix/smtps
#  -o smtpd_tls_wrappermode=yes
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#628       inet  n       -       -       -       -       qmqpd

pickup    fifo  n       -       -       60      1       pickup

cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
#qmgr     fifo  n       -       n       300     1       oqmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# Recent Cyrus versions can use the existing "lmtp" master.cf entry.
#
# Specify in cyrus.conf:
#   lmtp    cmd="lmtpd -a" listen="localhost:lmtp" proto=tcp4
#
# Specify in main.cf one or more of the following:
#  mailbox_transport = lmtp:inet:localhost
#  virtual_transport = lmtp:inet:localhost
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus     unix  -       n       n       -       -       pipe
#  user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
# Old example of delivery via Cyrus.
#
#old-cyrus unix  -       n       n       -       -       pipe
#  flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -   n   n   -   2   pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}

mail.log:

4月16日05:55:37 serverName postfix / pickup [1774]:48F1F2C0663:uid = 0 from =

4月16日05:55:37 serverName后缀/清除[1789]:48F1F2C0663:message-id = <20140416055537.48F1F2C0663@serverName.mydomain.com>

4月16日05:55:37 serverName postfix / qmgr [1773]:48F1F2C0663:from =,size = 294,nrcpt = 1(队列处于活动状态)

4月16日05:55:58 serverName postfix / smtp [1791]:48F1F2C0663:to = <12356fdgjn56y23refsdv2ecwsdf21dfsdf@drdrb.net>,relay = mail.digitalsanctuary.com [174.73.49.123]:52,delay = 40,delays = 19 / 0.01 / 0.42 / 20,dsn = 5.1.1,状态=退回(主机mail.digitalsanctuary.com [174.37.94.132]说:550 5.1.1 <12356fdgjn56y23refsdv2ecwsdf21dfsdf@drdrb.net>:收件人地址被拒绝:虚拟别名中的用户未知表(回复RCPT TO命令)

4月16日05:55:58 serverName postfix / cleanup [1789]:3F13A2C0869:message-id = <20140614055558.3F13A2C0869@serverName.mydomain.com>

4月16日05:55:58 serverName后缀/退回[1800]:48F1F2C0663:发件人未送达通知:3F13A2C0869

4月16日05:55:58 serverName postfix / qmgr [1773]:3F13A2C0869:from = <>,size = 2513,nrcpt = 1(队列处于活动状态)

4月16日05:55:58 serverName postfix / qmgr [1773]:48F1F2C0663:已删除

4月16日05:55:58 serverName postfix / pipe [1801]:3F13A2C0869:to =,relay = mybh,delay = 0.04,delays = 0/0/0 / 0.03,dsn = 2.0.0,status = sent(通过mybh服务)

4月16日05:55:58 serverName postfix / qmgr [1773]:3F13A2C0869:已删除

我提供了所有我认为可能有用的信息,这些信息是我阅读许多指南和许多其他帖子并尝试自己弄清楚的必要信息。

如果有一双新鲜的眼睛可以帮助我,我将不胜感激。

  • 我也想知道,如果有人知道如何执行此操作,请将原始目标电子邮件附加到退回的电子邮件中。

谢谢

因此,当您添加过滤器时,基本上是在说我会从这里处理事情,因此,为了仍然收到退回邮件,您需要将邮件发送给原始收件人。

// original bounce handling code

// Now resend the bounced message
// get message from stdin
$fp = fopen("php://stdin", "r");

$message= '';
while (! feof($fp)) {
    $message.= fgets($fp);
}

// send to original recipient, lazy but easiest just to pipe the message in to sendmail
shell_exec('echo ' . escapeshellarg($message) . ' | /usr/sbin/sendmail -G -i ' . $recipient);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM