修改postfix2.9-20111113版本的smtp发送模块在发送前输出发件人

来源:互联网 发布:致远移动协同软件 编辑:程序博客网 时间:2024/06/14 02:55

基于postfix的转发机制是异步的,而不是跟smtp收信直接反馈是否发送成功.而是要先放入队列后再转发,接着对于发送情况那么就只能看日志了.而它的日志却是每行一句.所以.很难从一句日志中找到它与信的关联:这个发送状态(发给xx@yy.com的信成功与否)的日志行跟哪封信是相关的?因为我们需要统计每封信群发情况.

 

看了一下源码后.感觉想把发件人(mail from)放到每行日志中还是比较麻烦的.但是在发送之前把这个信息输出来做一行日志.接着因为它的日志应该是有顺序,先输出sender(smtp 连接远程mail之前的mail from),接后面接下来的日志肯定是smtp交互后的成功与否的日志了.但是可能会出现与我考虑意外情况,那就是多个smtp出现时.对于这种状态解决方法是也把queue_id输出就可以了.

 

发送前的信的信息可用信息如下:

 


typedef struct DELIVER_REQUEST {
    VSTREAM *fp;            /* stream, shared lock */
    int     flags;            /* see below */
    char   *queue_name;            /* message queue name */
    char   *queue_id;            /* message queue id */
    long    data_offset;        /* offset to message */
    long    data_size;            /* message size */
    char   *nexthop;            /* next hop name */
    char   *encoding;            /* content encoding */
    char   *sender;            /* envelope sender */
    MSG_STATS msg_stats;        /* time profile */
    RECIPIENT_LIST rcpt_list;        /* envelope recipients */
    DSN    *hop_status;            /* DSN status */
    char   *client_name;        /* client hostname */
    char   *client_addr;        /* client address */
    char   *client_port;        /* client port */
    char   *client_proto;        /* client protocol */
    char   *client_helo;        /* helo parameter */
    char   *sasl_method;        /* SASL method */
    char   *sasl_username;        /* SASL user name */
    char   *sasl_sender;        /* SASL sender */
    char   *log_ident;            /* original queue ID */
    char   *rewrite_context;        /* address rewrite context */
    char   *dsn_envid;            /* DSN envelope ID */
    int     dsn_ret;            /* DSN full/header notification */
} DELIVER_REQUEST;

 

输出这句日志点在.src/smtp/smtp.c 905行之前就可以了.

