php - Postfix : Bounce checking. 电子邮件通过管道传输到 php,但阻止电子邮件正确发送和接收

标签 php email postfix-mta email-bounces

我被告知要在 stackoverflow 中重新发布此内容,因为它实际上可能不是后缀问题。但是,我对 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 需要是现有的本地用户: [email protected] root:) 整个步骤已被删除

  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-n n-10 管道 flags=q user=bounce argv=/usr/local/bouncehandler/mybh.php ${发送者} ${接收者}

  10. /etc/postfix/master.cf 中的更改:

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

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

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

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

    如果不存在则创建表bounce_list ( 电子邮件 VARCHAR(255) NOT NULL PRIMARY KEY, ounce_count INT(4) NOT NULL )ENGINE=InnoDB;

  2. 重新启动 Postfix:

$sudo postfix 重新加载

我传递的脚本检查收件人是否是我的域,我相信这表明它已发送给我。然后,如果是,我会检查它是否最初发送给其他人,如果是,我会检查用户并将电子邮件计为已退回。我没有在脚本中做任何其他事情,所以我不确定之后我是否应该调用原始邮件脚本(如果有的话)。

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;
?>

主.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}

邮件日志:

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

Apr 16 05:55:37 serverName postfix/cleanup[1789]: 48F1F2C0663: message-id=<[email protected]>

Apr 16 05:55:37 serverName postfix/qmgr[1773]: 48F1F2C0663: from=, size=294, nrcpt=1 (queue active)

Apr 16 05:55:58 serverName postfix/smtp[1791]: 48F1F2C0663: to=<[email protected]>, relay=mail.digitalsanctuary.com[174.73.49.123]:52, delay=40, delays=19/0.01/0.42/20, dsn=5.1.1, status=bounced (host mail.digitalsanctuary.com[174.37.94.132] said: 550 5.1.1 <[email protected]>: Recipient address rejected: User unknown in virtual alias table (in reply to RCPT TO command))

Apr 16 05:55:58 serverName postfix/cleanup[1789]: 3F13A2C0869: message-id=<[email protected]>

Apr 16 05:55:58 serverName postfix/bounce[1800]: 48F1F2C0663: sender non-delivery notification: 3F13A2C0869

Apr 16 05:55:58 serverName postfix/qmgr[1773]: 3F13A2C0869: from=<>, size=2513, nrcpt=1 (queue active)

Apr 16 05:55:58 serverName postfix/qmgr[1773]: 48F1F2C0663: removed

Apr 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 (delivered via mybh service)

Apr 16 05:55:58 serverName postfix/qmgr[1773]: 3F13A2C0869: removed

我已经包含了我认为可能有用的所有信息,这些信息来 self 在阅读许多指南和许多其他帖子时看到的必要信息,并试图自己解决这个问题。

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

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

谢谢

最佳答案

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

// 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);

关于php - Postfix : Bounce checking. 电子邮件通过管道传输到 php,但阻止电子邮件正确发送和接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979379/

相关文章:

php - fatal error : Uncaught ArgumentCountError: Too few arguments to function

php - .htaccess 子域参数在查询字符串中不可访问

email - 设置电子邮件标题,以便退回的电子邮件转到特定地址

php - PHP Mail 函数中链接的奇怪行为

java - 使用 Java Mail API 从 Gmail 导出所有电子邮件

mysql - 在数据库中保存后缀电子邮件副本

linux - Linux 上的 Coldfusion - 发送邮件的 Postfix - 在 Coldfusion Administrator 中放什么?

php - 如何使用 php 建立下一个和上一个链接?

linux - 如何获取或捕获由 postfix 发送的电子邮件正文和标题

javascript - 如何通过一键删除按钮删除选中的项目