如何将使用poplib捕获的电子邮件转发到其他电子邮件地址?

时间:2012-08-09 23:52:10

标签: python poplib

我有以下脚本处理电子邮件并将其保存到csv文件。我将使用mechanize lib来处理提取的电子邮件数据,以便在另一个Web界面上进一步处理。有时它可能会失败现在我可以捕获特定的电子邮件而不会有任何问题但是如何将被困电子邮件转发到我可以手动处理或查看它有什么问题的不同地址?

这是脚本

import ConfigParser
import poplib
import email
import BeautifulSoup
import csv
import time

DEBUG = False
CFG = 'email'    # 'email' or 'test_email'


#def get_config():
def get_config(fnames=['cron/orderP/get_orders.ini'], section=CFG):
    """
    Read settings from one or more .ini files
    """
    cfg = ConfigParser.SafeConfigParser()
    cfg.read(*fnames)
    return {
        'host':    cfg.get(section, 'host'),
        'use_ssl': cfg.getboolean(section, 'use_ssl'),
        'user':    cfg.get(section, 'user'),
        'pwd':     cfg.get(section, 'pwd')
    }

def get_emails(cfg, debuglevel=0):
    """
    Returns a list of emails
    """
    # pick the appropriate POP3 class (uses SSL or not)
    #pop = [poplib.POP3, poplib.POP3_SSL][cfg['use_ssl']]

    emails = []
    try:
        # connect!
        print('Connecting...')
        host = cfg['host']
        mail = poplib.POP3(host)
        mail.set_debuglevel(debuglevel)  # 0 (none), 1 (summary), 2 (verbose)
        mail.user(cfg['user'])
        mail.pass_(cfg['pwd'])

        # how many messages?
        num_messages = mail.stat()[0]
        print('{0} new messages'.format(num_messages))

        # get text of messages
        if num_messages:
            get = lambda i: mail.retr(i)[1]                 # retrieve each line in the email
            txt = lambda ss: '\n'.join(ss)                  # join them into a single string
            eml = lambda s: email.message_from_string(s)    # parse the string as an email
            print('Getting emails...')
            emails = [eml(txt(get(i))) for i in xrange(1, num_messages+1)]
        print('Done!')
    except poplib.error_proto, e:
        print('Email error: {0}'.format(e.message))

    mail.quit() # close connection
    return emails

def parse_order_page(html):
    """
    Accept an HTML order form
    Returns (sku, shipto, [items])
    """
    bs = BeautifulSoup.BeautifulSoup(html)  # parse html

    # sku is in first <p>, shipto is in second <p>...
    ps = bs.findAll('p')                    # find all paragraphs in data
    sku = ps[0].contents[1].strip()         # sku as unicode string
    shipto_lines = [line.strip() for line in ps[1].contents[2::2]]
    shipto = '\n'.join(shipto_lines)        # shipping address as unicode string

    # items are in three-column table
    cells = bs.findAll('td')                        # find all table cells
    txt   = [cell.contents[0] for cell in cells]    # get cell contents
    items = zip(txt[0::3], txt[1::3], txt[2::3])    # group by threes - code, description, and quantity for each item

    return sku, shipto, items

def get_orders(emails):
    """
    Accepts a list of order emails
    Returns order details as list of (sku, shipto, [items])
    """
    orders = []
    for i,eml in enumerate(emails, 1):
        pl = eml.get_payload()
        if isinstance(pl, list):
            sku, shipto, items = parse_order_page(pl[1].get_payload())
            orders.append([sku, shipto, items])
        else:
            print("Email #{0}: unrecognized format".format(i))
    return orders

def write_to_csv(orders, fname):
    """
    Accepts a list of orders
    Write to csv file, one line per item ordered
    """
    outf = open(fname, 'wb')
    outcsv = csv.writer(outf)
    for poNumber, shipto, items in orders:
      outcsv.writerow([])     # leave blank row between orders
      for code, description, qty in items:
        outcsv.writerow([poNumber, shipto, code, description, qty])
        # The point where mechanize will come to play

def main():
    cfg    = get_config()
    emails = get_emails(cfg)
    orders = get_orders(emails)
    write_to_csv(orders, 'cron/orderP/{0}.csv'.format(int(time.time())))

if __name__=="__main__":
    main()

1 个答案:

答案 0 :(得分:0)

众所周知,POP3仅用于检索(知道或了解电子邮件如何工作的人),因此为了发送消息而使用POP3毫无意义,为什么我提到如何转发使用poplib到不同的电子邮件地址?作为一个问题。

完整的答案是 smtplib可以用于转发poplib捕获的电子邮件消息,您只需要捕获邮件正文并使用smtplib将其发送到所需的电子邮件地址。此外,正如Aleksandr Dezhin引用的那样,我会同意他的看法,因为有些SMTP服务器会对处理它们的邮件施加不同的限制。

除此之外,如果你在Unix机器上,你可以使用sendmail实现这一点。