[Mailman-Users] Mailman Archive Migration Question

Mark Sapiro mark at msapiro.net
Sat Apr 9 15:49:24 EDT 2016


On 04/09/2016 08:20 AM, Richard Robbins wrote:

> Once I have the collection culled to what I want to retain I thought it
> would be nice to use an email reader with a redirect command that would
> permit me to then send those messages to my WordPress site using the post
> by email function.
> 
> Does this approach make sense?  Are there alternatives I should consider?
> 
> Oh -- and I need to find a current mail program that will allow me redirect
> existing messages to a new address as opposed to using forward.


Mutt can 'bounce' (i.e. redirect) a mail to a new recipient, but it is
an almost trivial Python script to read a mbox and resend every message
to a new address. For example, the script at
<https://www.msapiro.net/scripts/post_from_mbox> does essentially that
in a Mailman context.

I have attached a rough modification of that script that can read a mbox
and remail the messages therein to a given address.

These things are set in the script

SMTPHOST = 'localhost'
SMTPPORT = 25
SMTPUSER = None
SMTPPW = None
MYHOST = socket.gethostname()
ENVFROM = 'root@{}'.format(MYHOST)

The script will work as is if there is an MTA listening on localhost:25
that can deliver the mail to the given address. If not, set SMTPHOST to
the fully qualified name of the MTA you want to use and set SMTPPORT to
the port. If you change nothing else, the script will attempt to send
via that host without authentication. If you need to authenticate, set
SMTPUSER and SMTPPW appropriately and the script will attempt to start
TLS and authenticate as that user.

MYHOST is the host name that will me used in EHLO for a startTLS
connection and may or may not need adjustment. ENVFROM is the envelope
sender and can be adjusted if needed.

The script does almost no error checking so if anything goes wrong, it
will stop with an exception and a traceback and you'll have to fix the
problem and try again.

Start with a mbox with only one or two messages and a test address to
send to until you're sure it works.

-- 
Mark Sapiro <mark at msapiro.net>        The highway is for gamblers,
San Francisco Bay Area, California    better use your sense - B. Dylan


-------------- next part --------------
#! /usr/bin/env python

"""Remail all messages in some mbox file to a given address.

Usage: %(PROGRAM)s [options] filename address

Where:

    --verbose
    -v
        Print information about what's done.

    --help
    -h
        Print this help message and exit.

'filename' is the name of a *nix mbox file containing one or more messages to
be remailed to 'address'.
"""

import sys
import getopt
import socket
import smtplib

from mailbox import mbox

PROGRAM = sys.argv[0]

def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, __doc__
    if msg:
        print >> fd, msg
    sys.exit(code)


conn = None
SMTPHOST = 'localhost'
SMTPPORT = 25
SMTPUSER = None
SMTPPW = None
MYHOST = socket.gethostname()
ENVFROM = 'root@{}'.format(MYHOST)
def sendit(msg, address):
    global conn
    if not conn:
        conn = smtplib.SMTP()
        conn.connect(SMTPHOST, SMTPPORT)
        if SMTPUSER:
            conn.starttls()
            conn.ehlo(MYHOST)
            conn.login(SMTPUSER, SMTPPW)
    conn.sendmail(ENVFROM, address, msg.as_string())


def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'hv',
            ['help', 'verbose'])
    except getopt.error, msg:
        usage(1, msg)

    verbose = False
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-v', '--verbose'):
            verbose = True

    if len(args) != 2:
        usage(1, 'Exactly two arguments required.')

    filename = args[0]
    address = args[1]
    mb = mbox(filename, create=False)
    for msg in mb:
        if verbose:
            print """Resending message
    Subject: %s
    Date: %s
""" % (msg.get('subject', 'N/A'), msg.get('date', 'N/A'))
        sendit(msg, address)


if __name__ == '__main__':
    main()




More information about the Mailman-Users mailing list