need help with nullmailer-inject in python cgi script to send attachements ?

Denis S. Otkidach ods at strana.ru
Thu Mar 24 09:08:46 EST 2005


On 24 Mar 2005 05:20:06 -0800 tvmaly at gmail.com wrote:

TC> Due to the restrictions I have at my host, I cannot use smtplib in
TC> my email cgi script.  They gave me a script they use that calls
TC> nullmailer-inject.   I am trying to figure out how to add the
TC> ability to send attachments via the nullmailer-inject call.  I could
TC> not find much documentation on google about nullmailer.  Has anyone
TC> used it before to send attachments?   Here is function  I have so
TC> far that sends a regular email.  The variables like recipient are
TC> set from a cgi in another part of the script.
[...]
TC>   mailfl=os.popen('/usr/bin/nullmailer-inject -f '+fromaddr,'w')
TC>   mailfl.write('To: '+recipient+nl)
TC>   if fromaddr: mailfl.write('From: '+fromaddr+nl)
TC>   mailfl.write('Subject: %s%s\n\n'%(subject,uri))
TC>   mailfl.write(intro)
TC>   rx=0
TC>   prev=''
TC>   for x in cap:
TC>     while (rx<len(reql) and reql[rx]<x):
TC>       if reql[rx]<>prev:
TC>         putln(reql[rx],mailfl)
TC>       rx=rx+1
TC>     putln(x,mailfl)
TC>     prev=x
TC> 
TC>   for x in envl:
TC>     mailfl.write('%s: %s\n'%(x,env[x]))
TC>   mailfl.close()

Use email package. Something like the following (untested):

from email.MIMEText import MIMEText
msg = MIMEText(body)
msg['Subject'] = ...
msg['From'] = ...
msg['To'] = ...

from email.MIMEBase import MIMEBase
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(open(filename, 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', 
                      filename=filename)
from email.Encoders import encode_base64
encode_base64(attachment)
msg.attach(attachment)

# using popen is unsafe if we can't trust fromaddr, using subprocess
from subprocess import Popen, PIPE
mailfl = Popen(['/usr/bin/nullmailer-inject',  '-f', fromaddr],
               stdin=PIPE)
mailfl.stdin.write(msg.as_string())
mailfl.stdin.close()
mailfl.wait()

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]



More information about the Python-list mailing list