#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    5
    Rep Power
    0

    emailing and.....


    Do someone know how i can make a .txt document and then write to it like:


    txtfile = open("hello.txt", "w")
    txtfile.write("Hello World\n")
    txtfile.close()

    But instead of write: "hello world" then write:

    test = raw_input ("")
    txtfile = open("hello.txt", "w")
    txtfile.write test
    txtfile.close()

    But this is not working...
    Then after i wont it to send the file to an other e-mail.
    Can someone help?

    WinXP
    py2exe

    Is what i use...


    Thanks

    Jomik1993
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    37
    Rep Power
    10
    In general, you should include any error messages you get to give folks more information into your problem. Here, though, I think your problem is in this line:

    txtfile.write test

    where you need to say:

    txtfile.write(test)

    Other than that, the following web page does a pretty good job talking about sending e-mail:

    e-mail info

    Hope this helps.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I wrote a simple email function for an article a while that you might be interested in. You can find out more the function here:

    http://www.devshed.com/c/a/Python/Python-on-the-Web/3/

    Code:
    #!/usr/bin/env python
    
    import smtplib
    
    def mail(address, subject, message, host = 'localhost'):
    
     headers = 'From: %srnTo: %srnSubject: %srnrn%s'
     message = headers % (address[0], ','.join(address[1:]), subject, message)
    
     server = smtplib.SMTP(host)
     server.sendmail(address[0], address[1:], message)
     server.quit()
    
    if __name__ == '__main__':
    
    mail(('someone@somewhere.com', 'sometwo@somewhere.com'), 'subject', 'message')
    Anyway assuming that you want to send the data in the file, rather than the file itself (as an attachment) it should be pretty straight forward; read() the data into your program and send that data as the message. If you do want to attach a file it's a little more difficult.

    In any case I would recommend that you check out the documentation for the email and smtplib modules for more information:

    http://www.python.org/doc/2.4/lib/module-smtplib.html
    http://www.python.org/doc/2.4/lib/module-email.html

    Hope this helps,

    Mark.

    Comments on this post

    • Yegg` agrees : Nice job.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    5
    Rep Power
    0
    thanks

IMN logo majestic logo threadwatch logo seochat tools logo