域名邮箱搭建

一、前置条件:

  1. GMail邮箱账号
  2. cloudflare账号
  3. resend.com账号
  4. 域名

二、域名邮件接收

使用cloudflare的邮箱服务,将域名邮箱转发到你的gmail邮箱,添加对于的dns记录

image-20251001220334461

image-20251001220447757

配置Catch-all,选择发送到电子邮件,目标使用gmail邮箱image-20251001220619945

三、域名邮件发送

使用resend.com服务,免费发送邮件额度:3000/月,100/天

将域名添加到resend

image-20251001012036868

在cloudflare上面添加相应的dns记录,可以等了cloudflare快捷添加,待出现三个Verified就表示成功了

image-20251001012230381

四、GMail添加邮箱

进入Gmail设置 → “账号和导入” → “用这个地址发送邮件” → 点击”添加其他电子邮件地址”。取消勾选 视为别名,点击下一步

image-20251001012537971

添加SMTP配置

  • Host:smtp.resend.com
  • Port:465
  • User:resend
  • Password:你的resend key

点击添加账号后,去到cloudflare配置的目标邮箱点击确认认证即可,然后设置为默认发信地址即可

image-20251001012652172

至此,你可以通过cloudflare转发域名邮箱到gmail,在gmail上使用域名邮箱发送或回复邮件。

五、简单的发件程序

对于日常的发送邮件而不需要回复邮件,可以使用如下python代码进行发送,下面是一个python gui程序,方便发送邮件

1、email_sender.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import base64
from pathlib import Path
import tkinter as tk
from tkinter import filedialog, messagebox
import requests

# Windows 高分屏适配,避免字体模糊
if sys.platform == "win32":
try:
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except Exception:
pass


class EmailSenderApp:
"""极简 Resend 邮件发送器:仅保留发送功能"""

def __init__(self, root):
self.root = root
self.root.title("Resend 邮件发送器")
self.root.geometry("720x560")
self.api_key = os.getenv("RESEND_KEY")

self.attachments = []

self.build_ui()

if not self.api_key:
messagebox.showwarning("提示", "未检测到 RESEND_KEY 环境变量,发送前请先配置。")

def build_ui(self):
pad = 10
frm = tk.Frame(self.root)
frm.pack(fill=tk.BOTH, expand=True, padx=pad, pady=pad)

# From
tk.Label(frm, text="From").grid(row=0, column=0, sticky="w")
self.from_entry = tk.Entry(frm)
self.from_entry.grid(row=0, column=1, sticky="we", padx=(6, 0))

# To
tk.Label(frm, text="To").grid(row=1, column=0, sticky="w", pady=(6, 0))
self.to_entry = tk.Entry(frm)
self.to_entry.grid(row=1, column=1, sticky="we", padx=(6, 0), pady=(6, 0))

# Subject
tk.Label(frm, text="Subject").grid(row=2, column=0, sticky="w", pady=(6, 0))
self.subject_entry = tk.Entry(frm)
self.subject_entry.grid(row=2, column=1, sticky="we", padx=(6, 0), pady=(6, 0))

# Message
tk.Label(frm, text="Message").grid(row=3, column=0, sticky="nw", pady=(6, 0))
msg_frame = tk.Frame(frm)
msg_frame.grid(row=3, column=1, sticky="nsew", padx=(6, 0), pady=(6, 0))
self.msg_text = tk.Text(msg_frame, height=12, wrap=tk.WORD)
self.msg_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
msg_scroll = tk.Scrollbar(msg_frame, command=self.msg_text.yview)
msg_scroll.pack(side=tk.RIGHT, fill=tk.Y)
self.msg_text.configure(yscrollcommand=msg_scroll.set)

# Attachments
attach_bar = tk.Frame(frm)
attach_bar.grid(row=4, column=1, sticky="w", pady=(6, 0))
tk.Button(attach_bar, text="添加附件", command=self.add_attachments).pack(side=tk.LEFT)
tk.Button(attach_bar, text="移除选中", command=self.remove_attachment).pack(side=tk.LEFT, padx=(6, 0))

self.attach_list = tk.Listbox(frm, height=4)
self.attach_list.grid(row=5, column=1, sticky="nsew", padx=(6, 0), pady=(6, 0))

# Buttons
btn_bar = tk.Frame(frm)
btn_bar.grid(row=6, column=1, sticky="e", pady=(10, 0))
tk.Button(btn_bar, text="清空", command=self.clear_form).pack(side=tk.RIGHT)
tk.Button(btn_bar, text="发送", command=self.send_email).pack(side=tk.RIGHT, padx=(0, 8))

# Layout weights
frm.columnconfigure(1, weight=1)
frm.rowconfigure(3, weight=1)
frm.rowconfigure(5, weight=1)

def add_attachments(self):
files = filedialog.askopenfilenames(title="选择附件")
for f in files:
self.attachments.append(f)
self.attach_list.insert(tk.END, Path(f).name)

def remove_attachment(self):
sel = self.attach_list.curselection()
if not sel:
return
idx = sel[0]
self.attach_list.delete(idx)
self.attachments.pop(idx)

def clear_form(self):
self.from_entry.delete(0, tk.END)
self.to_entry.delete(0, tk.END)
self.subject_entry.delete(0, tk.END)
self.msg_text.delete("1.0", tk.END)
self.attachments.clear()
self.attach_list.delete(0, tk.END)

def send_email(self):
if not self.api_key:
messagebox.showerror("错误", "未配置 RESEND_KEY 环境变量。")
return

from_email = self.from_entry.get().strip()
to_email = self.to_entry.get().strip()
subject = self.subject_entry.get().strip()
message = self.msg_text.get("1.0", tk.END).strip()

if not all([from_email, to_email, subject, message]):
messagebox.showerror("错误", "请填写 From、To、Subject 和 Message。")
return

payload = {
"from": from_email,
"to": [to_email],
"subject": subject,
"html": f"<html><body><pre>{message}</pre></body></html>",
}

if self.attachments:
att = []
for p in self.attachments:
try:
with open(p, "rb") as fh:
att.append({
"filename": Path(p).name,
"content": base64.b64encode(fh.read()).decode(),
})
except Exception as e:
messagebox.showerror("错误", f"读取附件失败: {Path(p).name} - {e}")
return
payload["attachments"] = att

try:
resp = requests.post(
"https://api.resend.com/emails",
json=payload,
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
timeout=30,
)
if resp.status_code == 200:
messagebox.showinfo("成功", "邮件已发送。")
self.clear_form()
else:
try:
msg = resp.json().get("message", resp.text)
except Exception:
msg = resp.text
messagebox.showerror("发送失败", msg)
except Exception as e:
messagebox.showerror("错误", f"请求失败: {e}")


def main():
root = tk.Tk()
EmailSenderApp(root)
root.mainloop()


if __name__ == "__main__":
main()


2、requirements.txt

1
requests==2.31.0