简体   繁体   English

从Linux命令行阅读邮件附件

[英]Read the mail attachment from Linux command line

Is it possible to read the emails based on the Subject line and then get the base64 attachment or directly get the attachment ? 是否可以根据主题行阅读电子邮件,然后获取base64附件或直接获取附件? Server : Linux System 服务器:Linux系统

Your question seems to presuppose that there is a single attachment and that it can be reliably extracted. 您的问题似乎以一个附件为前提,并且可以可靠地提取它。 In the general case, an email message can have a basically infinite amount of attachments, and the encoding could be one out of several. 在一般情况下,电子邮件中基本上可以有无限数量的附件,并且编码可以是几分之一。

But if we assume that you are dealing with a single sender which consistently uses a static message template where the first base64 attachment is always going to be the one you want, something like 但是,如果我们假设您要处理的单个发件人始终使用静态消息模板,则第一个base64附件始终将是您想要的附件,例如

case $(formail -zcxSubject: <"$message") in
    "Hello, here is your report for "*)
        awk 'BEGIN { h=1 }
            h { if ($0 ~ /^$/) h=0 ; next }  # skip headers
            /^Content-Disposition: attachment/ { a=1 }  # find att
            a && /^$/ { p=1; next }
            p && /^$/ { exit }
            p' "$message" |
        base64 -d ;;
esac

This will extract the Subject: header and compare it to a glob pattern. 这将提取Subject:标头并将其与glob模式进行比较。 I expect this is what you mean by "based on subject" -- if we find a matching subject header, examine this message, otherwise discard. 我希望这就是“基于主题”的意思-如果我们找到匹配的主题标题,请检查此消息,否则将其丢弃。

The crude Awk script attempts to isolate the base64 data and pass it to base64 -d for extraction. 原始的Awk脚本尝试隔离base64数据,并将其传递给base64 -d进行提取。 This contains a number of pesky and somewhat crude assumptions about the message format, and probably requires significant additional tweaking. 这包含许多有关消息格式的令人讨厌的粗略假设,并且可能需要进行大量其他调整。 Briefly, we skip the headers, then look for MIME headers identifying an attachment, and print that, skipping everything else in the message. 简要地说,我们跳过标题,然后查找标识附件的MIME标题,然后打印该标题,并跳过消息中的所有其他内容。 If this header is missing, or identifies the wrong MIME part, you will get no results, or (worse) incorrect results. 如果缺少此标头,或标识了错误的MIME部分,则不会获得任何结果,或者(更糟糕的)不正确的结果。 Also, the /^Content-Disposition:/ regex could theoretically match on a line which is not a MIME header, though this seems highly unlikely (but might actually happen if you are looking eg at a bounce message). 同样, /^Content-Disposition:/正则表达式理论上可以在不是MIME标头的行上进行匹配,尽管这似乎不太可能(但实际上可能发生在您查看退信等情况下)。

A more robust approach would involve a MIME extraction tool or perhaps a custom script to actually parse the MIME structure and extract the part you want. 一种更可靠的方法是使用MIME提取工具或自定义脚本来实际解析MIME结构并提取所需的部分。 Without details about what exactly you need, I'm not able to provide that. 没有有关您真正需要什么的详细信息,我无法提供。 (This would also allow you to use the sender's specified filename; the above script simply prints the decoded payload to standard output.) (这还将允许您使用发送者的指定文件名;上面的脚本只是将解码后的有效负载打印到标准输出。)

Note also that formail has no idea about RFC2047 encoding, so if the subject is not plain ASCII, you have to specify the encoded form in the script. 还要注意, formail并不了解RFC2047编码,因此,如果主题不是纯ASCII,则必须在脚本中指定编码格式。

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

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