Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 625
Dat Ha Avatar asked Nov 29 '22 09:11

Dat Ha


2 Answers

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())}
like image 166
Januka samaranyake Avatar answered Dec 04 '22 23:12

Januka samaranyake


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

like image 42
BmoreAGG Avatar answered Dec 05 '22 00:12

BmoreAGG