Files
crawlab/core/notification/mail_gmail.go
Marvin Zhang 3276083994 refactor: replace apex/log with structured logger across multiple services
- Replaced all instances of apex/log with a structured logger interface in various services, including Api, Server, Config, and others, to enhance logging consistency and context.
- Updated logging calls to utilize the new logger methods, improving error tracking and service monitoring.
- Added logger initialization in services and controllers to ensure proper logging setup.
- Improved error handling and logging messages for better clarity during service operations.
- Removed unused apex/log imports and cleaned up related code for better maintainability.
2024-12-24 19:11:19 +08:00

61 lines
1.5 KiB
Go

package notification
import (
"context"
"encoding/base64"
"github.com/crawlab-team/crawlab/core/models/models"
"github.com/crawlab-team/crawlab/trace"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"strings"
)
func sendMailGmail(ch *models.NotificationChannel, smtpConfig smtpAuthentication, options sendOptions, htmlBody, txtBody string) error {
// 读取服务账户 JSON 密钥
b := []byte(ch.GoogleOAuth2Json)
// 使用服务账户 JSON 密钥文件创建 JWT 配置
config, err := google.JWTConfigFromJSON(b, gmail.GmailSendScope)
if err != nil {
logger.Errorf("Unable to parse service account key file to config: %v", err)
return trace.TraceError(err)
}
// 使用服务账户的电子邮件地址来模拟用户
config.Subject = ch.SMTPUsername
// 创建 Gmail 服务
client := config.Client(context.Background())
srv, err := gmail.New(client)
if err != nil {
logger.Errorf("Unable to create Gmail client: %v", err)
return err
}
// 创建 MIME 邮件
m, err := getMailMessage(smtpConfig, options, htmlBody, txtBody)
if err != nil {
return err
}
var buf strings.Builder
if _, err := m.WriteTo(&buf); err != nil {
logger.Errorf("Unable to write message: %v", err)
return err
}
// 将邮件内容进行 base64 编码
gmsg := &gmail.Message{
Raw: base64.URLEncoding.EncodeToString([]byte(buf.String())),
}
// 发送邮件
_, err = srv.Users.Messages.Send("me", gmsg).Do()
if err != nil {
logger.Errorf("Unable to send email: %v", err)
return err
}
return nil
}