Python unresolved reference import MIMEMultipart - python

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

Related

Email with no attachment is showing paperclip

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

Send mail using python to required recipients

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.

Invalid syntax for Python MIMETEXT

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.

Error while sending Mail with attachement in Python

import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
os.chdir(path)
def send_mail(send_from,send_to,subject,text,files,server,port,username='',password='',isTls=True):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = 'Test'
msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open("Olaf.xlsx", "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment;
filename="Olaf.xlsx"')
msg.attach(part)
smtp = smtplib.SMTP('smtp.web.de', 587)
smtp.starttls()
smtp.login('test#test.de', 'pw')
At this part the error occurs: NameError: name 'msg' is not defined. But whats wrong?
Here is where I got the code from: add excel file attachment when sending python email
smtp.sendmail('xy','xyz', msg.as_string())
smtp.quit()
You can try the below code:
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from email.mime.text import MIMEText
emaillist=['to#gmail.com'] # Receipient email address
msg = MIMEMultipart('mixed')
msg['Subject'] = 'Arrest Warrent' # mail subject line
msg['From'] = 'xyz#gmail.com' # From email address
msg['To'] = ', '.join(emaillist)
part = MIMEBase('application', "octet-stream")
# Provide the path of the file to be attached in the mail
part.set_payload(open('C:'+os.sep+'Users'+os.sep+'abhijit'+os.sep+'Desktop'+os.sep+'WarrentDetails.txt', "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="WarrentDetails.txt"')
msg.attach(part)
msg.add_header('To', msg['From'])
text = "Dear Sir, \n\n An arrest warrent has been generated due to XYZ reason by ZZZ complain.\n YOU MUST APPEAR IN PERSON TO RESOLVE THIS MATTER. \n\n Regards,\n FBI :)"
part1 = MIMEText(text, 'plain')
msg.attach(part1)
# provide SMTP details of the host and its port number
server = smtplib.SMTP_SSL("smtp.gmail.com",465)
# If the host supports TLS then enable the below 2 lines of code
# server.ehlo()
# server.starttls()
server.login("xyz#gmail.com", "password")
server.sendmail(msg['From'], emaillist , msg.as_string())
More details on this can be found at this blog.

Permission Denied error while sending mail using Python smtplib

I am trying to send mail using Python 3.2. My code is as follows:
#from email import Encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
import os
import smtplib
from base64 import encode
from email import encoders
def sendMail(to, subject, text, files=[],server="smtp.mydomain.com"):
assert type(to)==list
assert type(files)==list
fro = "From <myemail#mydomain.com>"
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP_SSL(server, 465)
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.ehlo()
smtp.login("myemail#mydomain.com", "mypassword")
smtp.ehlo()
smtp.sendmail(fro, to, msg.as_string() )
smtp.close()
print("Email send successfully.")
sendMail(
["recipient"],
"hello","cheers",
[]
)
It gives me following error:
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'5.7.1 <myemail#mydomain.com>... Permission denied', 'myemail#mydomain.com')
Does anybody know how to solve this problem?
Thanks in advance.
As the error says: you need to call the connect method on the smtplib.SMTP_SSL instance before you try to use it. smtplib.SMTP_SSL does not automatically connect (and neither does smtplib.SMTP.)

Categories

Resources