I just want to send an email in python with an attachment
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.login('[email protected]','fu')
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
ATTACHMENTS = ['/tmp/2013-11-04-test.csv']
send_from=['[email protected]']
send_to=['[email protected]']
subject='adfadfadf'
text = 'adfadfadf'
send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)
How do I auth? I have to provide a username and password. How?
Traceback (most recent call last):
File "/home/ubuntu/workspace/miliza-devops/classes/utilities.py", line 133, in <module>
send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)
File "/home/ubuntu/workspace/miliza-devops/classes/utilities.py", line 124, in send_mail
smtp.login('[email protected]','fu')
File "/usr/lib/python2.7/smtplib.py", line 576, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
SMTP Authentication, often abbreviated SMTP AUTH, is an extension of the Simple Mail Transfer Protocol (SMTP) whereby a client may log in using any authentication mechanism supported by the server. It is mainly used by submission servers, where authentication is mandatory.
smtplib is Python's built-in module for sending emails to any Internet machine with an SMTP or ESMTP listener daemon. I'll show you how to use SMTP_SSL() first, as it instantiates a connection that is secure from the outset and is slightly more concise than the . starttls() alternative.
You need a call to starttls()
before you login:
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.starttls()
smtp.login('[email protected]', 'fu')
Also, your send_from
should be a str
, not a list
:
send_from='[email protected]'
Note that smtp.starttls()
calls smtp.ehlo()
implicitly:
If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first. https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.starttls
On gmail you also have to give a smtp.ehlo()
before smtp.starttls()
This is also duplicate of How to send an email with Gmail as provider using Python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With