Skip to content

feat(gmail): auto-populate From display name + fix reply-all to own message#530

Merged
jpoehnelt merged 2 commits intogoogleworkspace:mainfrom
malob:feat/gmail-default-sender
Mar 18, 2026
Merged

feat(gmail): auto-populate From display name + fix reply-all to own message#530
jpoehnelt merged 2 commits intogoogleworkspace:mainfrom
malob:feat/gmail-default-sender

Conversation

@malob
Copy link
Contributor

@malob malob commented Mar 18, 2026

Description

Two changes to the Gmail helpers:

1. Auto-populate From header with display name from send-as settings

When sending email without --from, the CLI now fetches the user's send-as
identities (/users/me/settings/sendAs) and sets the From header with the
display name, matching Gmail web client behavior. Previously, no From header
was set and Gmail's API filled it in with a bare email address.

Also enriches bare --from emails: if you pass --from alias@work.com and
that alias has a display name configured in Gmail, the From header includes it
automatically.

Three cases:

  • No --from → use the default send-as identity (display name + email)
  • --from with bare email → look up in send-as list, enrich if found
  • --from with display name already → use as-is, skip the API call

Workspace accounts: For accounts where the primary address inherits its
display name from the organization directory (sendAs returns empty
displayName), falls back to the People API (people/me) to fetch the
profile name. This requires the userinfo.profile scope, which is now
included in the identity scopes that auth login always requests. Degrades
gracefully with a tip if the scope hasn't been granted yet.

Scopes: gmail.modify already covers the sendAs.list endpoint, so no
additional OAuth scope is needed for the primary feature. In +send, auth
uses the discovery doc scopes rather than hardcoding gmail.modify,
preserving compatibility with narrower send-only OAuth setups (e.g.
gmail.send). The auth login flow now includes userinfo.profile alongside
the existing openid and userinfo.email identity scopes (non-sensitive).

2. Fix reply-all to own message

When replying-all to a message you sent, the original sender (you) was
excluded from To, leaving it empty and producing an error. Gmail web handles
this by using the original To recipients as reply targets instead. The fix
detects self-reply by checking if the original From matches the user's primary
email or send-as alias, then swaps the candidate logic accordingly.

Gmail web also ignores Reply-To on self-sent messages — we match that behavior.

This also correctly handles the case where the primary email differs from the
default send-as alias — both are excluded from recipients to prevent CCing
yourself.

Other notes

  • build_api_error: new shared helper that parses Google API JSON error
    responses (extracting message, reason, and enable URL), matching the
    executor's handle_error_response pattern. Used by all four Gmail API
    functions (fetch_message_metadata, fetch_send_as_identities,
    fetch_profile_display_name, fetch_user_email). All error messages
    printed to stderr are sanitized via sanitize_for_terminal.
  • resolve_sender is a new shared function used by all four mail helpers
  • parse_send_as_response, resolve_sender_from_identities, build_api_error,
    and parse_profile_display_name are pure/sync functions with comprehensive
    unit tests
  • fetch_user_email in reply.rs is retained for reply-all self-dedup (the
    primary email may differ from the send-as alias)

Checklist:

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: 674c298

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@googleworkspace-bot googleworkspace-bot added the area: core Core CLI parsing, commands, error handling, utilities label Mar 18, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Gmail CLI helpers by improving how sender information is handled and by correcting a long-standing issue with reply-all functionality. It introduces automatic population of the 'From' header with configured display names, making sent emails appear more professional and consistent with the web client. Additionally, it resolves a critical bug where replying-all to one's own message would fail, ensuring a smoother and more reliable user experience for email interactions.

Highlights

  • Auto-populate From header with display name: The CLI now fetches the user's send-as identities and sets the From header with the display name, matching Gmail web client behavior. This enriches bare --from emails and uses the default send-as identity if --from is not provided. This degrades gracefully if the API call fails.
  • Fix reply-all to own message: When replying-all to a message you sent, the original sender (you) was previously excluded from To, leading to an error. The system now detects self-replies and uses the original To recipients as reply targets instead, matching Gmail web client behavior. It also correctly handles cases where the primary email differs from the default send-as alias for recipient exclusion.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two valuable features for the Gmail helpers: auto-populating the 'From' header with a display name, and fixing an issue with 'reply-all' to one's own messages. The implementation is robust, with comprehensive unit tests covering various edge cases. The code is clean and well-documented.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two main features for the Gmail helpers: auto-populating the From header with a display name and fixing an issue with reply-all to one's own message. The implementation is comprehensive and includes new tests. My review focuses on improving security and maintainability. I've identified a potential security vulnerability related to unsanitized error messages being printed to the terminal, which could lead to escape sequence injection. I've also suggested refactoring the error handling for new API calls to be more robust by using structured errors instead of string matching, which improves maintainability.

