Python向多人发送、抄送带附件的邮件(含详细代码)

Python向多人发送、抄送带附件的邮件(含详细代码),第1张

python要发送档知销带附件邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后使用smtplib.smtp发送。

步骤:

(1)设置服务器所需信息(ps:部门邮行游箱密码为授权码,需自行登猛帆录相应邮箱设置授权码)

(2)设置email信息

(3)附件部分

(4)登录邮箱并发送邮件

附上源码:

1、发送邮件:

import zmail

server = zmail.server(' yourmail@example.com ’, 'yourpassword')

server.send_mail(' yourfriend@example.com ',{'subject':'Hello!','content_text':'By zmail.'})

server.send_mail([' friend1@example.com ',' friend2@example.com '],{'subject':'Hello!','content_text':'By zmail.'})

2、接收最后一封邮件:

import zmail

server = zmail.server(' yourmail@example.com ’, 'yourpassword')

latest_mail = server.get_latest()

zmail.show(latest_mail)

3、发雀困送带附件的邮件:

import zmail

mail = {

'subject': 'Success!', # Anything you want.

'content_text': 'This message from zmail!', # Anything you want.

'attachments': ['/Users/zyh/Documents/example.zip','/root/1.jpg'], # Absolute path will be better.

}

server = zmail.server(' yourmail@example.com ’, 'yourpassword')

server.send_mail(' yourfriend@example.com ', mail)

server.send_mail([' yourfriend@example.com ','顷闭念 12345@example.com '], mail)

4、发送html格式邮件:

with open('/Users/example.html','r') as f:

content_html = f.read()

mail = {

'subject': 'Success!', # Anything you want.

'content_html': content_html,

'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.

}

server.send_mail(' yourfriend@example.com ',mail)

5、使用抄送:

server.send_mail([' foo@163.com ',' foo@126.com '],mail,cc=[' bar@163.com '])

6、自定义server

server = zmail.server('username','password',smtp_host='smtp.163.com',smtp_port=994,smtp_ssl=True,pop_host='pop.163.com',pop_port=995,pop_tls=True)

7、根据ID取回邮件:mail = server.get_mail(2)

根据日期、主题、发送人取回邮件:

mail = server.get_mails(subject='态梁GitHub',after='2018-1-1',sender='github')

mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)

8、查看邮箱统计

mailbox_info = server.stat() #结果为包含两个整型的元组: (邮件的数量, 邮箱的大小).

9、删除邮件:MailServer.delete(which)

10、保存附件:zmail.save_attachment(mail,target_path=None,overwrite=False)

11、保存邮件:zmail.save(mail,name=None,target_path=None,overwrite=False)

12、读取邮件:zmail.read(file_path,SEP=b'\r\n')

支持的列表:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8231224.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存