I am trying to send an email via python with gmail as the host, as follows:
# Import smtplib for the actual sending function
import smtplib
# For guessing MIME type
import mimetypes
# Import the email modules we'll need
import email
import email.mime.application
# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Greetings'
msg['From'] = 'x@gmail.com'
msg['To'] = 'x@gmail.com'
# The main body is just another attachment
body = email.mime.Text.MIMEText("""Hello, how are you? I am fine.
This is a rather nice letter, don't you think?""")
msg.attach(body)
# PDF attachment
filename=r'C:\Users\cost9\OneDrive\Documents\PYTHON\TEST0530\testingemail.xlsx'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="xlsx")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)
# send via Gmail server
# NOTE: my ISP, Centurylink, seems to be automatically rewriting
# port 25 packets to be port 587 and it is trashing port 587 packets.
# So, I use the default port 25, but I authenticate.
s = smtplib.SMTP("smtp.gmail.com")
s.starttls()
s.login('x@gmail.com','password')
s.sendmail('x@gmail.com',['x@gmail.com'], msg.as_string())
s.quit()
Unfortunately I get the error after about 20 seconds of calculating:
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I've turned off gmail's security settings I think so that it will go through, but that does't work either. I am trying to send an excel (xlsx) attachment with the email to myself. Not sure what's wrong.
edit: New error after the below suggestion:
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbud\n5.7.14 Ptkk7FoIWgMYWUepxcLLdzPhOBbY5hJDbghJm9AqpU61dWd3pVjuxVaNzkiIyiT0gpijJl\n5.7.14 PQU08uhdkGEPOoo2LatNeL-W0_IiJi3GzdhBBd57yr77BxAYfdY6qMF4CtxW1UmbborYgl\n5.7.14 6Az7m2-ULUcjo96qXX31S2wKGN-XWcmd3F-SagzxJax-8v-KoloZlN1BBQM4ATPikNnlB9\n5.7.14 UpPRBSGiG8fE_mi4d-y345I8EJEJU> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 93sm7663257iod.17 - gsmtp')