Skip to content

Add standalone background Gmail reader script#55

Open
Gerifield wants to merge 1 commit intomainfrom
feat/gmail-background-reader-110509471110472474
Open

Add standalone background Gmail reader script#55
Gerifield wants to merge 1 commit intomainfrom
feat/gmail-background-reader-110509471110472474

Conversation

@Gerifield
Copy link
Owner

Implemented a standalone Go script that authenticates to the Gmail API, polls for unread emails with attachments (or matching a query), extracts them, and forwards them as multipart/form-data to a configurable local endpoint.

Key Changes:

  • Added new executable cmd/gmail-reader/main.go implementing the requested logic.
  • Added functions to securely cache OAuth tokens.
  • Configurable with POLLING_INTERVAL, WEBHOOK_URL, and SEARCH_QUERY environment variables with sensible defaults as requested.

(Note: Tests failing in other packages were present before my changes, and my standalone file does not impact them.)


PR created automatically by Jules for task 110509471110472474 started by @Gerifield

@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

…chments

Adds `cmd/gmail-reader/main.go` that:
*   Authenticates via OAuth 2.0 Desktop flow to the Gmail API.
*   Polls for specific emails matching a configurable query using `time.Ticker`.
*   Extracts email metadata and attachments.
*   Forwards attachments as multiple files in a multipart/form-data POST request under the `payload` key to a configurable local Webhook URL.
*   Marks successfully forwarded emails as read to prevent duplicate processing.
*   Retrieves configurations for polling interval, webhook URL, and search query via standard environment variables.

Co-authored-by: Gerifield <195914+Gerifield@users.noreply.github.com>
@Gerifield Gerifield force-pushed the feat/gmail-background-reader-110509471110472474 branch from dbaf74c to abe3776 Compare March 12, 2026 13:26
@Gerifield Gerifield requested a review from Copilot March 12, 2026 14:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new standalone Go executable to poll Gmail for messages matching a query, extract body/attachments, and forward them as multipart/form-data to a configurable local HTTP endpoint.

Changes:

  • Added cmd/gmail-reader/main.go implementing OAuth flow, polling, message extraction, and multipart forwarding.
  • Updated go.mod / go.sum to include Gmail API + OAuth2 dependencies (and related indirect upgrades).

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 9 comments.

