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

StackOverflow https://stackoverflow.com/questions/11893618

  •  25-06-2021
  •  | 
  •  

Question

I have following script which processes emails and save them to csv file. there will be advancement to script where I will use mechanize lib to process the extracted emails data for further processing on an another web interface. There are times it may fail now I can trap that specific email without having any problem but how can I forward the trapped email to a different address where I can process it manually or see what's wrong with it?

Here's the script

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()
Was it helpful?

Solution

As we all know that POP3 is used solely for retrieval (those who know or have idea how emails work) so there is no point using POP3 for the sake of message sending that why I mentioned How to forward an email message captured with poplib to a different email address? as an question.

The complete answer was smtplib can be used for that sake to forward an poplib captured email message, all you need to do is to capture the message body and send it using smtplib to the desired email address. Furthermore as Aleksandr Dezhin quoted I will agree with him as some SMTP servers impose different restrictions on message they are processed.

Beside that you can use sendmail to achieve that if you are on Unix machine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top