python * development 1st —— use python to send mail and caputre the screen then combine them

时间:2021-04-19 08:40:18
 import smtplib
 from email.mime.text import MIMEText
 msg_from='1@qq.com'                                 #发送方邮箱
 passwd='bd'                                   #填入发送方邮箱的授权码
 msg_to='1@qq.com'

 subject="python邮件测试"                                     #主题
 content="这是我使用python smtplib及email模块发送的邮件"
 msg = MIMEText(content)
 msg['Subject'] = subject
 msg['From'] = msg_from
 msg['To'] = msg_to
 try:
     s = smtplib.SMTP_SSL("smtp.qq.com",465)
     s.login(msg_from, passwd)
     s.sendmail(msg_from, msg_to, msg.as_string())
     print( "发送成功")
 except:
     print("发送失败")
 finally:
     s.quit()

以上是文本类型邮件的发送,不适用图片邮件的发送

以下是图片类型的邮件发送

 import smtplib
 from email.mime.text import MIMEText
 from PIL import ImageGrab
 import random
 from time import *
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.mime.image import MIMEImage

 def send():
     sender = '1@qq.com'
     receivers = '17@qq.com'
     message =  MIMEMultipart('related')
     subject = '终于能发图片了'
     message['Subject'] = subject
     message['From'] = sender
     message['To'] = receivers
     content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>','html','utf-8')
     message.attach(content)

     file=open("F:\\12.png", "rb")
     img_data = file.read()
     file.close()

     img = MIMEImage(img_data)
     img.add_header('Content-ID', 'imageid')
     message.attach(img)

     try:
         server=smtplib.SMTP_SSL("smtp.qq.com",465)
         server.login(sender,"i")
         server.sendmail(sender,receivers,message.as_string())
         server.quit()
         print ("邮件发送成功")
     except smtplib.SMTPException as e:
         print(e)
 send()

之后我们继续学习截屏幕保存

 from PIL import ImageGrab
 import random
 from time import *

 strings = "qwertyuiopasdfghjkzcvbnm0123456789[].,/?"
 lenght  = len(strings)
 name = ""

 for j in range(1,3):
     for i in range(1,5):
         r = random.randint(1,36)
         name+=strings[r]
     im = ImageGrab.grab()
     im.save('F:\\'+name+'.png')
     name = ""

我们尝试将他们结合起来:

 import smtplib
 from email.mime.text import MIMEText
 from PIL import ImageGrab
 import random
 from time import *
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.mime.image import MIMEImage

 def send():
     im = ImageGrab.grab()
     im.save('F:\\test.png')
     sender = '@qq.com'
     receivers = '@qq.com'
     message =  MIMEMultipart('related')
     subject = '终于能发图片了'
     message['Subject'] = subject
     message['From'] = sender
     message['To'] = receivers
     content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>','html','utf-8')
     message.attach(content)

     file=open("F:\\test.png", "rb")
     img_data = file.read()
     file.close()

     img = MIMEImage(img_data)
     img.add_header('Content-ID', 'imageid')
     message.attach(img)

     try:
         server=smtplib.SMTP_SSL("smtp.qq.com",465)
         server.login(sender,"")
         server.sendmail(sender,receivers,message.as_string())
         server.quit()
         print ("邮件发送成功")
     except smtplib.SMTPException as e:
         print(e)
 send()

欧克,成功,我们打包试试看

python * development 1st —— use python to send mail and caputre the screen then combine them