用于批量转发电子邮件的电子邮件工具

用于批量转发电子邮件的电子邮件工具

我需要转发 200 封电子邮件。我有一个 Gmail 帐户并安装了 Thunderbird。我无法手动转发每封邮件;我想一次性转发它们。我该怎么做?

答案1

如果这些电子邮件具有任何共同特征,您可以对其进行过滤,例如它们都来自一个(或几个)发件人,那么请在 Gmail 中创建一个过滤器,并在“转发到:”文本框中填写要将它们发送到的地址,然后选择“也将过滤器应用于 X 个匹配的对话”。

它将立即发送过滤后的消息。

答案2

Gmail 的行为显然已经改变,Niki 的回答中的提示现在不适用了。我刚刚遇到了这个问题,但找不到真正的解决方案,所以这里是我的 Python 供感兴趣的人参考。这是一个基本的脚本:标题没有经过仔细重写,并且它不处理对话。要使用它,请确保您的帐户和要从中获取邮件的“文件夹”中启用了 IMAP。

import email
import email.Utils
import imaplib
import os
import smtplib
import tempfile
import time

# Download email.
gmail = imaplib.IMAP4_SSL('imap.gmail.com')
# 'password' can be an app-specific password if two-step authentication is
# enabled.
gmail.login('[email protected]', 'password')
print 'Login to IMAP server succeded.'
# Select an appropriate "folder".
gmail.select('[Gmail]/All Mail', readonly=True)
message_ids = gmail.search(None, '(OR FROM "[email protected]" TO "[email protected]")')[1][0].split()
# Fetch all messages, that might take long. Assumes the message numbers don't
# change during the session.
print 'Fetching email...'
messages = map(lambda x: gmail.fetch(x, '(RFC822)')[1][0][1], message_ids)
print '%d messages fetched' % len(message_ids)
# We're done with IMAP.
gmail.shutdown()

# Parse email content into objects we can manipulate.
messages = map(email.message_from_string, messages)

# I like mail sorted by date. Does not account for different time zones.
messages.sort(key=lambda message: email.Utils.parsedate(message['Date']))
print 'Sorted email.'

# Write email to a directory if you want to inspect the changes from processing
# (read below).
temp_directory_in = tempfile.mkdtemp(suffix='_email')
map(lambda pair: file(os.path.join(temp_directory_in, '%d.eml' % pair[0]), 'w').write(pair[1].as_string()), enumerate(messages))
print 'Unprocessed email saved at \'%s\'.' % temp_directory_in

# Process your messages. Email with third-party addresses in 'to', 'cc', 'bcc',
# 'reply-to', 'in-reply-to' and 'references' fields may be tricky: Gmail
# apparently automatically copies third-party people who appear in some of
# these headers so it might be safer to canonicalize or remove them. Also,
# Gmail does not seem to like email that already contains a message id, so just
# remove this too.
def remove_header(message, header):
  if header in message:
    del message[header]

def remove_headers(message, headers):
  for header in headers:
    remove_header(message, header)

def process_message(message):
  if 'To' in message:
    if '[email protected]' in message['From']:
      message.replace_header('To', '"You" <[email protected]>')
    else:
      message.replace_header('To', '"Me" <[email protected]>')

  # Gmail will rewrite the 'from' address (not the name!) to match your email
  # address. It will also add a 'bcc' matching the recipient if it is different
  # from the 'to' address.
  remove_headers(message, ['Cc', 'Bcc', 'Reply-To', 'In-Reply-To', 'References', 'Message-ID'])

map(process_message, messages)
print 'Processed email.'

# At this point it may be a good idea to actually peek at you're going to send.
temp_directory_out = tempfile.mkdtemp(suffix='_email')
map(lambda pair: file(os.path.join(temp_directory_out, '%d.eml' % pair[0]), 'w').write(pair[1].as_string()), enumerate(messages))
print 'Processed email saved at \'%s\'.' % temp_directory_out

# If it looks good to you, send it out.
if raw_input('Continue? ') == 'yes':
  gmail = smtplib.SMTP_SSL('smtp.gmail.com')
  gmail.login('[email protected]', 'password')
  print 'Login to SMTP server succeded.'

  for index, message in enumerate(messages):
    status = gmail.sendmail('[email protected]', '[email protected]', message.as_string())
    print 'Email %d/%d sent (status: %s)' % (index + 1, len(messages), status)
    time.sleep(1)

  gmail.quit()
  print 'All done.'

相关内容