I have a script that sends an email via a mail server. The script only works when I call import smtplib
first in the interactive window. Otherwise, I get the following error:
ImportError: No module named MIMEMultipart
Can someone help me understand the underlying reason behind this behavior?
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
# Fill in the necessary blanks here
gmail_user = "<your user name>"
gmail_pwd = "<your password>"
def mail(to, subject, text):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
msg.attach(MIMEText(text))
mailServer =smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
mailServer.close()
mail("<recipient's email>",
"Hello from python!",
"This is an email sent with python")
Could it be that either your script is named "email.py" or that you have an "email.py" (or "email.pyc" etc) file in your current directory ?