How do i add a document attachment when sending an email with python ?
i get the email to send
(please ignore: i am looping the email to send every 5 seconds, only for testing purposes, i want it to send every 30 min, just have to change 5 to 1800)
here is my code so far. how do i attach a document from my computer?
#!/usr/bin/python
import time
import smtplib
while True:
TO = 'xxxx@gmail.com'
SUBJECT = 'Python Email'
TEXT = 'Here is the message'
gmail_sender = 'xxxx@gmail.com'
gmail_passwd = 'xxxx'
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(gmail_sender, gmail_passwd)
BODY = '
'.join([
'To: %s' % TO,
'From: %s' % gmail_sender,
'Subject:%s' % SUBJECT,
'',
TEXT
])
try:
server.sendmail(gmail_sender,[TO], BODY)
print 'email sent'
except:
print 'error sending mail'
time.sleep(5)
server.quit()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…