python对邮件的常用操作收邮件发邮件
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header host_server = 'smtp.163.com' send_sina = 'xx@163.com' pwd = 'xxx' send_sina_mail = 'xx@163.com' receiver = 'xx@qq.com' mail_title = 'python办公自动化的邮件' mail_content = '你好,这是使用python登陆sina邮箱发送邮件的测试' msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = send_sina_mail msg['To'] = Header("测试邮箱", 'utf-8') msg.attach(MIMEText(mail_content, 'plain', 'utf-8')) # 邮件正文内容 smtp = SMTP_SSL(host_server) # ssl登陆 smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string())
网页版邮件
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header host_server = 'smtp.163.com' send_sina = 'aa@163.com' pwd = 'pass' send_sina_mail = 'aa@163.com' receiver = 'bb@qq.com' mail_title = 'python办公自动化的邮件' mail_content = '你好,<p>这是使用python登陆sina邮箱发送邮件的测试</p>' \ '<a href="https://www.python.org">Python</a>' # 邮件正文内容 msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = send_sina_mail msg['To'] = Header("测试邮箱", 'utf-8') msg.attach(MIMEText(mail_content, 'html', 'utf-8')) # 邮件正文内容 try: smtp = SMTP_SSL(host_server) # ssl登陆 smtp.set_debuglevel(1) # 开启debug smtp.ehlo(host_server) smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string()) smtp.quit() print("邮件发送成功") except smtplib.SMTPException: print("邮件无法发送")
发送带附件的邮件
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header host_server = 'smtp.163.com' send_sina = 'aa@163.com' pwd = 'pass' send_sina_mail = 'aa@163.com' receiver = 'bb@qq.com' mail_title = 'python办公自动化的邮件' mail_content = '你好,<p>这是使用python登陆sina邮箱发送邮件的测试</p>' \ '<a href="https://www.python.org">Python</a>' # 邮件正文内容 msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = send_sina_mail msg['To'] = Header("测试邮箱", 'utf-8') msg.attach(MIMEText(mail_content, 'html', 'utf-8')) # 邮件正文内容 attachment = MIMEApplication(open("d:/peisong.xls", "rb").read()) attachment.add_header('Content-Disposition', 'attachment', filename='peisong.xlsx') msg.attach(attachment) try: smtp = SMTP_SSL(host_server) # ssl登陆 smtp.set_debuglevel(1) # 开启debug smtp.ehlo(host_server) smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string()) smtp.quit() print("邮件发送成功") except smtplib.SMTPException: print("邮件无法发送")
收取邮件
import zmail server = zmail.server("aa@163.com", "pass") mail = server.get_latest() # zmail.show(mail) print(mail['subject']) print(mail['id']) print(mail['from']) print(mail['to']) print(mail['content_text']) print(mail['content_html']) # 查看附件 zmail.save_attachment(mail, target_path=None, overwrite=True)
邮件监控
import zmail from tkinter import * import tkinter.messagebox server = zmail.server('aa@163.com', 'pass') mail = server.get_latest() new_id = mail['id'] file_read = open('id.txt', 'r') old_id = file_read.readline() file_write = open('id.txt', 'w') file_write.write(str(new_id)) file_write.close() if old_id != str(new_id): main_window = Tk() main_window.withdraw() tkinter.messagebox.showinfo("你有邮件了", '老铁,你的邮件来了,瞅一眼吧\n,邮件标题:%s' % mail['subject'])
发送邮件脚本示例
# -*- coding:utf-8 -*- # # 通过阿里云邮件推送服务的api测试 # 主要是给网红用的 # # import smtplib import email from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.base import MIMEBase from email.mime.application import MIMEApplication from email.header import Header # 发件人地址,通过控制台创建的发件人地址 username = 'admin@service.chinasoft.com' # 发件人密码,通过控制台创建的发件人密码 password = 'password' # 自定义的回复地址 replyto = 'admin@service.chinasoft.com' # 收件人地址或是地址列表,支持多个收件人,最多30个 #receivers = ['xxx@alx.com', 'xxx@xx.com'] receivers = ['jack@chinasoft.cn',] #rcptto = ','.join(rcptto) rcptto = '***' # 构建alternative结构 msg = MIMEMultipart('alternative') msg['Subject'] = Header('2021 service.ws.com'.decode('utf-8')).encode() msg['From'] = '%s <%s>' % (Header('admin@service.chinasoft.com'.decode('utf-8')).encode(), username) msg['To'] = rcptto msg['Reply-to'] = replyto msg['Message-id'] = email.utils.make_msgid() msg['Date'] = email.utils.formatdate() # 构建alternative的text/plain部分 textplain = MIMEText('2021 15:16', _subtype='plain', _charset='UTF-8') msg.attach(textplain) # 构建alternative的text/html部分 texthtml = MIMEText('2020 15:16', _subtype='html', _charset='UTF-8') msg.attach(texthtml) # 发送邮件 try: client = smtplib.SMTP() #python 2.7以上版本,若需要使用SSL,可以这样创建client #client = smtplib.SMTP_SSL() #SMTP普通端口为25或80 client.connect('mail.service.chinasoft.com', 25) #开启DEBUG模式 client.set_debuglevel(1) client.login(username, password) #发件人和认证地址必须一致 #备注:若想取到DATA命令返回值,可参考smtplib的sendmaili封装方法: # 使用SMTP.mail/SMTP.rcpt/SMTP.data方法 #client.sendmail(username, rcptto, msg.as_string()) #支持多个收件人 client.sendmail(username, receivers, msg.as_string()) client.quit() print '邮件发送成功!' except smtplib.SMTPConnectError, e: print 'send mail fail, conn error:', e.smtp_code, e.smtp_error except smtplib.SMTPAuthenticationError, e: print 'send mail fail, renzheng fail:', e.smtp_code, e.smtp_error except smtplib.SMTPSenderRefused, e: print 'fail recive account reject', e.smtp_code, e.smtp_error except smtplib.SMTPRecipientsRefused, e: print 'fail recive account reject:', e.smtp_code, e.smtp_error print e except smtplib.SMTPDataError, e: print 'data recive error:', e.smtp_code, e.smtp_error except smtplib.SMTPException, e: print 'send fail, ', e.message except Exception, e: print 'except, ', str(e)