我需要一些建议/向正确的方向推进。
我已经写了一些小脚本,接收传入的HTML电子邮件,将其转换为PostScript,然后通过CUPS将其发送到指定的打印机。打印机基于电子邮件的接收方。
我正在使用以下方法来实现这一点;
The flow
问题
我的问题是:如何从电子邮件中获取这些图像,以便它们能够在PostScript文件中成功打印出来?
如果PostScript不合适的话,我非常乐意转换成PDF格式,但是即使转换成PDF格式也让我失去了图像,因为我无法得到它们。
.procmailrc
SHELL=/bin/bash
# Extract the subject and normalise
SUBJECT=`formail -x"Subject: "\
| /usr/bin/tr '[:space:][:cntrl:][:punct:]' '_' | expand | sed -e 's/^[_]*//' -e 's/[_]*$//'`
YMD=`date +%Y%m%d`
MAKE_SURE_DIRS_EXIST=`
mkdir -p received_mail/backup
if [ ! -z ${SUBJECT} ]
then
mkdir -p received_mail/${YMD}/${SUBJECT}
else
mkdir -p received_mail/${YMD}/no_subject
fi
`
# Backup all received mail into the backup directory appending to a file named by date
:0c
received_mail/backup/${YMD}.m
# If no subject, just store the mail
:0c
* SUBJECT ?? ^^^^
received_mail/${YMD}/no_subject/.
# Else there is a subject, generate a unique filemane, place the received email
# in that file and then execute process_mail passing the filename and subject as parameters
:0Eb
| f=`uuidgen`; export f; cat > received_mail/${YMD}/${SUBJECT}/${f}; $HOME/bin/process_mail received_mail/${YMD}/${SUBJECT}/${f} "${SUBJECT}"
# and don't deliver to standard mail, don't want to clutter up the inbox.
:0
/dev/nullprocess_mail
#/bin/bash
# Test Printer
printer=$(whoami)
file=$1
subject=$2
function process_rrs {
typeset file
file=$1
cat $file \
| $HOME/bin/get_html_from_message \
| html2ps \
| lp -d ${printer} -o media=a4 2>&1
}
case "$subject" in
*)
process_rrs $file
;;
esacget_html_from_message
cat | awk '
BEGIN {
typeout=0
}
{
if($0 ~ /<html/)
typeout=1
if($0 ~ /^------=/)
typeout=0
if(typeout)
print $0
}'编辑:格式化
发布于 2016-06-03 14:49:08
我已经想出了如何实现这一目标。详情如下。所有这些都运行在两个负载平衡的CentOS 6盒上。
应用程序
是如何工作的
使用上面的过程,我可以将它降到一个脚本,.procmailrc。这就是我在.procmailrc文件中所写的内容。
SHELL=/bin/bash
# Designate the printer. Printer names match usernames so you don't have to manually change 60+ files.
printer=`whoami`
# Generate a unique ID
f=`uuidgen`
# Convert email, including headers and body into a HTML file and save off the images using MHONARC https://www.mhonarc.org/
# Open file and search <!--X-Body-of-Message--> string using SED and collect all text to EOF.
# Pipe the result into SED again to remove unwanted HTML tags added by MHONARC
# Pipe result into HTML2PS to convert to PostScript
# Pipe PostScript file to the designated printer
:0E
| mhonarc -single > ${f}.html; sed -n '/^<!--X-Body-of-Message-->$/ { s///; :a; n; p; ba; }' ${f}.html | sed -e '/<hr>/d' | html2ps | lp -d ${printer} -o media=a4 2>&1
# Finally, delete the email
:0
/dev/null我对"sed“不是很了解,而且很可能有更容易的方法来实现这一点。我会在某个时候进一步调查。
希望这对某人有帮助:)
发布于 2016-05-31 18:39:45
问题可能是不完全了解HTML是如何在电子邮件中表示的。通常会有一个带有一个HTML部件和多个映像的MIME多部件。HTML在图像链接中使用cid:寻址方案来引用这些同级部分。但是,如果您只提取HTML,它就不再存在于有任何兄弟关系的上下文中。(即使将所有部件提取到文件中,cid:通常也不会映射到本地文件。也许您可以对HTML进行后置处理以解决这个问题;但我想也许您的方法应该重新考虑一下。您考虑过使用带有本地HTML支持的邮件客户端来呈现这些消息吗?)
一个简单的xmlstarlet脚本或类似于从任何img链接的src属性中去掉cid:前缀的脚本应该不难,但是如果您尝试这个路径,可能还需要做一些其他的事情。
https://stackoverflow.com/questions/37550657
复制相似问题