Skip to content

Gmail +reply: Invalid To header when From display name contains commas or parentheses #512

@richandersonio

Description

@richandersonio

Bug

gws gmail +reply fails with Invalid To header when replying to emails where the sender's display name contains commas or parentheses (common with corporate email like Outlook/Exchange).

Reproduction

# Email from: "Anderson, Rich (CORP)" <Richard.Anderson@adp.com>
gws gmail +reply --message-id <id> --body "Test reply"
{
  "error": {
    "code": 400,
    "message": "Invalid To header",
    "reason": "invalidArgument"
  }
}

Root Cause

In src/helpers/gmail/mod.rs, encode_address_header() reconstructs the address from parsed components but does not re-quote the display name:

// Line ~492
format!("{} <{}>", display, email)

When the original From header is "Anderson, Rich (CORP)" <Richard.Anderson@adp.com>:

  1. extract_display_name() strips the quotes → Anderson, Rich (CORP)
  2. encode_address_header() reconstructs it as: Anderson, Rich (CORP) <Richard.Anderson@adp.com>
  3. The unquoted comma makes this look like two mailboxes to the Gmail API
  4. The unquoted (CORP) is interpreted as an RFC 2822 comment
  5. Gmail API rejects the malformed To header

Fix

The display name should be re-quoted when it contains RFC 2822 special characters (,, ;, (, ), <, >, @, \, ", ., [, ]):

// ASCII display name — quote if it contains special characters
if display.contains(|c: char| ",;()<>@\\\".[]".contains(c)) {
    format!("\"{}\" <{}>", display.replace('"', "\\\""), email)
} else {
    format!("{} <{}>", display, email)
}

Environment

  • gws version: 0.16.0
  • macOS 15 (Darwin 25.2.0)
  • Source email: Outlook/Exchange corporate email with "Last, First (DEPT)" display name format

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions