通过bash从文件检索电子邮件主题 [英] Retrieve email subject from file via bash

查看:170
本文介绍了通过bash从文件检索电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本,将文件从服务器邮件文件夹下载到NAS设备,以便客户端具有本地备份的副本。



文件是保存为 11469448248.H15587P19346.smtp.x14.eu_2,S 文件。
我已将扩展名更改为标准的.eml格式,因此电子邮件客户端可以从光盘读取它们。

  for f in * .smtp.x14.eu_2,S;做
#sed9q; d$ f
#tail -n + 9 $ f | head -n1
mv - $ f$ {f%.smtp.x14.eu_2,S} .eml;
done

正如你所看到的,我试图使用 sed 尾部命令从文件中获取第9行;问题是,主题并不总是在第9行,文件名没有说明其内容。



我正在尝试获取文件名以一种可以理解的格式,所以我认为这个主题可能是有帮助的。



电子邮件文件的第n行是以主题:PD开头的行:主题



我试图找到这条线,删除主题:PD:,并将其余的作为ne文件名

解决方案

以下是错误,但实现了你似乎在问的问题。

 code> subj = $(sed -n'/ ^主题:PD * //!d; p; q; / ^ $ / q'$ f)

这个问题是它成功的琐碎的情况,但是当你有一个MIME RFC2047编码的主题:标题,而($)$($)

标题:我会用稍微更现代的编程语言来处理这个问题。这不是一个单行的,但是Python很简单。

  subj = $(./ emailsubj.py$ f)

其中 emailsubj.py 包含一些或多或少像

 #!/ usr / bin / env python 
from email.parser import Parser
from email.header import header,decode_header
from sys import argv

for argv [1:]:
with open(filename,'rb')as handle #处理文件未找到等等?
message = Parser()。parse(handle)
try:
subj =''.join([frag.decode(enc)if enc else frag
for frag,enc在decode_header(message ['subject'])])
除了HeaderParseError,UnicodeDecodeError:
subj = message ['subject']#可能会警告关于错误?
print(subj)

(记住, chmod + x emailsubj .py ,显然。)


I've a shell script which downloads files from the servers mail folder to a NAS devices so the client has copies made locally with a backup.

The files are saved as 11469448248.H15587P19346.smtp.x14.eu_2,S files. I've changed the extension to standard .eml format so email clients can read them from disc.

for f in *.smtp.x14.eu_2,S; do
#sed "9q;d" $f
#tail -n+9 $f | head -n1
mv -- "$f" "${f%.smtp.x14.eu_2,S}.eml";
done

As you can see I've tried to use the sed and tail command to get the 9th line from the file; problem is that the subject isn't always on the 9th line and the file names don't say much of its content.

I'm trying to get the files names to be in a understandable format, so I figured the subject could be helpful.

On the nth line of the email file is a line that begins with Subject: PD: the subject

Im trying to find this line fet rid of Subject: PD: and leave the rest as the ne file name

解决方案

The following is wrong but implements what you seem to be asking.

subj=$(sed -n '/^Subject: PD *//!d;p;q;/^$/q' "$f")

The problem with this is that it succeeds in the trivial case, but fails when you have a MIME RFC2047-encoded Subject: header, and (more trivially) when the Subject: header spans more than a single line.

I would approach this with a slightly more modern programming language. It's not quite a one-liner, but it's easy enough with Python.

subj=$(./emailsubj.py "$f")

where emailsubj.py contains something more or less like

#!/usr/bin/env python
from email.parser import Parser
from email.header import Header, decode_header
from sys import argv

for filename in argv[1:]:
    with open(filename, 'rb') as handle:  # handle file not found etc?
        message = Parser().parse(handle)
    try:
        subj = ''.join([frag.decode(enc) if enc else frag
            for frag, enc in decode_header(message['subject'])])
    except HeaderParseError, UnicodeDecodeError:
        subj = message['subject']   # maybe warn about error?
    print(subj)

(Remember to chmod +x emailsubj.py, obviously.)

这篇关于通过bash从文件检索电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