Posts tagged ‘maillog’

使用php发送邮件很简单,实际利用的是linux自带的smtp服务。这里不加赘述。

邮件服务,我的理解是数据包与包的交互。发送邮件时,smtp服务接收client程序的邮件标题、正文、from、to、优先级等信息,拼装为smtp数据包,网络发送给to地址所在的邮件服务器。接收邮件时,smtp服务从网络上的邮件服务器接收数据包,解析出to的email地址,利用推或拉的方式传递给client。

php中的mail函数,作为一种client形式,在其help里写的很清楚:

reject note 返回值

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

即mail的返回值仅代表php.ini里配置的smtp server是否接收发送请求成功,而并不代表最终的to email接收成功。

进而推测,smtp对mail请求应当是异步的,那么也许smtp的log中会有邮件接收的真正状态。进入centos系统的/var/log,果然有maillog!

Feb  7 09:21:45 v080106 sendmail[32612]: q171LiwL032612: to=abc@sohu.com, ctladdr=admin (500/500), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=32760, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as AC58F1060001)

这是一条发送成功的邮件记录。各字段意义为:

Feb  7 09:21:45 发送时间
v080106 发送者机器名称
sendmail[32612]: 进程号
q171LiwL032612: 唯一Message-ID,可以根据该字段,找到log里与本次发送相关的所有记录
to=abc@sohu.com, 接收者邮箱
ctladdr=admin (500/500)
delay=00:00:01, xdelay=00:00:01, 延时
mailer=relay, 邮件服务器,是relay,则参考下面的relay字段可知具体信息
pri=32760, 优先级
relay=[127.0.0.1] [127.0.0.1], 中继
dsn=2.0.0, smtp code (200表示成功)
stat=Sent (Ok: queued as AC58F1060001) 状态说明,Sent代表发送成功

Feb  8 01:38:48 v080106 postfix/smtpd[11264]: F1E521060007: client=localhost.localdomain[127.0.0.1]

Feb  8 01:38:49 v080106 postfix/cleanup[11267]: F1E521060007: message-id=<201202071738.q17HcmEB011273@v080106.isv>

Feb  8 01:38:49 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 01:38:49 v080106 sendmail[11273]: q17HcmEB011273: to=abc@vip.sohu.com, ctladdr=admin (500/500), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=32807, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as F1E521060007)

Feb  8 01:38:49 v080106 postfix/smtp[11270]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=0.25, delays=0.05/0/0.15/0.05, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 02:04:59 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 02:05:00 v080106 postfix/smtp[22089]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=1572, delays=1571/0.85/0.13/0.04, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 02:38:26 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 02:38:26 v080106 postfix/smtp[28707]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=3577, delays=3577/0.01/0.14/0.05, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 03:45:54 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 03:45:54 v080106 postfix/smtp[10772]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=7625, delays=7625/0.03/0.08/0.05, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 05:08:20 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 05:08:30 v080106 postfix/smtp[19312]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=12581, delays=12572/9.5/0.11/0.04, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 06:31:47 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 06:31:47 v080106 postfix/smtp[21697]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=17578, delays=17578/0/0.08/0.05, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 07:55:00 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 07:55:00 v080106 postfix/smtp[22783]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=22572, delays=22571/0.02/0.11/0.37, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 09:01:41 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 09:01:41 v080106 postfix/smtp[23869]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=26572, delays=26572/0/0/0, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

Feb  8 10:08:21 v080106 postfix/qmgr[18249]: F1E521060007: from=<admin@v080106.isv>, size=3258, nrcpt=1 (queue active)

Feb  8 10:08:21 v080106 postfix/smtp[24922]: F1E521060007: to=<abc@vip.sohu.com>, relay=mx.vip.sohu.com[61.135.132.86]:25, delay=30572, delays=30572/0/0/0, dsn=4.1.8, status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))

以上是一组失败的记录,唯一号是F1E521060007。可以看到,当接收到失败之后,sendmail进行了多次重试。里面还有一些疑问,之后再解决,现在主要关心错误信息:

relay=mx.vip.sohu.com[61.135.132.86]:25, 中继
dsn=4.1.8, # 错误码
status=deferred (host mx.vip.sohu.com[61.135.132.86] said: 450 4.1.8 <admin@v080106.isv>: Sender address rejected: Domain not found (in reply to RCPT TO command))  # 具体描述,延迟发送,mx.vip.sohu.com认为无法找到发送者的domain

