Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8368881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:29:49+00:00 2026-06-09T13:29:49+00:00

I have following script which processes emails and save them to csv file. there

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:29:51+00:00Added an answer on June 9, 2026 at 1:29 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which will delete a file off a BlackBerry. However,
I have the following python script which takes some inputs and puts them in
I have an image uploading script in which i use the following setup to
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
Currently Im have the following script which checks to see if a checkbox value
I have the following script, which is working for the most part Link to
I have the following script currently, which gets the category IDs as arrays: $sql_select_categories
I have a problem with following script. It generates a list of places which
I have the following function in a PHP script, which returns and API call:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.