Skip to content

Commit 28fc1d9

Browse files
RealTongdehesa
authored andcommitted
feat(apps/mail): add copy-captcha-from-mail script
This script allows users to copy verification codes from Apple Mail and automatically copies them to the clipboard. Users can then paste the verification code anywhere they need it.
1 parent 5002430 commit 28fc1d9

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
3+
# Copy the verification code from Mail.
4+
#
5+
# Dependency: This script requires the `emlx` package.
6+
# Install it via `pip install emlx`.
7+
#
8+
# Required parameters:
9+
# @raycast.schemaVersion 1
10+
# @raycast.title CaptchaFromMail
11+
# @raycast.mode silent
12+
13+
# Optional parameters:
14+
# @raycast.icon ./images/mail.png
15+
# @raycast.packageName Mail
16+
17+
# Documentation:
18+
# @raycast.description Copy the captcha from the emlx file.
19+
# @raycast.author RealTong
20+
# @raycast.authorURL https://raycast.com/RealTong
21+
22+
import os
23+
import re
24+
import pyperclip
25+
import emlx
26+
27+
28+
def extract_captcha_from_emlx(emlx_path):
29+
if emlx_file is None or os.path.exists(emlx_file) is False:
30+
print("Mail not found")
31+
return
32+
email = emlx.read(emlx_path)
33+
headers = email.headers
34+
content = email.html
35+
subject = headers["Subject"]
36+
captcha_match_keyword = [
37+
"验证码",
38+
"动态密码",
39+
"代码",
40+
"确认",
41+
"码",
42+
"verification",
43+
"code",
44+
"confirm",
45+
]
46+
content_pattern = r"\b[0-9]{6}\b"
47+
subject_pattern = r"\b[a-zA-Z0-9]{6}\b"
48+
# 判断内容中是否包含验证码关键字, 只有包含关键字的邮件才会查找验证码
49+
for keyword in captcha_match_keyword:
50+
if keyword in subject:
51+
if re.search(subject_pattern, subject):
52+
captcha = re.search(subject_pattern, subject).group()
53+
pyperclip.copy(captcha)
54+
print("Verification code successfully copied to clipboard:", captcha)
55+
return
56+
if re.search(content_pattern, content):
57+
captcha = re.search(content_pattern, content).group()
58+
pyperclip.copy(captcha)
59+
print("Verification code successfully copied to clipboard:", captcha)
60+
return
61+
else:
62+
print("Verification code not found")
63+
else:
64+
print("Not a verification code email")
65+
66+
67+
def get_latest_emlx_file(folder):
68+
latest_emlx_file = None
69+
latest_mod_time = 0
70+
for root, dirs, files in os.walk(folder):
71+
for file in files:
72+
if file.endswith(".emlx"):
73+
file_path = os.path.join(root, file)
74+
mod_time = os.path.getmtime(file_path)
75+
if mod_time > latest_mod_time:
76+
latest_mod_time = mod_time
77+
latest_emlx_file = file_path
78+
79+
return latest_emlx_file
80+
81+
82+
if __name__ == "__main__":
83+
try:
84+
import emlx
85+
86+
os.listdir(f"/")
87+
except ImportError:
88+
print("emlx not installed, please run 'pip install emlx' to install it")
89+
exit(1)
90+
except OSError:
91+
print(
92+
"Currently there is no Full Disk Access permission, please grant Full Disk Access permission to the terminal in System Preferences > Security & Privacy > Privacy > Full Disk Access."
93+
)
94+
exit(1)
95+
mail_path = f"{os.path.expanduser('~')}/Library/Mail/V10/"
96+
emlx_file = get_latest_emlx_file(mail_path)
97+
extract_captcha_from_emlx(emlx_file)

commands/apps/mail/images/mail.png

3.96 KB
Loading

0 commit comments

Comments
 (0)