小编soc*_*yer的帖子

发送python电子邮件时添加excel文件附件

如何使用python发送电子邮件时添加文档附件?我收到要发送的电子邮件(请忽略:我正在循环发送电子邮件每5秒发送一次,仅用于测试目的,我希望它每隔30分钟发送一次,只需要更改5到1800)

到目前为止,这是我的代码.如何从我的电脑附加文件?

#!/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 = '\n'.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()
Run Code Online (Sandbox Code Playgroud)

python email document attachment

14
推荐指数
3
解决办法
4万
查看次数

标签 统计

attachment ×1

document ×1

email ×1

python ×1