【问题标题】:Save an email attachment automatically to map with qmail and reformime自动保存电子邮件附件以映射到 qmail 和 Reformime
【发布时间】:2020-01-30 16:35:14
【问题描述】:

自动保存电子邮件附件以映射到 qmail 和 Reformime

我正在尝试使用 dot-qmail 文件将附件自动移动到另一个位置。

我的 .qmail 文件

#------------------------------------------------------------
| condredirect pdf-junkmail headermatch 'X-Spam-Status: Yes'
| reformime -X /bin/sh -c "if [ "\${FILENAME#*.}" == "pdf" ];  then cat > /home/users/name/home/$(date +%Y%m%d)_\$FILENAME; fi"
# Forward not set
# Vacation Message not set
./Maildir/

这适用于带有一个附件的简单邮件。我的问题:

  1. 如何将属于此附件的邮件也移动到名为“完成”的邮箱中。
  2. 上述命令不适用于一封邮件中的多个附件?如何调整这条线以适用于多个附件?
  3. 如果文件名包含多个点,例如“how.areyou.pdf”,则此方法不起作用

感谢您的帮助

【问题讨论】:

  • 您对-c shell commands 的引用是错误的,并且您的== 比较不适用于POSIX sh。文件扩展名提取需要两个锐角##,否则它不会抓取* 的最长匹配,并且当文件名中有多个点时将无法工作。从变量形成字符串时需要引号。这是一个固定版本:| reformime -X /bin/sh -c 'if [ "${FILENAME##*.}" = "pdf" ]; then cat > "/home/users/name/home/$(date +%Y%m%d)_\$FILENAME"; fi'

标签: bash email pdf attachment qmail


【解决方案1】:

这是针对您的问题的特色实现。

首先为您的用户保存并设置此 bash 脚本的权限。
您需要从您的 .qmail 文件中调用它:

extract-pdf-attachments.sh

#!/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"
  fi

done < <(reformime < "$mailmessage") # reformime list all mime sections

cat <"$mailmessage" # Re-inject the message to stdout for further processing

那么在你.qmail:

#------------------------------------------------------------
| condredirect pdf-junkmail headermatch 'X-Spam-Status: Yes'
| bash /path/to/extract-pdf-attachments.sh | condredirect done true
# Forward not set
# Vacation Message not set
./Maildir/

【讨论】:

  • 所有附件都命名为 YYYYMMDD_unnamed_XXXXXXXX.pdf。如果我尝试理解上面的代码,我会认为名称应该是 YYYYMMDD_named.pdf。出了什么问题?
  • 我无法将其移至名为“完成”的邮箱。邮箱“done”是一个maildir(邮箱格式);所以./Maildir/.done/
  • 如果附件文件的 MIME 部分没有content_name,则应用名称YYYYMMDD_unnamed_&lt;random characters replacing the XXXXXXXX&gt;.pdf,否则使用YYYYMMDD_content_name 命名。对于done邮箱,可能我手头没有qmail,使用.done或者完整的文件系统路径,我真的不确定这里使用的语法。
  • 通过添加'-c'选项来解决它。因此 'content_name="$(reformime -c UTF-8 -h "$content_name")"' 来源:sourceforge.net/p/courier/mailman/message/24972857
  • 2021 年仍在工作!在 Ubuntu 上,我需要安装 maildrop 才能使用命令“reformime”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2021-06-26
  • 2015-05-29
相关资源
最近更新 更多