使用 Mutt 选择并保存所有 .jpg 附件(不包括其他附件)

使用 Mutt 选择并保存所有 .jpg 附件(不包括其他附件)

我在用杂种狗,我的收件箱中有一封邮件,其中包含大约 1000 个附件,其中约 50% 是 .jpg 文件。我想将所有这些附件保存到指定目录中,而不保留其他附件。

是否可以通过几个按键或命令轻松完成此操作?Shift+T似乎允许基于模式标记消息,但不允许标记附件。

答案1

您可以标记(t)带有附件的消息,然后通过管道(;|)程序发送消息以提取附件。我建议您在您的.muttrc

set pipe_split = yes # sends tagged messages 1by1 otherwise they're concatenated with $pipe_sep
unset pipe_decode    # don't strip headers and eval mimes when piping

我写了一个非常原始的(WIP 尚未完成但可以工作...)ruby 脚本来提取附件。你可以用它来激发自己的灵感...

#!/usr/bin/env ruby
#
# scriptname : ~/bin/mutt_utils.rb
#
# Messages tagged
# To work with tagged message you have to :set pipe_split
# Otherwise only first message tagged will be processed.
#
require 'optparse'
require 'fileutils'
require 'mail'
#
# monkeypatch Mail::Message class to fix Invalid byte sequence with ruby 1.9
# https://github.com/mikel/mail/issues/340
# https://github.com/mreinsch/mail/commit/c7818a95f9f9fddc675f75452ea5000c46c30d94
#
if RUBY_VERSION >= "1.9.1"
  class Mail::Message
    def raw_source=(value)
      value.force_encoding("binary") if RUBY_VERSION >= "1.9.1"
      @raw_source = value.to_crlf
    end
  end
end

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: #{File.basename $0} [OPTIONS]"
  opts.on("-o", "--output PATH", "save in PATH folder") do |path|
    options[:output] = path
  end
  opts.on("-O", "--overwrite", "overwrite existing files") do |owr|
    options[:overwrite] = owr
  end
  opts.on("-n", "--name-regexp PATTERN", "save only attachment's name matching PATTERN") do |pat|
    options[:name_pattern] = pat
  end
  opts.on("-c", "--content-regexp PATTERN", "save only attachment's content_type matching PATTERN") do |pat|
    options[:content_pattern] = pat
  end
  opts.on("-v", "--verbose", "verbose") do |v|
    options[:verbose] = v
  end
  opts.on("-h", "--help", "usage") do
    puts opts
    exit
  end
end.parse!
puts "Options passed : #{options}" if options[:verbose]

if options[:output]
  unless File.directory?(options[:output])
    $stderr.puts "'#{options[:output]}' is not a valid folder !"
    exit
  end
end

def sanitize(value)
  value.gsub(/\s/, '_').gsub(/:/, '-').gsub(/\//, '-').gsub(/\\/, '-')
end

def check_criteria(criteria, pattern)
  return true if pattern.nil?
  criteria =~ /#{pattern}/i
end

data = ARGF.readlines.join
mail = Mail.read_from_string data #.force_encoding("UTF-8")
ts = mail.date.strftime("%Y-%m-%dT%H%M")
puts "Processing mail '#{mail.subject}' " if options[:verbose]
subject = mail.subject.nil? ? 'no subject' : mail.subject.downcase

mail.attachments.each do |att|
  puts "Attachment : filename=#{att.filename} content_type=#{att.content_type}" if options[:verbose]
  valid_name = check_criteria(att.filename, options[:name_pattern])
  valid_content = check_criteria(att.content_type, options[:content_pattern])
  unless valid_name
    puts "Filename doesn't match #{options[:name_pattern]}" if options[:verbose]
  end
  unless valid_content
    puts "Content doesn't match #{options[:content_pattern]}" if options[:verbose]
  end
  next unless ( valid_name && valid_content )
  fn = sanitize "#{ts}_#{subject}_#{att.filename}"
  fn = File.join(options[:output], fn) if options[:output]
  puts "Saving attachment to #{fn}." if options[:verbose]
  if (!options[:overwrite] && File.exist?(fn))
    $stderr.puts "'#{fn}' already exists ! Skipping." if options[:verbose]
    next
  end
  begin
    File.open(fn, 'wb') {|f| f.write att.body.decoded }
  rescue Exception => e
    $stderr.puts "Error while saving #{fn} ! Cause: #{e.message}"
  end
end

别忘了chmod +x ~/bin/mutt_utils.rb

使用示例:用附件标记一些消息,;然后点击|

在提示符下输入~/bin/mutt_utils.rb -o ~/Desktop/

您现在应该可以在桌面上找到您的附件。

相关内容