本文介绍了自动保存电子邮件附件以使用qmail和改革格式进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动保存电子邮件附件以使用qmail和改革格式进行映射

我正在尝试通过dot-qmail文件将附件自动移动到另一个位置.

I'm trying to move attachment automatically to another locatie with the dot-qmail file.

我的.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/

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

This works for a simple mail with one attachment. My questions:

  1. 我还如何将属于此附件的邮件消息移动到名为完成"的邮箱.
  2. 以上命令不能在一个邮件消息中使用多个附件吗?如何调整该行以用于多个附件?
  3. 如果文件名包含多个点,例如"how.areyou.pdf",则此方法不起作用

感谢您的帮助

推荐答案

这是针对您的问题的功能强大的实现.

Here is a featured implementation for your problem.

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

First save and set permissions to this bash script for your user.
You will need to call it from your .qmail file:

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/

这篇关于自动保存电子邮件附件以使用qmail和改革格式进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:09