我试图在特定的mbox中向现有的电子邮件中添加一个额外的标题项。
X-存档到-CRM: true,user,CRM-ID
在归档之后,CRM-ID基本上是CRM数据库中的id。
我的MTA是sendmail,不确定这是否重要。
我看过邮件,邮件,但不知道怎么做。
我已经能够在交互模式下使用Mutt来完成这个任务,所以必须有一种方法在命令行中这样做。
任何(命令行)实用程序都可以,它只需要非交互地工作。
发布于 2021-08-23 07:07:47
使用perl -i可以很容易地实现这一点,因为mbox文件只是纯文本。perl有各种mbox操作库模块,但是像这样简单的模块并不需要它们。
最大的困难是避免写入mbox文件,而其他的东西则同时写入mbox文件。
下面的脚本使用标准的dotlock方法,避免对同一个mbox文件同时编写多个作者(即使用mboxfilename.lock)。
它可能应该在 dotlock之外执行flock和fcntl锁定<#>in,以匹配您的邮件传递代理(MDA)和其他可能正在编写mbox的程序(例如mutt或其他邮件用户代理(MUA)、邮件传输代理(MTA) (如postfix或sendmail )或POP或IMAP守护进程)所使用的锁定方法(S)。
作为读者的练习,我将留下这一点--请参阅档案::FcntlLock和档案::Flock perl库模块(还有几个类似的模块,它们具有不同的实现方式,这只是我发现的第一个模块)。您可以使用apt-get install libfile-flock-perl libfile-fcntllock-perl在Debian (等)上安装这些文件。其他发行版可能已经打包了它们或类似的模块。否则,使用cpan安装。
#!/usr/bin/perl -i.bak
#!/usr/bin/perl -i
# delete the first #! line if you don't want perl -i to create a .bak copy
use strict;
use Getopt::Std;
my %opts;
getopts('m:i:u:h', \%opts);
if ($opts{h}) {
print "$0 <-m messsage-id> <-u user> <-i crmid> [mbox file...]\n";
exit 1;
};
my $msgid = $opts{m} // die "-m messsage-id is required\n";
my $user = $opts{u} // die "-u user is required\n";
my $crmid = $opts{i} // die "-i crmid is required\n";
# I don't want to implement my own -i, so I'll just iterate
# over each file on @ARGV one at a time
my @files = @ARGV;
foreach my $mbox (@files) {
# if the mbox is locked, then wait until it isn't.
while (-e "$mbox.lock") {
print "$mbox is locked!\n";
sleep 1;
};
# lock it
open(my $touch, ">", "$mbox.lock") || die "couldn't lock $mbox: $!\n";
close($touch);
@ARGV=($mbox);
while (<>) {
print;
# case-insensitive match for "Message-ID" literal string,
# case-sensitive for actual $msgid
if (m/^(?:(?i)Message-ID:) <$msgid>/) {
print "X-archived-to-crm: true,$user,$crmid\n"
};
};
# remove the lock
unlink "$mbox.lock";
}给定以下mbox文件:
From cas@example.org Mon Aug 23 16:04:42 2021
Return-Path:
X-Original-To: cas@example.org
Delivered-To: cas@example.org
Received: by example.org (Postfix, from userid 1000)
id 6B1DE3F2C; Mon, 23 Aug 2021 16:04:42 +1000 (AEST)
Date: Mon, 23 Aug 2021 16:04:42 +1000
From: Craig Sanders
To: Craig Sanders
Subject: test
Message-ID:
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Length: 56
Lines: 6
test messsage
craig
--
craig sanders 运行以下命令:
$ ./insert-header.pl -m 'YSM6eiyrpvNKgxex@example.org' -i 99999 -u cas test.mbox结果将mbox文件更改为:
From cas@example.org Mon Aug 23 16:04:42 2021
Return-Path:
X-Original-To: cas@example.org
Delivered-To: cas@example.org
Received: by example.org (Postfix, from userid 1000)
id 6B1DE3F2C; Mon, 23 Aug 2021 16:04:42 +1000 (AEST)
Date: Mon, 23 Aug 2021 16:04:42 +1000
From: Craig Sanders
To: Craig Sanders
Subject: test
Message-ID:
X-archived-to-crm: true,cas,99999
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Length: 56
Lines: 6
test messsage
craig
--
craig sanders 顺便说一句,我还在我的主mbox的副本上测试了这一点:
$ ./insert-header.pl -m '.*' -i 99999 -u cas main.mbox并且它在mbox中的每个消息中都插入了相同的X-archived-to-crm头。
https://unix.stackexchange.com/questions/665804
复制相似问题