golang 代发邮件支持附件发送,outlook案列,其他邮箱须要更换对应邮箱服务域名

[复制链接]
发表于 2025-12-13 07:55:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
GPT===问答实例
  1. import pandas as pd
  2. from openai.embeddings_utils import get_embedding, cosine_similarity
  3. import openai
  4. import os
  5. import logging as logger
  6. from flask_cors import CORS
  7. import os
  8. openai.api_key = os.getenv('OPENAI_API_KEY')
  9. class Chatbot():
  10.     def parse_paper(self, pdf):
  11.         logger.info("Parsing paper")
  12.         number_of_pages = len(pdf.pages)
  13.         logger.info(f"Total number of pages: {number_of_pages}")
  14.         paper_text = []
  15.         for i in range(number_of_pages):
  16.             page = pdf.pages[i]
  17.             page_text = []
  18.             def visitor_body(text, cm, tm, fontDict, fontSize):
  19.                 x = tm[4]
  20.                 y = tm[5]
  21.                 # ignore header/footer
  22.                 if (y > 50 and y < 720) and (len(text.strip()) > 1):
  23.                     page_text.append({
  24.                         'fontsize': fontSize,
  25.                         'text': text.strip().replace('\x03', ''),
  26.                         'x': x,
  27.                         'y': y
  28.                     })
  29.             _ = page.extract_text(visitor_text=visitor_body)
  30.             blob_font_size = None
  31.             blob_text = ''
  32.             processed_text = []
  33.             for t in page_text:
  34.                 if t['fontsize'] == blob_font_size:
  35.                     blob_text += f" {t['text']}"
  36.                     if len(blob_text) >= 2000:
  37.                         processed_text.append({
  38.                             'fontsize': blob_font_size,
  39.                             'text': blob_text,
  40.                             'page': i
  41.                         })
  42.                         blob_font_size = None
  43.                         blob_text = ''
  44.                 else:
  45.                     if blob_font_size is not None and len(blob_text) >= 1:
  46.                         processed_text.append({
  47.                             'fontsize': blob_font_size,
  48.                             'text': blob_text,
  49.                             'page': i
  50.                         })
  51.                     blob_font_size = t['fontsize']
  52.                     blob_text = t['text']
  53.                 paper_text += processed_text
  54.         logger.info("Done parsing paper")
  55.         return paper_text
复制代码
通用代发邮件支持附件发送
  1. package utils
  2. import (
  3.         "crypto/aes"
  4.         "crypto/cipher"
  5.         "dsar-backend/constant"
  6.         "dsar-backend/dto"
  7.         "errors"
  8.         "io"
  9.         "strings"
  10.         "github.com/spf13/viper"
  11.         "gopkg.in/gomail.v2"
  12. )
  13. type EmailConfig struct {
  14.         UserName           string            `json:"userName"`
  15.         Password           string            `json:"password"`
  16.         Address            string            `json:"address"`
  17.         Host               string            `json:"host"`
  18.         Port               int               `json:"port"`
  19.         Identity           string            `json:"identity"`
  20.         FromAdress         string            `json:"fromAdress"`
  21.         ToAdress           []string          `json:"toAdress"`
  22.         Mailtype           string            `json:"mailtype"`
  23.         Subject            string            `json:"subject"`
  24.         Body               string            `json:"body"`
  25.         DontNeedDecryptPwd bool              `json:"dontNeedDecryptPwd"`
  26.         Attachments        map[string][]byte `json:"attachments"`
  27.         Bcc                string            `json:"bcc"`
  28. }
  29. // 加密方法
  30. func AESEncrypt(text, secret string) (string, error) {
  31.         block, err := aes.NewCipher([]byte(secret))
  32.         if err != nil {
  33.                 return "", err
  34.         }
  35.         plainText := []byte(text)
  36.         cfb := cipher.NewCFBEncrypter(block, _bytes)
  37.         cipherText := make([]byte, len(plainText))
  38.         cfb.XORKeyStream(cipherText, plainText)
  39.         return Encode(cipherText), nil
  40. }
  41. // 解密方法
  42. func AESDecrypt(text, secret string) (string, error) {
  43.         block, err := aes.NewCipher([]byte(secret))
  44.         if err != nil {
  45.                 return "", err
  46.         }
  47.         cipherText := Decode(text)
  48.         cfb := cipher.NewCFBDecrypter(block, _bytes)
  49.         plainText := make([]byte, len(cipherText))
  50.         cfb.XORKeyStream(plainText, cipherText)
  51.         return string(plainText), nil
  52. }
  53. func SendMail(config *EmailConfig) error {
  54.         // 设置邮箱主体
  55.         var err error
  56.         user := config.UserName
  57.         var password string
  58.         if config.DontNeedDecryptPwd {
  59.                 password = config.Password
  60.         } else {
  61.                 secret := viper.GetString("xxxxx")
  62.                 password, err = AESDecrypt(config.Password, secret)
  63.                 if err != nil {
  64.                         return errors.New("email secret decrypt error")
  65.                 }
  66.         }
  67.         m := gomail.NewMessage()
  68.         m.SetHeader("From", m.FormatAddress(user, user)) // 添加别名
  69.         m.SetHeader("To", config.ToAdress...)            // 发送给用户(可以多个)
  70.         m.SetHeader("Subject", config.Subject)           // 设置邮件主题
  71.         if !IsEmpty(config.Bcc) {
  72.                 m.SetHeader("Bcc", config.Bcc) // 密送
  73.         }
  74.         m.SetBody("text/html", config.Body)                             // 设置邮件正文
  75.         d := gomail.NewDialer(config.Host, config.Port, user, password) // 设置邮件正文
  76.         if len(config.Attachments) > 0 { //设置邮件附件  第一个参数是附件名称   第二个参数是二进制文件流
  77.                 for filename, content := range config.Attachments {
  78.                         contentCopy := content
  79.                         m.Attach(filename, gomail.SetCopyFunc(func(w io.Writer) error {
  80.                                 _, err := w.Write(contentCopy)
  81.                                 return err
  82.                         }))
  83.                 }
  84.         }
  85.         err = d.DialAndSend(m)
  86.         return err
  87. }
  88. func SendOutlookEmail(config *dto.EmailConfig) error {
  89.         config.Port = 587
  90.         config.Host = "smtp-mail.outlook.com"
  91.         config.Address = "smtp-mail.outlook.com:587"
  92.         return SendMail(config)
  93. }
  94. func GetEmailSuffix(email string) string {
  95.         if !strings.Contains(email, constant.EmailSeparator) {
  96.                 return ""
  97.         }
  98.         return email[strings.Index(email, constant.EmailSeparator)+1:]
  99. }
  100. func GetEmailSmtpHost(email string) string {
  101.         suffix := GetEmailSuffix(email)
  102.         if suffix == "" {
  103.                 return ""
  104.         }
  105.         return constant.EmailSmtpPreffix + suffix
  106. }
  107. func GetEmailPop3Host(email string) string {
  108.         suffix := GetEmailSuffix(email)
  109.         if suffix == "" {
  110.                 return ""
  111.         }
  112.         return constant.EmailPop3Preffix + suffix
  113. }
  114. EmailSeparator = "@"
  115. EmailPort = 25
  116. EmailSmtpPreffix = "smtp."
  117. EmailPop3Preffix = "pop."
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表