Skip to content

fix(gmail): request settings scope and auto-fill send-as display name - #984

Open
syf2211 wants to merge 2 commits into
taylorwilsdon:mainfrom
syf2211:fix/send-gmail-message-signature-from-name
Open

fix(gmail): request settings scope and auto-fill send-as display name#984
syf2211 wants to merge 2 commits into
taylorwilsdon:mainfrom
syf2211:fix/send-gmail-message-signature-from-name

Conversation

@syf2211

@syf2211 syf2211 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix send_gmail_message for Service Account + Domain-Wide Delegation mode by requesting gmail.settings.basic and auto-filling the From display name from Gmail send-as settings when from_name is omitted.

Fixes #752

Motivation

In SA+DWD mode, credentials only include scopes declared on the tool decorator. send_gmail_message previously requested send/read scopes only, so the send-as settings lookup for signatures failed with an auth error and was silently ignored. Separately, when callers omitted from_name, outbound mail used a bare email address even though Gmail stores a displayName on the send-as entry.

Changes

  • Add gmail_settings_basic to the send_gmail_message scope requirements.
  • Introduce shared send-as helpers (_get_send_as_settings, _select_send_as_entry) so signature and display name come from one settings round trip.
  • Auto-populate from_name from send-as displayName when the caller does not provide one.
  • Add regression tests for display-name autofill and explicit-name precedence.

Tests

python3 -m pytest tests/gmail/test_draft_gmail_message.py -k "send_gmail_message" -v

Result: 8 passed

Notes

  • send_gmail_message now requires gmail.settings.basic even when include_signature=False and from_name is provided, so existing send-only grants may need DWD/OAuth re-consent.
  • Forward and draft paths are unchanged in this PR; happy to follow up if maintainers want parity.

Summary by CodeRabbit

  • New Features
    • Gmail messages can now automatically use the configured send-as display name when no sender name is provided.
    • Explicit sender names still take precedence over send-as display names.
    • Signature selection now matches the requested sender (case-insensitively) and falls back to the primary/first send-as configuration when needed.
  • Bug Fixes
    • Improved sending resilience: benign authorization/scope failures for send-as settings no longer prevent message sending.
  • Tests
    • Expanded coverage for sender-name/signature precedence and send-as display name behavior.

Fixes taylorwilsdon#752

Service Account + DWD mode only grants scopes declared on send_gmail_message.
Add gmail.settings.basic so signature fetch works, and reuse one send-as
settings lookup to populate From displayName when from_name is omitted.
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

send_gmail_message now retrieves send-as signature HTML and display name through one scope-gated settings lookup, preserves benign lookup failures, resolves sender-name precedence, and passes the resolved name into message preparation. Tests cover explicit names, automatic display names, disabled signatures, and URL attachments.

Changes

Gmail send-as settings

Layer / File(s) Summary
Shared send-as settings retrieval
gmail/gmail_tools.py
Send-as selection matches from_email case-insensitively, then falls back to the primary or first entry, returning signature HTML and display name. Benign authentication or scope failures return empty settings.
Send composition and validation
gmail/gmail_tools.py, tests/gmail/test_draft_gmail_message.py
A scope-gated wrapper supplies settings when signatures are enabled or from_name is omitted; message preparation uses the resolved name, with tests covering precedence and lookup suppression.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant send_gmail_message
  participant GmailSettings
  participant _prepare_gmail_message
  Caller->>send_gmail_message: send message
  send_gmail_message->>GmailSettings: list sendAs settings when needed
  GmailSettings-->>send_gmail_message: signature HTML and display name
  send_gmail_message->>_prepare_gmail_message: body and resolved_from_name
  _prepare_gmail_message-->>send_gmail_message: prepared message
Loading

Possibly related PRs

Suggested labels: bug

Suggested reviewers: taylorwilsdon, 123andy

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change well, but it omits the repository's required template sections and checkbox checklist. Rewrite the PR description to match the template, including Description, Type of Change, Testing, Checklist, and Additional Notes sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main Gmail scope and display-name autofill changes.
Linked Issues check ✅ Passed The implementation matches #752 by using send-as settings for signature and display-name autofill with a safe fallback on lookup failure.
Out of Scope Changes check ✅ Passed The changes stay focused on Gmail send-as settings handling and related tests, with no clear unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Fix failing CI checks
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@gmail/gmail_tools.py`:
- Around line 2176-2178: Update the require_google_service usage around the
forwarding flow to avoid requiring gmail_settings_basic when forward_message_id
triggers the early return. Preserve the existing forwarding scope set while
retaining the settings scope for operations that need it, using
invocation-dependent scope selection or a separate forwarding path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cae5008f-41b9-45bc-876e-6bc77a2674b2

📥 Commits

Reviewing files that changed from the base of the PR and between d9d09fa and 5929369.

📒 Files selected for processing (2)
  • gmail/gmail_tools.py
  • tests/gmail/test_draft_gmail_message.py

Comment thread gmail/gmail_tools.py Outdated
Split send-as settings lookup into a separate authenticated helper that
requests gmail_settings_basic only for compose sends. Forwarding no longer
requires the settings scope, preserving DWD deployments without that grant.

Fixes CodeRabbit review on PR taylorwilsdon#984.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
gmail/gmail_tools.py (1)

2433-2442: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Preserve an explicitly empty sender name.

not resolved_from_name treats from_name="" as omitted and replaces it with the send-as display name. Use from_name is None for lookup/autofill checks so every explicitly supplied value takes precedence.

Proposed fix
-    if include_signature or not resolved_from_name:
+    if include_signature or from_name is None:
         signature_html, auto_display_name = await _get_send_as_settings_authenticated(
             user_google_email=user_google_email,
             from_email=sender_email,
         )
@@
-        if not resolved_from_name and auto_display_name:
+        if resolved_from_name is None and auto_display_name:
             resolved_from_name = auto_display_name
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gmail/gmail_tools.py` around lines 2433 - 2442, Update the sender-name
autofill condition in the flow around _get_send_as_settings_authenticated so the
send-as display name is applied only when from_name is None, preserving an
explicitly supplied empty string while retaining the existing auto_display_name
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@gmail/gmail_tools.py`:
- Around line 2433-2442: Update the sender-name autofill condition in the flow
around _get_send_as_settings_authenticated so the send-as display name is
applied only when from_name is None, preserving an explicitly supplied empty
string while retaining the existing auto_display_name behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 59e7a082-9a41-4936-a2cb-5b06abf5a54f

📥 Commits

Reviewing files that changed from the base of the PR and between 5929369 and e6c5a96.

📒 Files selected for processing (2)
  • gmail/gmail_tools.py
  • tests/gmail/test_draft_gmail_message.py

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.

send_gmail_message: signature silently drops + From display name not auto-fetched from send-as in SA+DWD mode

1 participant