SMTP
正如 HTTP 是计算机用来通过因特网发送网页的协议,简单邮件传输协议(SMTP)
是用于发送电子邮件的协议
import smtplib
发送电子邮件
连接到SMTP服务器
smtpObj = smtplib.SMTP_SSL('pop.exmail.qq.com',465)
type(smtpObj)
发送SMTP的"hello"消息
smtpObj.ehlo() #返回250 means 成功
开始TLS加密
TLS 加密需要使用.starttls()方法
SSL 加密则不需要使用
登录到SMTP服务器
smtpObj.login('7379@jics.cn',input()) #返回235 means 成功 input由用户输入邮箱密码
发送电子邮件
smtpObj.sendmail("7379@jics.cn","evian_jeff@163.com",'Subject:So long.
Dear Alice,so long and thanks for all the fish. Sincerely,Bob')
#from发件邮箱 to收件邮箱 'Subject:
'开头的邮件标题行和正文字符串
#返回空字典means成功
从SMTP服务器断开
smtpObj.quit() #221 means bye
IMAP
- imaplib python自带模块
- imapclient 第三方包 pip install imapclient
- pyzmail 解析邮件内容 pip install pyzmail36
# pip install imapclient 安装imapclient
import imapclient
用IMAP获取和删除电子邮件
# pip install pyzmail36
import pyzmail #需要需改util.py文件
连接到IMAP服务器
import imapclient
imapObj = imapclient.IMAPClient('imap.exmail.qq.com', ssl=True)
登录到IMAP服务器
imapObj.login('7379@jics.cn', input())
搜索电子邮件
登录后,实际获取你感兴趣的电子邮件分为两步。首先,必须选择要搜索的文件
夹。然后,必须调用 IMAPClient 对象的 search()方法,传入 IMAP 搜索关键词字符串。
选择文件夹
import pprint
pprint.pprint(imapObj.list_folders())
imapObj.select_folder('INBOX', readonly=True) #除非你想删除的电子邮件,否则将 readonly 设置为 True总是个好主意
执行搜索
UIDs = imapObj.search(['SINCE 27-Aug-2019'])
UIDs
大小限制
import imaplib
imaplib._MAXLINE = 10000000
取邮件并标记为已读
rawMessages = imapObj.fetch(UIDs,['BODY[]'])
import pprint
pprint.pprint(rawMessages)
从原始消息中获取电子邮件地址
import pyzmail
message = pyzmail.PyzMessage.factory(rawMessages[4][b'BODY[]'])
message.get_subject() #查看主题
message.get_address('from') #每个元组包含两个字符串:第一个是与该电子邮件地址关联的名称,第二个是电子邮件地址本身
message.get_address('to')
message.get_addresses('cc')
message.get_addresses('bcc')
从原始消息中获取正文
message.text_part != True
message.text_part.get_payload().decode(message.text_part.charset) #'NoneType' object has no attribute 'get_payload'
message.html_part != None
message.html_part.get_payload().decode(message.html_part.charset)
删除电子邮件
imapObj.select_folder('INBOX', readonly=False)
UIDs = imapObj.search(['ON 26-AUG-2019'])
UIDs
imapObj.delete_messages(UIDs)
imapObj.expunge()
pprint.pprint(imapObj.list_folders()) #INBOX收件箱 Sent Messages发件箱 Drafts草稿箱 Deleted Messages已删除 Junk垃圾箱
从IMAP服务器断开
imapObj.logout()
项目:向会员发送会费提醒电子邮件
打开Excel文件
import openpyxl,smtplib,sys
# Open the spreadsheet and get the latest dues status.
wb = openpyxl.load_workbook(r"C:UsersAdministratorautomate_online-materialsduesRecords.xlsx")
sheet = wb.active
lastCol = sheet.max_column
latestMonth = sheet.cell(row=1,column=lastCol).value
查找所有未付成员
# TODO: Check each member's payment status.
unpaidMembers = {}
for r in range(2,sheet.max_row+1):
payment = sheet.cell(row=r,column=lastCol).value
if payment != 'paid':
name = sheet.cell(row=r,column=1).value
email = sheet.cell(row=r,column=2).value
unpaidMembers[name] = email
发送定制的电子邮件提醒
# TODO: Log in to email account.
smtpObj = smtplib.SMTP_SSL('pop.exmail.qq.com',465)
smtpObj.ehlo()
smtpObj.login("7379@jics.cn",input())
# TODO: Send out reminder emails.
for name,email in unpaidMembers.items():
body = '''Subject: %s dues unpaid.
Dear %s,
Record show that you have not paid dues for %s.
Please make this payment as soon as possible. Thank you!''' % (latestMonth,name,latestMonth)
print('Sending email to %s ... '% email)
sendmailStatus = smtpObj.sendmail('7379@jics.cn',email,body)
if sendmailStatus != {}:
print("There was a problem sending email to %s:%s") % (email,sendmailStatus)
smtpObj.quit()