File Description
go.mod Adds OAuth2 + Gmail API dependencies and updates indirect deps to support the new executable.
go.sum Updates checksums for newly added/updated dependencies.
cmd/gmail-reader/main.go Implements the Gmail polling + forwarding script, including token caching and env-based config.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +292 to +293
h["Content-Disposition"] = []string{fmt.Sprintf(`form-data; name="payload"; filename="%s"`, escapeQuotes(att.Filename))}
if att.MimeType != "" {
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attachment filenames come from email content and can contain CR/LF or other special characters. Building Content-Disposition manually with fmt.Sprintf(...) risks header injection / malformed multipart bodies. Prefer multipart.FileContentDisposition(...) (already used in pkg/httpBotter/logic.go) or explicitly reject/strip \r and \n in filenames.

Copilot uses AI. Check for mistakes.
Comment on lines +291 to +300
h := make(map[string][]string)
h["Content-Disposition"] = []string{fmt.Sprintf(`form-data; name="payload"; filename="%s"`, escapeQuotes(att.Filename))}
if att.MimeType != "" {
h["Content-Type"] = []string{att.MimeType}
} else {
h["Content-Type"] = []string{"application/octet-stream"}
}

part, err := writer.CreatePart(h)
if err != nil {
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multipart.Writer.CreatePart expects a textproto.MIMEHeader, but this code builds a plain map[string][]string. This won’t compile (cannot use h (type map[string][]string) as textproto.MIMEHeader). Use make(textproto.MIMEHeader) (and import net/textproto), or follow the existing pattern used in pkg/httpBotter/logic.go.

Copilot uses AI. Check for mistakes.
Comment on lines +240 to +260
decoded, err := base64.URLEncoding.DecodeString(attachObj.Data)
if err != nil {
log.Printf("Unable to decode attachment %s: %v", part.Filename, err)
return
}

data.Attachments = append(data.Attachments, attachment{
Filename: part.Filename,
Data: decoded,
MimeType: part.MimeType,
})
} else if part.MimeType == "text/plain" && part.Body != nil && part.Body.Data != "" {
// It's the body
decoded, err := base64.URLEncoding.DecodeString(part.Body.Data)
if err == nil {
data.Body += string(decoded)
}
} else if part.MimeType == "text/html" && data.Body == "" && part.Body != nil && part.Body.Data != "" {
// Fallback to HTML body if plain text is not found yet
decoded, err := base64.URLEncoding.DecodeString(part.Body.Data)
if err == nil {
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gmail message/attachment bodies are base64url-encoded and commonly omit padding. Using base64.URLEncoding.DecodeString will fail on unpadded data. Prefer base64.RawURLEncoding.DecodeString(...) (or URLEncoding.WithPadding(base64.NoPadding)) for attachObj.Data and part.Body.Data.

Copilot uses AI. Check for mistakes.
if data.Body != "" {
_ = writer.WriteField("Body", data.Body)
}

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo’s /message endpoint reads the user message from form field message (r.PostFormValue("message")). This client sends Sender/Subject/Date/Body fields but never sends message, so the server will see an empty prompt. Consider populating a message field (e.g., subject + body + metadata) and keeping payload for attachments.

Suggested change
// Populate unified message field expected by the /message endpoint.
// This combines metadata and body so that r.PostFormValue("message")
// on the server side receives a meaningful prompt.
var messageContentBuilder strings.Builder
if data.Sender != "" {
messageContentBuilder.WriteString("From: ")
messageContentBuilder.WriteString(data.Sender)
messageContentBuilder.WriteString("\n")
}
if data.Subject != "" {
messageContentBuilder.WriteString("Subject: ")
messageContentBuilder.WriteString(data.Subject)
messageContentBuilder.WriteString("\n")
}
if data.Date != "" {
messageContentBuilder.WriteString("Date: ")
messageContentBuilder.WriteString(data.Date)
messageContentBuilder.WriteString("\n")
}
if data.Body != "" {
messageContentBuilder.WriteString("\n")
messageContentBuilder.WriteString(data.Body)
}
messageContent := messageContentBuilder.String()
if messageContent != "" {
_ = writer.WriteField("message", messageContent)
}

Copilot uses AI. Check for mistakes.
Comment on lines +276 to +282
_ = writer.WriteField("Sender", data.Sender)
_ = writer.WriteField("Subject", data.Subject)
_ = writer.WriteField("Date", data.Date)

// Add body if no attachments or to provide context
if data.Body != "" {
_ = writer.WriteField("Body", data.Body)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writer.WriteField(...) return values are ignored. If a write fails, the script will still send the request (possibly missing required fields) with no surfaced error. Capture these errors and return early so the message remains unread and can be retried with a clear log.

Suggested change
_ = writer.WriteField("Sender", data.Sender)
_ = writer.WriteField("Subject", data.Subject)
_ = writer.WriteField("Date", data.Date)
// Add body if no attachments or to provide context
if data.Body != "" {
_ = writer.WriteField("Body", data.Body)
if err := writer.WriteField("Sender", data.Sender); err != nil {
return fmt.Errorf("failed to write Sender field: %w", err)
}
if err := writer.WriteField("Subject", data.Subject); err != nil {
return fmt.Errorf("failed to write Subject field: %w", err)
}
if err := writer.WriteField("Date", data.Date); err != nil {
return fmt.Errorf("failed to write Date field: %w", err)
}
// Add body if no attachments or to provide context
if data.Body != "" {
if err := writer.WriteField("Body", data.Body); err != nil {
return fmt.Errorf("failed to write Body field: %w", err)
}

Copilot uses AI. Check for mistakes.
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.NewEncoder(f).Encode(token) error is ignored. If the token can’t be written (disk full, permission issues after open, etc.), the script may proceed but re-auth every run. Handle/return the encode error (and consider logging without printing sensitive paths).

Suggested change
json.NewEncoder(f).Encode(token)
if err := json.NewEncoder(f).Encode(token); err != nil {
log.Fatalf("Unable to encode oauth token to file: %v", err)
}

Copilot uses AI. Check for mistakes.

func loadConfig() Configuration {
cfg := Configuration{
WebhookURL: "http://localhost:8080/webhook",
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default webhook URL (http://localhost:8080/webhook) doesn’t match the repo’s server route (POST /message in internal/server/server.go). With defaults, this script will 404 against the local server-bot. Consider defaulting to /message (or documenting that this targets a different endpoint).

Suggested change
WebhookURL: "http://localhost:8080/webhook",
WebhookURL: "http://localhost:8080/message",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants