Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send email sample #17

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/send_mail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: 上班早八发邮件

on:
schedule:
# 此处是UTC时间,对应北京时间早八点
- cron : '00 00 * * *'
workflow_dispatch:

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run send email notification script
run: |
python send_mail_daily.py
env:
SENDER: ${{ secrets.SENDER }}
RECEIVER: ${{ secrets.RECEIVER }}
PASSWORD: ${{ secrets.PASSWORD }}
54 changes: 54 additions & 0 deletions send_mail_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os


# 注意,邮箱 以及接受邮箱还有邮箱验证码,应该设置在App Secret,而不是公开到仓库里面。
SENDER = os.environ.get("SENDER");
RECEIVER = os.environ.get("RECEIVER");
PASSWORD = os.environ.get("PASSWORD");

sender = SENDER # 你的QQ邮箱 机密文件
receiver = RECEIVER # 接收者邮箱 机密文件

subject = 'Python邮件测试' # 邮件主题
body = '''这是使用Python通过QQ邮箱发送的邮件,哈哈哈。
测试通过,注意事项:
1 要设置repository secret 设置发件人 授权码 收件人等到 action的 repository secret
2 修改正文内容
3 rerun的时候 要新建一个job 不能一直re-run 那个failed job。
''' # 邮件正文

# 设置 SMTP 服务器及端口号
smtp_server = 'smtp.qq.com'
smtp_port = 587
password = PASSWORD # QQ邮箱的授权码 机密文件

# 创建一个MIMEText对象,指定邮件正文内容、格式和编码
message = MIMEText(body, 'plain', 'utf-8')
message['From'] = Header(sender)
message['To'] = Header(receiver)
message['Subject'] = Header(subject)

try:
# 连接到QQ邮箱的SMTP服务器并进行SSL加密
server = smtplib.SMTP(smtp_server, smtp_port, timeout=10)
# 启动 TLS 安全连接
server.starttls()
# 登录QQ邮箱
server.login(sender, password)
# 发送邮件
server.sendmail(sender, [receiver], message.as_string())

print("邮件发送成功")
except smtplib.SMTPException as e:
print(f"邮件发送失败: {e}")
except Exception as e:
print(f"其他错误: {e}")
finally:
# 关闭连接
try:
server.quit()
except:
pass
44 changes: 44 additions & 0 deletions use_serv00_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os
# 使用免费 serv00 企业邮箱 也能正常发送

# 注意,邮箱 以及接受邮箱还有邮箱验证码,应该设置在App Secret,而不是公开到仓库里面。
SENDER = '[email protected]'
RECEIVER = '[email protected]';
PASSWORD = 'passwordxxx';


sender = SENDER
receiver = RECEIVER

subject = 'Python邮件测试'
body = '这是使用Python通过Sev00邮箱发送的邮件,哈哈哈'

# 设置 SMTP 服务器及端口号
smtp_server = 'mail7.serv00.com'
smtp_port = 465
password = PASSWORD

message = MIMEText(body, 'plain', 'utf-8')
message['From'] = Header(sender)
message['To'] = Header(receiver)
message['Subject'] = Header(subject)

try:
# use SMTP_SSL directly
server = smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=10)
server.login(sender, password)
server.sendmail(sender, [receiver], message.as_string())

print("邮件发送成功")
except smtplib.SMTPException as e:
print(f"邮件发送失败: {e}")
except Exception as e:
print(f"其他错误: {e}")
finally:
try:
server.quit()
except:
pass