By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

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

P: n/a
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()

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 24 Mar 2005 05:20:06 -0800 tv****@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]
Jul 18 '05 #2

P: n/a
Thank you Denis

Ty

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.