follow RFC2822 reply structure

This commit is contained in:
DBras 2025-03-05 19:23:42 +01:00
parent 094671c654
commit 1e35142e33
1 changed files with 32 additions and 16 deletions

48
main.go
View File

@ -32,7 +32,12 @@ func getEmail(service *gmail.Service, msgID string) *gmail.Message {
return r
}
func getEmailPlaintext (mail *gmail.Message) (fromAddr string, bodyStr string) {
func getEmailPlaintext (mail *gmail.Message) (
fromAddr string,
bodyStr string,
subjStr string,
idStr string,
) {
part := mail.Payload.Parts[0].Body.Data
body, err := base64.StdEncoding.DecodeString(part)
if err != nil {
@ -43,10 +48,14 @@ func getEmailPlaintext (mail *gmail.Message) (fromAddr string, bodyStr string) {
if header.Name == "From" {
_, addrPart, _ := strings.Cut(header.Value, "<")
fromAddr, _, _ = strings.Cut(addrPart, ">")
} else if header.Name == "Subject" {
subjStr = header.Value
} else if header.Name == "Message-ID" {
idStr = header.Value
}
}
return fromAddr, string(body)
return fromAddr, string(body), subjStr, idStr
}
func main() {
@ -82,8 +91,9 @@ func main() {
for _, msg := range r.Messages {
fmt.Printf("- %s\n", msg.Id)
mail := getEmail(service, msg.Id)
fromAddr, msgBody := getEmailPlaintext(mail)
fromAddr, msgBody, msgSubj, msgID := getEmailPlaintext(mail)
fmt.Printf("From: %s\n", fromAddr)
fmt.Printf("Subject: %s\n", msgSubj)
fmt.Printf("Link: %s", msgBody)
cmd := exec.Command("yt-dlp", msgBody)
@ -92,6 +102,25 @@ func main() {
log.Fatalf("Video download failed: %v", err)
}
var message gmail.Message
messageStr := fmt.Sprintf(
"From: %s\r\n" +
"To: %s\r\n" +
"Subject: %s\r\n" +
"In-Reply-To: %s\r\n" +
"\r\n" +
"Video download complete",
EMAILADDR,
fromAddr,
msgSubj,
msgID,
)
message.Raw = base64.URLEncoding.EncodeToString([]byte(messageStr))
_, err = service.Users.Messages.Send(USER, &message).Do()
if err != nil {
log.Fatalf("Unable to send email: %v", err)
}
var modifications gmail.ModifyMessageRequest
modifications.AddLabelIds = []string{"Label_6181251506395195727"}
modifications.RemoveLabelIds = []string{"INBOX"}
@ -100,19 +129,6 @@ func main() {
log.Fatalf("Unable to move email request from inbox: %v", err)
}
var message gmail.Message
messageStr := fmt.Sprintf(
"From: %s\r\n" +
"To: %s\r\n" +
"Subject: Request received\r\n\r\n" +
"Message body",
EMAILADDR,
fromAddr)
message.Raw = base64.URLEncoding.EncodeToString([]byte(messageStr))
_, err = service.Users.Messages.Send(USER, &message).Do()
if err != nil {
log.Fatalf("Unable to send email: %v", err)
}
fmt.Printf("Download completed, confirmation sent to: %s\n", fromAddr)
fmt.Println()
}