
Sending E-Mail From the AS/400
The AS/400 has some reasonably good e-mail facilities built-in, however there are times when it's still easier to be able to send e-mail from within Python, particularly when you have to use any kind of custom formatting that exploits Python's strengths.
The module listed below will send an HTML formatted e-mail (no specific HTML formatting included, that's part of the "text" spec) to the specified address. Be sure that the "sender" address is at least a valid domain for your e-mail server, otherwise the server may reject it.
To use this module, you would simply call:
SendEmail('myname@mydomain.com','AS400 <test@mydomain.com>','Hey, it works!','Message body here!')
Here's the module:
def SendEmail( recipient = 'webmaster@yourdomain.com',\
sender = 'Automated Update <webmaster@yourdomain.com>',\
subject = 'no subject',\
text = 'No Message Text Specified'):
#
# Send an e-mail
#
maildate = time.strftime("%a, %d %b %Y %H:%M:%S",time.localtime(time.time()))
output = StringIO()
message = MimeWriter.MimeWriter(output)
message.addheader("To", recipient)
message.addheader("From", sender)
message.addheader("Subject", subject)
message.addheader("Date", maildate)
message.addheader("MIME-Version","1.0")
message.startmultipartbody("alternative")
instructions = message.nextpart()
instructions.addheader("Content-Transfer-Encoding","quoted-printable")
instructions.flushheaders()
mailfile = instructions.startbody("text/html")
#
# Here we have to do our own "Quoted-Printable"
encoding
# due to a bug in the QUOPRI module that can't properly
# compensate for the EBCDIC to ASCII conversion
#
text = binascii.b2a_qp(text.encode('ascii'))
text = text.decode('ascii')
mailfile.write(text)
message.lastpart()
ftext = output.getvalue()
#
# SEND THE E-MAIL TO THE DEFINED SERVER
#
serverip = "myserver.iporname.com"
server = smtplib.SMTP(serverip)
server.set_debuglevel(0)
server.sendmail(sender, recipient, ftext)
server.quit()
return