Skip to content

Bug: sendDraft returns undefined, crashes on .id access despite successful send #3

Description

@willtmc

Bug Description

The sendDraft function in gmail-service.js crashes with Cannot read properties of undefined (reading 'id') even though the email is successfully sent.

Root Cause

// Line ~375 in gmail-service.js
async sendDraft(email, draftId) {
    const gmail = this.getGmailClient(email);
    const response = await gmail.users.drafts.send({
        userId: "me",
        requestBody: { id: draftId },
    });
    return response.data.message;  // ❌ BUG: should be response.data
}

Gmail's drafts.send API returns the Message resource directly in response.data, not nested under response.data.message.

Impact

  • Email IS sent successfully (API call works)
  • Function crashes when trying to access .message.id
  • CLI shows error, making user think send failed
  • Users retry, causing duplicate emails

Fix

async sendDraft(email, draftId) {
    const gmail = this.getGmailClient(email);
    const response = await gmail.users.drafts.send({
        userId: "me",
        requestBody: { id: draftId },
    });
    return response.data;  // ✅ Returns Message directly
}

Reproduction

gmcli you@gmail.com drafts create --to test@example.com --subject "Test" --body "Test"
# Draft created: r-1234567890

gmcli you@gmail.com drafts send r-1234567890
# Error: Cannot read properties of undefined (reading 'id')
# BUT the email was actually sent!

Verified Fix

Applied the fix locally to dist/gmail-service.js and confirmed it works:

gmcli you@gmail.com drafts send r-8175838983646871376
# Sent: 19b2e8c686442822  ✅

Happy to submit a PR if you'd like!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions