16 python learning Notes - Send e-mail

It will be sent after we finish the automated scripts to generate test report mail to the needs of the relevant personnel, python send mail smtplib have native email and libraries, but add an attachment when the need to set up their own MIME, at least there will be a dozen lines of code , is not easy, this chapter describes how to send mail in python to use more convenient yagmail library.

A, yagmail installation

Direct can be installed using pip install yagmail

Second, before e-mail mailbox configuration ready

Before writing code to set the e-mail mailbox, open the SMTP service, to obtain an authorization code. yagmail module password to use when sending mail is the authorization code, rather than mail password we usually use.

Qq-mail with an example: In Settings - Accounts authorization code --- ---- --- SMTP service to send SMS settings authorization code, authorization code to use this time to send a message on it

 

 163 E-mail acquisition process is similar authorization code, the client authorization password provided --- --- --- Open --- authorization code arranged to send SMS authorization code

 

 Third, automatically send mail module yagmail

 

 1 import yagmail
 2 
 3 username = '123456@163.com'#邮箱账号
 4 passwd = '123456789'#授权码,不是邮箱密码
 5 smtp = yagmail.SMTP(user=username,
 6                     password=passwd,
 7                     host='smtp.163.com',#其他服务器就smtp.qq.com  smtp.126.com
 8                     # smtp_ssl=True
 9                     ) #如果用的是qq邮箱或者你们公司的邮箱使用是安全协议的话,必须写上 smtp_ssl=True
10 smtp.send(
11     #如果多个收件人的话,写成list就行了,如果只是一个账号,就直接写字符串就行to='[email protected]'
12     to=['[email protected]','[email protected]'],
13     cc='[email protected]',#抄送
14     subject='发送邮件的标题',#邮件标题
15     contents='你好,你今天开心吗?',#邮件正文
16     attachments=[r'D:\python\作业\作业.txt',#附件如果只有一个的话,用字符串就行,attachments=r'C:\\pp\\b.txt'
17                  r'D:\python\作业\造日志的脚本.py'])
18 print('发送成功')

 

 

 

Guess you like

Origin www.cnblogs.com/cocomoly/p/11943701.html