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

Python013

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

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

步骤:

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

(2)设置email信息

(3)附件部分

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

附上源码:

1、发送邮件:

import zmail

server = zmail.server(' [email protected] ’, 'yourpassword')

server.send_mail(' [email protected] ',{'subject':'Hello!','content_text':'By zmail.'})

server.send_mail([' [email protected] ',' [email protected] '],{'subject':'Hello!','content_text':'By zmail.'})

2、接收最后一封邮件:

import zmail

server = zmail.server(' [email protected] ’, '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(' [email protected] ’, 'yourpassword')

server.send_mail(' [email protected] ', mail)

server.send_mail([' [email protected] ',' [email protected] '], 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(' [email protected] ',mail)

5、使用抄送:

server.send_mail([' [email protected] ',' [email protected] '],mail,cc=[' [email protected] '])

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')

支持的列表: