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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tvmaly@gmail.com

    #1

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

    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('T o: '+recipient+nl)
    if fromaddr: mailfl.write('F rom: '+fromaddr+nl)
    mailfl.write('S ubject: %s%s\n\n'%(subj ect,uri))
    mailfl.write(in tro)
    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()

  • Denis S. Otkidach

    #2
    Re: need help with nullmailer-inject in python cgi script to sendattachement s ?

    On 24 Mar 2005 05:20:06 -0800 tvmaly@gmail.co m 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('T o: '+recipient+nl)
    TC> if fromaddr: mailfl.write('F rom: '+fromaddr+nl)
    TC> mailfl.write('S ubject: %s%s\n\n'%(subj ect,uri))
    TC> mailfl.write(in tro)
    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('appli cation', 'octet-stream')
    attachment.set_ payload(open(fi lename, 'rb').read())
    attachment.add_ header('Content-Disposition', 'attachment',
    filename=filena me)
    from email.Encoders import encode_base64
    encode_base64(a ttachment)
    msg.attach(atta chment)

    # 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.wr ite(msg.as_stri ng())
    mailfl.stdin.cl ose()
    mailfl.wait()

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

    Comment

    • tvmaly@gmail.com

      #3
      Re: need help with nullmailer-inject in python cgi script to send attachements ?

      Thank you Denis

      Ty

      Comment

      Working...