bash - 如何在 bash 脚本中标记邮件?
问题描述
我有一个名为extract-pdf-attachments.sh的 bash 脚本,它会自动保存一个电子邮件附件 (pdf) 以与 qmail 和重组文件进行映射。请参阅:问题 58188368 Stackoverflow
脚本效果很好。
现在我还想用这个脚本中的 pdf 标记每条消息。想过这样做'sed 's/\(^Subject:\).*/\1 [Settled]/' "$mailmessage"?'
我似乎没有让它工作,所以我的问题是如何在脚本中使用 pdf 更改消息的主题。我可以使用以下内容:sed 's/\(^Subject:\)/\1 [Settled]/' "$mailmessage"
见第 58 行sed 's/\(^Subject:\)/\1 [Settled]/' "$mailmessage"
这似乎不起作用。
bash 脚本:(参见与上面相同的链接)
#!/usr/bin/env bash
# This script process mail message attachments from stdin MIME message
# Extract all PDF files attachments
# and return the MIME message to stdout for further processing
# Ensure all locale settings are set to C, to prevent
# reformime from failing MIME headers decode with
# [unknown character set: ANSI_X3.4-1968]
# See: https://bugs.gentoo.org/304093
export LC_ALL=C LANG=C LANGUAGE=C
# Setting the destination path for saved attachments
attachements='/home/users/name/home'
trap 'rm -f -- "$mailmessage"' EXIT # Purge temporary mail message
# Create a temporary message file
mailmessage="$(mktemp)"
# Save stdin message to tempfile
cat > "$mailmessage"
# Iterate all MIME sections from the message
while read -r mime_section; do
# Get all section info headers
section_info="$(reformime -s "$mime_section" -i <"$mailmessage")"
# Parse the Content-Type header
content_type="$(grep 'content-type' <<<"$section_info" | cut -d ' ' -f 2-)"
# Parse the Content-Name header (if available)
content_name="$(grep 'content-name' <<<"$section_info" | cut -d ' ' -f 2-)"
# Decode the value of the Content-Name header
content_name="$(reformime -c UTF-8 -h "$content_name")"
if [[ $content_type = "application/pdf" || $content_name =~ .*\.[pP][dD][fF] ]]; then
# Attachment is a PDF
if [ -z "$content_name" ]; then
# The attachment has no name, so create a random name
content_name="$(mktemp --dry-run unnamed_XXXXXXXX.pdf)"
fi
# Prepend the date to the attachment filename
filename="$(date +%Y%m%d)_$content_name"
# Save the attachment to a file
reformime -s "$mime_section" -e <"$mailmessage" >"$attachements/$filename"
# Prepend the text settled to the mail subject
sed 's/\(^Subject:\)/\1 [Afgehandeld]/' "$mailmessage"
fi
done < <(reformime < "$mailmessage") # reformime list all mime sections
cat <"$mailmessage" # Re-inject the message to stdout for further processing
解决方案
推荐阅读
- ruby - dailyLimitExceededUnreg:已超过未经验证使用的每日限制。继续使用需要注册
- php - 加入 2 个表后如何在 Codeigniter 视图中使用过滤器
- git - 如何删除 github 上的最新提交
- entitymanager - entityManager 数据经常变化
- java - JAVA的引用和C、C++的引用有区别吗?
- javascript - Html5画布在mousedown和mouseup之间画线
- mysql - 将字符串拆分为数组并执行类似操作以匹配另一列中的值
- java - 应用程序退出时优雅地关闭 javaagent
- omnet++ - 如何修改代码以在 omnet 中具有特定于 DSDV 的各种节点更改?
- html - 抓取特定的
雅虎财经类(VBA、Excel)