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

发布于:2024-12-07 ⋅ 阅读:(81) ⋅ 点赞:(0)

GPT===问答实例

import pandas as pd
from openai.embeddings_utils import get_embedding, cosine_similarity
import openai
import os
import logging as logger
from flask_cors import CORS
import os
openai.api_key = os.getenv('OPENAI_API_KEY')

class Chatbot():

    def parse_paper(self, pdf):
        logger.info("Parsing paper")
        number_of_pages = len(pdf.pages)
        logger.info(f"Total number of pages: {number_of_pages}")
        paper_text = []
        for i in range(number_of_pages):
            page = pdf.pages[i]
            page_text = []

            def visitor_body(text, cm, tm, fontDict, fontSize):
                x = tm[4]
                y = tm[5]
                # ignore header/footer
                if (y > 50 and y < 720) and (len(text.strip()) > 1):
                    page_text.append({
                        'fontsize': fontSize,
                        'text': text.strip().replace('\x03', ''),
                        'x': x,
                        'y': y
                    })

            _ = page.extract_text(visitor_text=visitor_body)

            blob_font_size = None
            blob_text = ''
            processed_text = []

            for t in page_text:
                if t['fontsize'] == blob_font_size:
                    blob_text += f" {t['text']}"
                    if len(blob_text) >= 2000:
                        processed_text.append({
                            'fontsize': blob_font_size,
                            'text': blob_text,
                            'page': i
                        })
                        blob_font_size = None
                        blob_text = ''
                else:
                    if blob_font_size is not None and len(blob_text) >= 1:
                        processed_text.append({
                            'fontsize': blob_font_size,
                            'text': blob_text,
                            'page': i
                        })
                    blob_font_size = t['fontsize']
                    blob_text = t['text']
                paper_text += processed_text
        logger.info("Done parsing paper")
        return paper_text

 

通用代发邮件支持附件发送

package utils

import (
	"crypto/aes"
	"crypto/cipher"
	"dsar-backend/constant"
	"dsar-backend/dto"
	"errors"
	"io"
	"strings"

	"github.com/spf13/viper"
	"gopkg.in/gomail.v2"
)
type EmailConfig struct {
	UserName           string            `json:"userName"`
	Password           string            `json:"password"`
	Address            string            `json:"address"`
	Host               string            `json:"host"`
	Port               int               `json:"port"`
	Identity           string            `json:"identity"`
	FromAdress         string            `json:"fromAdress"`
	ToAdress           []string          `json:"toAdress"`
	Mailtype           string            `json:"mailtype"`
	Subject            string            `json:"subject"`
	Body               string            `json:"body"`
	DontNeedDecryptPwd bool              `json:"dontNeedDecryptPwd"`
	Attachments        map[string][]byte `json:"attachments"`
	Bcc                string            `json:"bcc"`
}
// 加密方法
func AESEncrypt(text, secret string) (string, error) {
	block, err := aes.NewCipher([]byte(secret))
	if err != nil {
		return "", err
	}

	plainText := []byte(text)
	cfb := cipher.NewCFBEncrypter(block, _bytes)
	cipherText := make([]byte, len(plainText))
	cfb.XORKeyStream(cipherText, plainText)

	return Encode(cipherText), nil
}

// 解密方法
func AESDecrypt(text, secret string) (string, error) {
	block, err := aes.NewCipher([]byte(secret))
	if err != nil {
		return "", err
	}

	cipherText := Decode(text)
	cfb := cipher.NewCFBDecrypter(block, _bytes)
	plainText := make([]byte, len(cipherText))
	cfb.XORKeyStream(plainText, cipherText)

	return string(plainText), nil
}

func SendMail(config *EmailConfig) error {
	// 设置邮箱主体
	var err error
	user := config.UserName
	var password string
	if config.DontNeedDecryptPwd {
		password = config.Password
	} else {
		secret := viper.GetString("xxxxx")
		password, err = AESDecrypt(config.Password, secret)
		if err != nil {
			return errors.New("email secret decrypt error")
		}
	}

	m := gomail.NewMessage()
	m.SetHeader("From", m.FormatAddress(user, user)) // 添加别名
	m.SetHeader("To", config.ToAdress...)            // 发送给用户(可以多个)
	m.SetHeader("Subject", config.Subject)           // 设置邮件主题
	if !IsEmpty(config.Bcc) {
		m.SetHeader("Bcc", config.Bcc) // 密送
	}
	m.SetBody("text/html", config.Body)                             // 设置邮件正文
	d := gomail.NewDialer(config.Host, config.Port, user, password) // 设置邮件正文

	if len(config.Attachments) > 0 { //设置邮件附件  第一个参数是附件名称   第二个参数是二进制文件流
		for filename, content := range config.Attachments {
			contentCopy := content
			m.Attach(filename, gomail.SetCopyFunc(func(w io.Writer) error {
				_, err := w.Write(contentCopy)
				return err
			}))
		}
	}
	err = d.DialAndSend(m)
	return err
}

func SendOutlookEmail(config *dto.EmailConfig) error {
	config.Port = 587
	config.Host = "smtp-mail.outlook.com"
	config.Address = "smtp-mail.outlook.com:587"

	return SendMail(config)
}

func GetEmailSuffix(email string) string {
	if !strings.Contains(email, constant.EmailSeparator) {
		return ""
	}

	return email[strings.Index(email, constant.EmailSeparator)+1:]
}

func GetEmailSmtpHost(email string) string {
	suffix := GetEmailSuffix(email)
	if suffix == "" {
		return ""
	}

	return constant.EmailSmtpPreffix + suffix
}

func GetEmailPop3Host(email string) string {
	suffix := GetEmailSuffix(email)
	if suffix == "" {
		return ""
	}

	return constant.EmailPop3Preffix + suffix
}

EmailSeparator = "@"
EmailPort = 25
EmailSmtpPreffix = "smtp."
EmailPop3Preffix = "pop."