如何预处理我的电子邮件以包含附件链接

发布于 2024-12-04 03:35:35 字数 203 浏览 2 评论 0原文

我使用 mutt over ssh 作为我的邮件客户端。这很有效,但是查看附件(主要是图像)有点烦人。现在我的想法是解析电子邮件,将附件存储在我的服务器上,并创建一个用于访问附件的 URL,并将该 URL 放入电子邮件中。有人已经这样做了吗?实现这一目标的简单方法是什么?

我已经在使用 procmail 来过滤内容,我可以使用它在电子邮件中插入文本吗?

谢谢

I'm using mutt over ssh as my mail client. This works great, but looking at the attachments (mostly images) is a bit annoying. Now my idea is to parse the email, store the attachments on my server and create an URL for accessing the attachment and put this URL inside the email. Has anyone done this already? What is a simple way to achieve this?

I'm already using procmail for filtering stuff, can I use it to insert text into my email?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-12-11 03:35:35

当然可以。不过,没有对 MIME 的直接支持,因此您可能需要一些或多或少聪明的东西来确保您不会弄乱正在处理的消息的 MIME 结构。

作为一个简单的起始示例,假设您已提取 ~/public_html/prv/att000.jpg 的附件,并将 Web 服务器设置为在 http://localhost:8080 处提供该附件/~you/prv/att00.jpg,你可以添加这样的标题;

:0fhw
| formail -I"X-Tracted: http://localhost:8080/~you/prv/att00.jpg"

在消息本身中添加内联内容比较困难,但并非不可能。

编辑:您的脚本可以简化为不使用临时文件(顺便说一句,避免多个猫的无用用途)如果您从食谱中提取消息 ID:

# Whitespace in [square brackets] is tab, space
:0c
* ^Message-Id:[   ]*<\/[^@>]+
| (mkdir -p "webserver path"/"$MATCH"; munpack -C "webserver path"/"$MATCH" )

...但也许您想保留您的脚本(没有无用的猫)并修改它,以便它不仅提取附件,还创建修改后的消息在标准输出上,内嵌附件链接。

我可以建议三个选项,所有这些都有点麻烦。假设您传入的消息是 (multipart/mixed (text/plain (multipart/related text/html image/png image/png)));您可以

  1. 将您的消息包装在(多部分/相关(文本/纯文本:此处的链接列表)(消息/rfc822:此处的原始消息:(多部分/混合(文本/纯文本(多部分/相关文本/html图像) /png image/png))))。这根本不需要您解析原始消息,但在大多数客户端中查看起来很困难。

  2. 假设始终有一个文本/纯文本部分,并将链接列表附加到其末尾,这需要逻辑来查找和扩展第一个文本/纯文本部分。 ,还有一些如果没有文本/纯文本部分作为第一部分,则可以进行后备。

  3. 假设您的所有消息都是 multipart/* 的,只需在开头附近添加一个文本/纯文本部分,作为第一个部分,或者紧接在第一个文本/纯文本部分之后。这需要相同的逻辑,并且在某些客户端中查看可能会很尴尬,但对传入消息的假设较少,因此可能更安全。

如果您可以对其进行编码,那么在其上调用 Procmail 就非常简单;只需省略之前配方中的 h 标志(指定仅在标头中进行过滤)。

:0fw
| replace-entire-message-with-a-different-message

Sure you can. There is no direct support for MIME, though, so you probably need something more or less clever to make sure you don't mess up the MIME structure of the message you are processing.

As a simple starting example, suppose you have extracted an attachment to ~/public_html/prv/att000.jpg and have your web server set up to serve this at http://localhost:8080/~you/prv/att00.jpg, you could add a header like this;

:0fhw
| formail -I"X-Tracted: http://localhost:8080/~you/prv/att00.jpg"

Adding something inline to the message itself is harder, but my no means impossible.

Edit: Your script can be simplified to not use a temporary file (and by the by, avoid the multiple Useless Uses of Cat) if you extract the Message-Id from within your recipe:

# Whitespace in [square brackets] is tab, space
:0c
* ^Message-Id:[   ]*<\/[^@>]+
| (mkdir -p "webserver path"/"$MATCH"; munpack -C "webserver path"/"$MATCH" )

... but perhaps you want to keep your script (without the Useless Cats) and modify it so that it not only extracts the attachments, but also creates a modified message on standard output with the links to the attachments in-lined.

I can suggest three options, all of which are slightly cumbersome. Suppose your incoming message is a (multipart/mixed (text/plain (multipart/related text/html image/png image/png))); you could

  1. Wrap your message in a (multipart/related (text/plain: your list of links here) (message/rfc822: original message here: (multipart/mixed (text/plain (multipart/related text/html image/png image/png)))). This doesn't require you to parse the original message at all, but is awkward to view in most clients.

  2. Assume there is always a text/plain part, and attach the list of links to its end. This requires logic to find and extend the first text/plain part, and some sort of fallback if there isn't a text/plain part as the first part.

  3. Assume all your messages are multipart/* and just add a text/plain part near the beginning, as the very first part, or immediately after the first text/plain part. This requires the same logic, and might be awkward to view in some clients, but makes fewer assumptions about the incoming message, and so might be safer.

If you can code this up, invoking Procmail on it is quite simple; just omit the h flag from the earlier recipe (which specifies to only filter in the headers).

:0fw
| replace-entire-message-with-a-different-message
抚你发端 2024-12-11 03:35:35

目前我有这个:

在 procmail 中我做了一个这样的副本:

:0c
| myscript.sh

然后在 myscript.sh 中

path="webserver path"

tee _temp >& /dev/null # is there a different way of doing this?

# extract message-id
mid=$(cat _tempmail | formail -c -x message-id - | sed 's/ <\(.*\)@.*>/\1/')

mkdir $path/$mid

# extract all attachments into directory depending on the message id
cat _tempmail | munpack -C $path/$mid

所以我唯一要做的就是从邮件中获取消息 ID 并创建我的 url。

根据 Tripleee 的建议,我可以将此网址添加为标题。

如果消息中包含链接,那就太好了,也许我会在其他时间这样做。目前,我对这个解决方案很满意。

At the moment I have this:

in procmail I do a copy like this:

:0c
| myscript.sh

then in myscript.sh

path="webserver path"

tee _temp >& /dev/null # is there a different way of doing this?

# extract message-id
mid=$(cat _tempmail | formail -c -x message-id - | sed 's/ <\(.*\)@.*>/\1/')

mkdir $path/$mid

# extract all attachments into directory depending on the message id
cat _tempmail | munpack -C $path/$mid

So the only thing I have to do is get the message-id from the mail and create my url.

As suggested by tripleee I could add this url as a header..

It would be great if there is a link in the message, maybe I'll do that some other time. At the moment, I'm fine with this solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文