繁体   English   中英

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

[英]How to forward an email message captured using poplib to a different email address?

我有以下脚本处理电子邮件并将其保存到csv文件。 脚本将有所进步,在这里我将使用机械化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()

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

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

此外,如果您在Unix机器上,则可以使用sendmail来实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM