单个账号流注册流程

This commit is contained in:
zyj
2025-09-27 18:13:59 +08:00
parent 90dfc3ac38
commit 22e21f6eb8
12 changed files with 404 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
package utils
import (
"crypto/tls"
"fmt"
"io"
"log"
@@ -18,7 +19,8 @@ type EmailImap struct {
// 获取最新一封邮件的正文内容
func (*EmailImap) GetLatestMailContent(server, username, password string) (string, error) {
// 1. 连接 IMAP 服务器 (SSL/TLS, 端口 993)
c, err := client.DialTLS(server, nil)
tlsConfig := &tls.Config{InsecureSkipVerify: true}
c, err := client.DialTLS(server, tlsConfig)
if err != nil {
return "", fmt.Errorf("连接失败: %v", err)
}
@@ -66,7 +68,8 @@ func (*EmailImap) GetLatestMailContent(server, username, password string) (strin
return "", fmt.Errorf("解析邮件失败: %v", err)
}
var content string
var htmlContent, textContent string
for {
p, err := mr.NextPart()
if err == io.EOF {
@@ -76,16 +79,23 @@ func (*EmailImap) GetLatestMailContent(server, username, password string) (strin
return "", fmt.Errorf("读取邮件失败: %v", err)
}
switch p.Header.(type) {
switch h := p.Header.(type) {
case *mail.InlineHeader:
mediaType, _, _ := h.ContentType()
b, _ := io.ReadAll(p.Body)
content = strings.TrimSpace(string(b))
// 只取第一个正文即可
return content, nil
if strings.HasPrefix(mediaType, "text/html") {
htmlContent = string(b)
} else if strings.HasPrefix(mediaType, "text/plain") {
textContent = string(b)
}
}
}
return content, nil
// 优先返回 HTML没有就退回纯文本
if htmlContent != "" {
return htmlContent, nil
}
return textContent, nil
}
func (e *EmailImap) GetMailCode(server, username, password string) string {
@@ -95,7 +105,6 @@ func (e *EmailImap) GetMailCode(server, username, password string) string {
}
htmlStr := res
fmt.Println(htmlStr)
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
if err != nil {
@@ -112,7 +121,7 @@ func (e *EmailImap) GetMailCode(server, username, password string) string {
if code == "" {
return "0"
}
return code
return strings.TrimSpace(code)
}
func test() {