I am trying to send email automatically from my official ID to another official ID. For that I have used following python script.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "<SENDER MAIL ID>"
toaddr = "<RECIPIENT MAIL ID>"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "Robot Uprising, We are coming for you"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
While executing this I am facing issue as follows:
Traceback (most recent call last):
File "mailsendtest.py", line 2, in <module>
from email.MIMEMultipart import MIMEMultipart
ImportError: No module named MIMEMultipart
I am a starter in python. Kindly provide inputs. Thanks !
I find two problems with your imports assuming you're using Python3.
Change from email.MIMEMultipart import MIMEMultipart to from email.mime.multipart import MIMEMultipart
Change from email.MIMEText import MIMEText to from email.mime.text import MIMEText
Also make sure you have the libraries installed if these don't work.
Related
I am using Python to send a simple HTML email and have the basic code:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
def send_test_mail(body):
sender_email = <SenderEmail>
receiver_email = <ReceiverEmail>
msg = MIMEMultipart()
msg['Subject'] = '[Email Test]'
msg['From'] = sender_email
msg['To'] = receiver_email
msgText = MIMEText('<b>%s</b>' % (body), 'html')
msg.attach(msgText)
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 587) as smtpObj:
smtpObj.ehlo()
smtpObj.login(<LoginUserName>, <LoginPassword>)
smtpObj.sendmail(sender_email, receiver_email, msg.as_string())
except Exception as e:
print(e)
if __name__ == "__main__":
send_test_mail("Welcome to Medium!")
This has been put together from a couple of sources from within the stackexchange community but the intention is only to send a straightforward email.
But, when it is received, I get the paperclip icon indicating there is an attachment when there clearly isn't one.
https://i.stack.imgur.com/Ysj3g.png
Python is not my strong point but I'm sure it has more to do with SMTPLIB rather than python.
Does anyone know what is going on here?
Thanks
I followed a tutorial in youtube for sending email in python using outlook, however I have received an error when running the python file.
This is my python script :
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
sender_email_address = 'nmahirahaqilah.awahab#gmail.com'
sender_email_password = 'mypassword'
receiver_email_address = 'mahirah.abd.wahab#ericsson.com'
email_subject_line = 'Sample python email'
msg = MIMEMultipart()
msg['From'] = sender_email_address
msg['To'] = receiver_email_address
msg['Subject'] = email_subject_line
email_body = 'Hello World'
msg.attach(MIMEText(email_body,'plain'))
email_content = msg.as_string()
server = smtplib.SMTP('smtp.mail.outlook.com:587')
serverstarttls()
server.login(sender_email_address,sender_email_password)
server.sendmail(sender_email_address,receiver_email_address,email_content)
server.quit()
This is the error that I am receiving, not quite sure why:
C:\Users\Azril\Desktop>python email.py
File "email.py", line 3
from email.mime.text
^
SyntaxError: invalid syntax
Appreciate your help on this.
I have a script as follow
#!/usr/bin/python
import smtplib
import datetime
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
now = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
fromaddr = "xxx#gmail.com"
toaddr = "yyy#live.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Device Start Up Notification"
body = "Your device is started up %s " % now
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "abc12345")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
When i execute this file, it shows error
Traceback (most recent call last):
File "./email.py", line 2, in <module>
import smtplib
File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "/home/jypang/email.py", line 4, in <module>
from email.MIMEMultipart import MIMEMultipart
ImportError: No module named MIMEMultipart
The library is installed because I have another script which same as the script above (importing same library) and it's working well.
Please assist, thank you!
You named your module email which conflicts with standard library module email; stmplib module depends on the standard library module email.
You need to rename email with another name. If there's email.pyc, make sure delete it.
I am writing some code in Pycharm to send my Selenium test result report in an email with an attachment.
In my import statement I am getting the error:
unresolved reference MIMEMultipart
unresolved reference MIMEText
unresolved reference MIMEBase
My import statement is written like this:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
Do I need to install any packages?
My full code snippet is:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
class Email(BasePage):
def send_email(self):
import smtplib
from email import encoders
fromaddr = "YOUR EMAIL"
toaddr = "EMAIL ADDRESS YOU SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"
body = "TEXT YOU WANT TO SEND"
msg.attach(MIMEText(body, 'plain'))
filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
With python 3.6:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
The email package was refactored at some point and the Mime support is now in the email.mime package.
https://docs.python.org/2/library/email.html
I'm trying send an attachment with an e-mail using following code. But it gives an error. Without the attachment it works perfectly. What is the problem with this code?
"mail5.py", line 14
smtpObj = smtplib.SMTP('domain', 25)
^
SyntaxError: invalid syntax
#!/usr/bin/python
import smtplib
sender = 'a#a.com'
receivers = ['b#b.com']
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIMEText(file("text.txt").read())
smtpObj = smtplib.SMTP('domain', 25)
smtpObj.sendmail(sender, receivers, msg.as_string())
print "Successfully sent email"
It looks like you're missing a closing parenthesis on the preceding line, try this:
msg.attach(MIMEText(file("text.txt").read()))