Getting error `No module named mime.text` [Python]

20,342

Solution 1

Rename the file you are working on named email.py to something else. It is likely breaking imports in your libraries.

Solution 2

This is what I did for gmail. Hope this sloves your problem

 from email.mime.text import MIMEText

    def construct_mesage():
      message = MIMEText(message_text)
      message['to'] = to
      message['from'] = sender
      message['subject'] = subject
      return {'raw': base64.urlsafe_b64encode(message.as_string())}
Share:
20,342
Dat Ha
Author by

Dat Ha

Studying aerospace engineering at Concordia University. Working on a liquid propellant rocket with Space Concordia's Rocketry Division. Most early questions on this account were pretty bad, to be honest. Big lack of research and understanding on my part, but we all learn and move on.

Updated on September 28, 2020

Comments

  • Dat Ha
    Dat Ha over 3 years

    This code is "suppose" to send an email.

    import smtplib
    #SERVER = "localhost"
    
    FROM = '[email protected]'
    
    TO = ["[email protected]"] # must be a list
    
    SUBJECT = "Hello!"
    
    TEXT = "This message was sent with Python's smtplib."
    
    # Prepare actual message
    
    message = """\
    From: %s
    To: %s
    Subject: %s
    
    %s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    
    # Send the mail
    
    server = smtplib.SMTP('myserver')
    server.sendmail(FROM, TO, message)
    server.quit()
    

    I get this error message.

    Traceback (most recent call last):
      File "C:\Users\myCpu\Documents\myFiles\python\test wy.py", line 1, in <module>
        import smtplib
      File "C:\Python27\lib\smtplib.py", line 46, in <module>
        import email.utils
      File "C:/Users/myCpu/Documents/myFiles/python\email.py", line 5, in <module>
    ImportError: No module named mime.text
    

    Using:

    Python 2.7

    Windows 7 Professionnal

    Gmail (@gmail.com)


    Can anyone help me with this code?

  • Dat Ha
    Dat Ha over 7 years
    Do I simply run this?? or is something else needed??
  • Januka samaranyake
    Januka samaranyake over 7 years
    This give you a raw object you can this to sever
  • Dat Ha
    Dat Ha over 7 years
    I get this now: No module named utils
  • Januka samaranyake
    Januka samaranyake over 7 years
  • Dat Ha
    Dat Ha over 7 years
    last thing... global name 'base64' is not defined
  • Januka samaranyake
    Januka samaranyake over 7 years
    "import base64"
  • Januka samaranyake
    Januka samaranyake over 7 years
  • Dat Ha
    Dat Ha over 7 years
    check messagess
  • Dat Ha
    Dat Ha over 7 years
    This is what i get:server = smtplib.SMTP('myserver') File "C:\Python27\lib\smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "C:\Python27\lib\smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Python27\lib\smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "C:\Python27\lib\socket.py", line 557, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno 11004] getaddrinfo failed
  • PythonWizard
    PythonWizard over 4 years
    I also got the same issue in Python 3.8. So that my solution is: from email.mime.text import MIMEText