---
date: 2022-02-20
title: Python 实现自动提交请求并发送结果到邮箱（可用于打卡签到）
cover: https://tse3-mm.cn.bing.net/th/id/OIP-C.fFN24vUFQL7MPjHg-Yy5pwHaC9?pid=ImgDet&rs=1
category: experience
recommend: true
location: 甘肃
tags:

- Python
- 打卡
- 签到

---

女朋友签的自主实习，抽空写了一个 `Python` 自动签到并且将签到结果发送到指定邮箱的简易脚本，发出来收藏备用

``` python
import requests
import json
import smtplib
from email.mime.text import MIMEText

url = 'https://www.baidu.com'
data = {
    'test':'test'
}
r = requests.post(url, data)
res = json.loads(r.text)

mail_config = {
    'mail_user': 'xxx@qq.com',
    'mail_pass': 'xxxx',
    'mail_receivers': 'sanqi377@outlook.com'
}

message = MIMEText(res['msg'], 'plain', 'utf-8')
# 邮件主题
message['Subject'] = '今日实习打卡结果'
# 发送方信息
message['From'] = mail_config['mail_user']
# 接受方信息
message['To'] = mail_config['mail_receivers']

try:
    smtpObj = smtplib.SMTP()
    # 连接到服务器
    smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登录到服务器
    smtpObj.login(mail_config['mail_user'], mail_config['mail_pass'])
    # 发送
    smtpObj.sendmail(
        mail_config['mail_user'], mail_config['mail_receivers'], message.as_string())
    # 退出
    smtpObj.quit()
    print('success')
except smtplib.SMTPException as e:
    print('error', e)  # 打印错误

```