static int deliver_message(const char *service, DELIVER_REQUEST *request)
{
    SMTP_STATE *state;
    int     result;

    if (msg_verbose)//把这个判断去掉也就ok了.或是再加上其它信息输出
    msg_info("deliver_message: from %s", request->sender);

    /*
     * Sanity checks. The smtp server is unprivileged and chrooted, so we can
     * afford to distribute the data censoring code, instead of having it all
     * in one place.
     */
    if (request->nexthop[0] == 0)
    msg_fatal("empty nexthop hostname");
    if (request->rcpt_list.len <= 0)
    msg_fatal("recipient count: %d", request->rcpt_list.len);

    /*
     * Initialize. Bundle all information about the delivery request, so that
     * we can produce understandable diagnostics when something goes wrong
     * many levels below. The alternative would be to make everything global.
     */
    state = smtp_state_alloc();
    state->request = request;
    state->src = request->fp;
    state->service = service;
    state->misc_flags |= smtp_addr_pref;
    SMTP_RCPT_INIT(state);

    /*
     * Establish an SMTP session and deliver this message to all requested
     * recipients. At the end, notify the postmaster of any protocol errors.
     * Optionally deliver mail locally when this machine is the best mail
     * exchanger.
     */
    result = smtp_connect(state);

 

 

修改后的日志

 


root@chrd-edm:/home/chrd/mail# cat /var/log/mail*
Nov 20 23:31:26 chrd-edm postfix/postfix-script[4634]: starting the Postfix mail system
Nov 20 23:31:26 chrd-edm postfix/master[4635]: daemon started -- version 2.9-20111113, configuration /etc/postfix
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: connect from localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: 6A900100730: client=localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/cleanup[4641]: 6A900100730: message-id=<4ec91d5765453@myhrd.cn>
Nov 20 23:31:35 chrd-edm postfix/qmgr[4637]: 6A900100730: from=<servicde@myhrd.cn>, size=1147, nrcpt=1 (queue active)
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: disconnect from localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/smtp[4642]: deliver_message:qidizi debug from servicde@myhrd.cn
Nov 20 23:31:36 chrd-edm postfix/smtp[4642]: 6A900100730: to=<qidizi@qq.com>, relay=mx3.qq.com[112.90.142.56]:25, delay=0.87, delays=0.09/0.01/0.27/0.5, dsn=2.0.0, status=sent (250 Ok: queued as )
Nov 20 23:31:36 chrd-edm postfix/qmgr[4637]: 6A900100730: removed
Nov 20 23:31:26 chrd-edm postfix/postfix-script[4634]: starting the Postfix mail system
Nov 20 23:31:26 chrd-edm postfix/master[4635]: daemon started -- version 2.9-20111113, configuration /etc/postfix
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: connect from localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: 6A900100730: client=localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/cleanup[4641]: 6A900100730: message-id=<4ec91d5765453@myhrd.cn>
Nov 20 23:31:35 chrd-edm postfix/qmgr[4637]: 6A900100730: from=<servicde@myhrd.cn>, size=1147, nrcpt=1 (queue active)
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: disconnect from localhost[127.0.0.1]
Nov 20 23:31:35 chrd-edm postfix/smtp[4642]: deliver_message:qidizi debug from servicde@myhrd.cn//自己的日志输出点.下面行就是发送状态
Nov 20 23:31:36 chrd-edm postfix/smtp[4642]: 6A900100730: to=<qidizi@qq.com>, relay=mx3.qq.com[112.90.142.56]:25, delay=0.87, delays=0.09/0.01/0.27/0.5, dsn=2.0.0, status=sent (250 Ok: queued as )
Nov 20 23:31:36 chrd-edm postfix/qmgr[4637]: 6A900100730: removed
Nov 20 23:31:36  6A900100730: to=<qidizi@qq.com>, relay=mx3.qq.com[112.90.142.56]:25, delay=0.87, delays=0.09/0.01/0.27/0.5, dsn=2.0.0, status=sent (250 Ok: queued as )
Nov 20 23:31:35 chrd-edm postfix/smtpd[4638]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled

 

--

在  result = smtp_connect(state);之前增加下面

 /* debug by qidizi */
         msg_info("smtp.c:%s %s", request->queue_id,  request->sender);

 

使用自己编写的uninstall.sh进行清理已安装的文件.但保留配置文件/etc/postfix

root@chrd-edm:/home/chrd/mail# cat ./postfix-dell

 #!/bin/sh
postfix stop
 rm -rv /var/lib/postfix
 rm -rv /var/spool/postfix
 rm -rv /usr/lib/postfix
 rm -v /usr/sbin/postfix

rm -v /usr/sbin/postalias    
rm -v /usr/sbin/postcat    
rm -v /usr/sbin/postconf    
rm -v /usr/sbin/postsuper    
rm -v /usr/sbin/postqueue    
rm -v /usr/sbin/postmulti    
rm -v /usr/sbin/postmap    
rm -v /usr/sbin/postlog    
rm -v /usr/sbin/postlock    
rm -v /usr/sbin/postkick    
rm -v /usr/sbin/postdrop
rm -v /var/log/mail*
/etc/init.d/rsyslog restart

-------

清理上次编译

make tidy

 

------

编译只显示错误,其它编译正常提示输入到文件

make > make.txt

------

编译安装

make install

接着一路回车下去就行了.

 

----

启动postfix

postfix start

 

---

使用telnet测试发送

 

---

查看日志

cat /var/log/mail*

然后在日志中就能看到自己加的输出了.

 

原创粉丝点击