How to send mails using python (SMTP)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skris
    New Member
    • Feb 2008
    • 2

    How to send mails using python (SMTP)?

    Hi,
    I started learning python a month ago.My problem now is, I need to send a
    mail to my gmail account using a python script.Can this be done? I did try
    out a lot of python codes but none of them really worked. Please help me
    out. Also kindly mention the names of some good python books for a beginner.
  • Subsciber123
    New Member
    • Nov 2006
    • 87

    #2
    Dive into Python should be a good book. http://www.diveintopyt hon.org/

    Comment

    • Elias Alhanatis
      New Member
      • Aug 2007
      • 56

      #3
      Hello!

      I think this code is what you need.Adjust it to your needs.....
      Sorry , but i dont remember where i got it from......

      Code:
      import smtplib
      
      smtpserver = 'foo.foo.foo'
      AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
      smtpuser = 'spam'  # for SMTP AUTH, set SMTP username here
      smtppass = 'egs'  # for SMTP AUTH, set SMTP password here
      
      RECIPIENTS = ['arecipient@somewhere.com']
      SENDER = 'youremail@somewhere.com'
      mssg = "The Message"
      
      session = smtplib.SMTP(smtpserver)
      if AUTHREQUIRED:
          session.login(smtpuser, smtppass)
      smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
      
      if smtpresult:
          errstr = ""
          for recip in smtpresult.keys():
              errstr = """Could not delivery mail to: %s
              Server said: %s %s %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
          raise smtplib.SMTPException, errstr
      Elias

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        You can try searching bytes.com for the phrase "Python SMTP" for older posts on this topic. Here's the most recent.

        HTH

        Comment

        Working...