@malob malob force-pushed the feat/gmail-default-sender branch from a12d6be to c0b9695 Compare March 18, 2026 04:14
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two significant and valuable features for the Gmail helpers: auto-populating the From header with a display name, and fixing reply-all behavior for self-sent messages. The implementation is well-structured, introducing new functions with clear responsibilities and comprehensive unit tests. The graceful degradation for API failures is also a nice touch for user experience. I've identified one high-severity issue related to error handling that should be addressed to ensure correct error reporting.

malob added 2 commits March 17, 2026 22:39
Fetch the user's send-as identities via /users/me/settings/sendAs to set
the From header with a display name in all mail helpers, matching Gmail
web client behavior. The gmail.modify scope already covers this endpoint.

Three resolution cases:
- No --from: use the default send-as identity (display name + email)
- --from with bare email: enrich from send-as list if the alias exists
- --from with display name: use as-is, skip the API call

For Workspace accounts where the primary address inherits its display
name from the organization directory (sendAs returns empty displayName),
falls back to the People API to fetch the profile name. This requires
the userinfo.profile scope, which is now included in the identity scopes
that auth login always requests. Degrades gracefully with a tip if the
scope hasn't been granted yet.

Introduces build_api_error, a shared helper that parses Google API JSON
error responses (extracting message, reason, and enable URL), matching
the executor's handle_error_response pattern. Used by all four Gmail API
functions. All error messages printed to stderr are sanitized via
sanitize_for_terminal.

In +send, auth uses the discovery doc scopes rather than hardcoding
gmail.modify, preserving compatibility with narrower send-only OAuth
setups. In reply-all, the profile endpoint is always called for
self-email dedup since the primary address may differ from the send-as
alias.
When replying-all to a message you sent, the original sender (you) was
excluded from To, leaving it empty and producing an error. Gmail web
handles this by using the original To recipients as reply targets.

Detect self-reply by checking if the original From matches the user's
primary email or send-as alias, then swap the candidate logic:
- Self-reply: To = original To, CC = original CC
- Normal reply: To = Reply-To or From, CC = original To + CC
@malob malob force-pushed the feat/gmail-default-sender branch from c0b9695 to 674c298 Compare March 18, 2026 05:44
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant improvements to Gmail helpers, primarily by auto-populating the 'From' display name and correctly handling 'reply-all' to self-sent messages. The changes are well-structured, with new functions for resolving sender identities and fetching profile display names. Comprehensive unit tests have been added for the new logic, ensuring correctness and maintainability. Error handling has also been refactored for consistency and includes proper sanitization of terminal output, adhering to best practices. Overall, the changes are a valuable addition, enhancing both functionality and robustness.

@jpoehnelt jpoehnelt merged commit 908cf73 into googleworkspace:main Mar 18, 2026
26 checks passed
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 71.32216% with 154 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.97%. Comparing base (9c26e3c) to head (674c298).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/helpers/gmail/mod.rs 71.63% 120 Missing ⚠️
src/helpers/gmail/reply.rs 85.10% 14 Missing ⚠️
src/helpers/gmail/send.rs 0.00% 13 Missing ⚠️
src/auth_commands.rs 0.00% 5 Missing ⚠️
src/helpers/gmail/forward.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #530      +/-   ##
==========================================
+ Coverage   68.10%   68.97%   +0.87%     
==========================================
  Files          40       41       +1     
  Lines       17954    19168    +1214     
==========================================
+ Hits        12227    13222     +995     
- Misses       5727     5946     +219     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@malob malob deleted the feat/gmail-default-sender branch March 18, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: auth area: core Core CLI parsing, commands, error handling, utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants