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

T

tvmaly

Due to the restrictions I have at my host, I cannot use smtplib in my
email cgi script. They gave me a script they use that calls
nullmailer-inject. I am trying to figure out how to add the ability
to send attachments via the nullmailer-inject call. I could not find
much documentation on google about nullmailer. Has anyone used it
before to send attachments? Here is function I have so far that
sends a regular email. The variables like recipient are set from a cgi
in another part of the script.

thanks

Ty

def genmail(reql):
if nosend: return
recip=recipient
cap=form.keys()
cap.sort()
mailfl=os.popen('/usr/bin/nullmailer-inject -f '+fromaddr,'w')
mailfl.write('To: '+recipient+nl)
if fromaddr: mailfl.write('From: '+fromaddr+nl)
mailfl.write('Subject: %s%s\n\n'%(subject,uri))
mailfl.write(intro)
rx=0
prev=''
for x in cap:
while (rx<len(reql) and reql[rx]<x):
if reql[rx]<>prev:
putln(reql[rx],mailfl)
rx=rx+1
putln(x,mailfl)
prev=x

for x in envl:
mailfl.write('%s: %s\n'%(x,env[x]))
mailfl.close()
 
D

Denis S. Otkidach

On 24 Mar 2005 05:20:06 -0800 (e-mail address removed) 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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top