还有一类统计数据:

Feb  8 10:50:21 v080106 postfix/scache[25440]: statistics: start interval Feb  8 10:41:42

Feb  8 10:50:21 v080106 postfix/scache[25440]: statistics: domain lookup hits=1692 miss=209 success=89%

Feb  8 10:50:21 v080106 postfix/scache[25440]: statistics: address lookup hits=947 miss=9428 success=9%

Feb  8 10:50:21 v080106 postfix/scache[25440]: statistics: max simultaneous domains=4 addresses=9 connection=30

有了以上这些信息,就可以准备的跟踪真实的邮件到达率了。当然,是否被认为是垃圾邮件之类,是无法从这里看出来的。

———————————————————

以gmail为例,可以点击邮件右上方的more按钮->show original查看到邮件头信息:

Delivered-To: abc@gmail.com
Received: by 10.182.176.71 with SMTP id cg7cs142014obc;
        Tue, 7 Feb 2012 15:44:47 -0800 (PST)
Received: by 10.68.218.167 with SMTP id ph7mr39734334pbc.110.1328658286305;
        Tue, 07 Feb 2012 15:44:46 -0800 (PST)
Return-Path: <201202072344452bac1460e73a42f2b86762ab94c6272d-C29QW58DB99VUH@bounces.amazon.com>
Received: from smtp-out-184-237.amazon.com (smtp-out-184-237.amazon.com. [207.171.184.237])
        by mx.google.com with ESMTP id e6si262857pbj.138.2012.02.07.15.44.45;
        Tue, 07 Feb 2012 15:44:46 -0800 (PST)
Received-SPF: pass (google.com: domain of 201202072344452bac1460e73a42f2b86762ab94c6272d-C29QW58DB99VUH@bounces.amazon.com designates 207.171.184.237 as permitted sender) client-ip=207.171.184.237;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of 201202072344452bac1460e73a42f2b86762ab94c6272d-C29QW58DB99VUH@bounces.amazon.com designates 207.171.184.237 as permitted sender) smtp.mail=201202072344452bac1460e73a42f2b86762ab94c6272d-C29QW58DB99VUH@bounces.amazon.com
Date: Tue, 7 Feb 2012 23:44:45 +0000 (UTC)
From: "Amazon.cn" <store-news@amazon.cn>
To: "abc@gmail.com" <abc@gmail.com>
Message-ID: <677952103.17204211328658285803.JavaMail.em-build@massmail-sender-fe-9004.sea19.amazon.com>

SPF的状态我看到的有pass(可信)、neutral(中性),有些垃圾邮件的状态也是pass的。
大批量发送广告邮件时,目前收集到的需要注意:

  1. 控制发送频率,10个以上ip并行发送,一天控制在几十万的数量
  2. 更真实的header信息,通过配置发件域名等信息
  3. 如果可能,同邮件服务商沟通,争取进入白名单

邮件域名配置:

  1. 主机A记录abc.com
  2. 主机A记录mail.abc.com
  3. 主机A记录www.abc.com
  4. 主机A记录ftp.abc.com
  5. abc.com 到 mail.abc.com的MX记录

另外还有反向解析相关的配置。比如我们的发件地址是news@mail.flykobe.com,则需要在邮件服务器里将HELO地址配成mail.flykobe.com,并且按照上面的方法配置好各种相关域名到固定ip。同时,还需要将ip到mail.flykobe.com做一个映射(这个需要联系DNS提供商)。我们既无固定ip,就无法实现这个了!可以参考:http://hi.baidu.com/yangming/blog/item/323d034ff87a103fafc3ab8a.html

————————————————————-

记录一下利用postfix,即开源的邮件传输代理,搭建邮件服务器的过程。

网上资料可见,需要以下几个套件:

  • SMTP 認證套件: cyrus-sasl ( cyrus-sasl-1.5.27-5mdk 以及其他認證機制函式庫 )
  • Postfix 郵件伺服器: postfix ( postfix-1.1.11-4mdk )
  • POP3 伺服器: imap ( imap-2001a-9mdk, imap-devel-2001a-9mdk 兩個 ) # 最后没有用这个
  • 郵件分析軟體:procmail ( procmail-3.22-3mdk )