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

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    It looks correct, but why does my script to send e-mail via gmail fail?


    Hi all,

    I feel like my code is correct, but clearly it's not because I get the following error:

    Code:
    Traceback (most recent call last):
      File "C:\Users\Tony\Desktop\email3.py", line 1, in <module>
        import smtplib
      File "C:\Python27\lib\smtplib.py", line 46, in <module>
        import email.utils
    ImportError: No module named utils
    What does that mean? Please see my code below.

    Thank you!

    Tony



    Code:
    import smtplib
    
    gmail_user = "myemail@gmail.com"
    gmail_pwd = "mypassword"
    FROM = 'myemail@gmail.com'
    TO = ['anotheremail@gmail.com'] #must be a list
    SUBJECT = "Testing sending using gmail"
    TEXT = "Testing sending mail using gmail servers"
    
    # Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        #server = smtplib.SMTP(SERVER) 
        server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        #server.quit()
        server.close()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,843
    Rep Power
    480
    I think it means you need to reinstall python.

    I've installed a whole slew of extra packages over the last few years, maybe my test is invalid, but

    import smtplib


    works for me in python versions 2 and 3.


    Another possibility is that you have modules with the same name in your current directory. And these modules are not the same as those you expect.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0
    Yea and I'm actually getting this error when trying to run another script:

    Code:
    Traceback (most recent call last):
      File "C:\Users\Tony\Desktop\test2.py", line 4, in <module>
        import requests
      File "C:\Python27\lib\requests\__init__.py", line 58, in <module>
        from . import utils
      File "C:\Python27\lib\requests\utils.py", line 24, in <module>
        from .compat import parse_http_list as _parse_list_header
      File "C:\Python27\lib\requests\compat.py", line 7, in <module>
        from .packages import charade as chardet
      File "C:\Python27\lib\requests\packages\__init__.py", line 3, in <module>
        from . import urllib3
      File "C:\Python27\lib\requests\packages\urllib3\__init__.py", line 16, in <mod
    ule>
        from .connectionpool import (
      File "C:\Python27\lib\requests\packages\urllib3\connectionpool.py", line 38, i
    n <module>
        from .request import RequestMethods
      File "C:\Python27\lib\requests\packages\urllib3\request.py", line 12, in <modu
    le>
        from .filepost import encode_multipart_formdata
      File "C:\Python27\lib\requests\packages\urllib3\filepost.py", line 15, in <mod
    ule>
        from .fields import RequestField
      File "C:\Python27\lib\requests\packages\urllib3\fields.py", line 7, in <module
    >
        import email.utils
    ImportError: No module named utils
    I will try to reinstall and report back!

IMN logo majestic logo threadwatch logo seochat tools logo