Gmail API emails appear sent but don't reach recipients - delivery issues

I’ve been using Gmail with my company domain through Google SMTP for manual email sending for over a year without problems. Now I’m trying to automate this process using the Gmail API.

I can successfully create drafts, read labels, and send messages through the API. The emails show up as sent in my Gmail interface, but recipients never receive them (not even in spam).

I think the issue might be one of these:

  • Emails are flagged as suspicious and filtered out
  • The API only marks emails as sent without actually delivering them
  • My app needs to be published instead of running in test mode

When I compare email headers from manual sends vs API sends, I see this suspicious header:

Received: from 814388093175 named unknown by gmailapi.google.com with HTTPREST; Thu, 27 Jun 2024 01:16:36 -0400

This “unknown” source probably causes delivery servers to reject the emails. How can I fix this?

Here’s my code:

import base64
from email.message import EmailMessage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow

def build_email_content(recipient_name, target_email, sender_email):
    email_obj = EmailMessage()
    email_obj['Subject'] = f'Hello {recipient_name}'
    email_obj['From'] = sender_email
    email_obj['To'] = target_email
    email_obj.set_content('Welcome message')
    
    with open('template.html', 'r') as template_file:
        html_content = template_file.read()
    
    return {"raw": base64.urlsafe_b64encode(email_obj.as_bytes()).decode()}

def execute_send():
    credentials = None
    PERMISSIONS = ["https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/gmail.compose", "https://www.googleapis.com/auth/gmail.readonly", "https://mail.google.com/"]
    
    if os.path.exists("auth_token.json"):
        credentials = Credentials.from_authorized_user_file("auth_token.json", PERMISSIONS)
    
    if not credentials or not credentials.valid:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            auth_flow = InstalledAppFlow.from_client_secrets_file("client_secrets.json", PERMISSIONS)
            credentials = auth_flow.run_local_server(port=0)
        
        with open("auth_token.json", "w") as token_file:
            token_file.write(credentials.to_json())
    
    try:
        gmail_service = build("gmail", "v1", credentials=credentials)
        email_message = build_email_content('John Smith', '[email protected]', '[email protected]')
        
        result = gmail_service.users().messages().send(userId="me", body=email_message).execute()
        print(f'Sent message ID: {result["id"]}')
        
    except HttpError as err:
        print(f"Error occurred: {err}")

if __name__ == "__main__":
    execute_send()

that’s strange! I’ve used the gmail api too, and haven’t faced this. that “unknown” header could be the culprit. def check your SPF and DKIM settings. and yeah, ensure your oauth app is verified; google is strict about that.

This is super common when switching from SMTP to Gmail API. That “unknown” header is definitely what’s killing your delivery rates - mail servers hate seeing that. I ran into the exact same thing setting up Gmail API for client notifications. Your code’s probably fine, but Google doesn’t trust your app yet. When you send through the API without proper domain verification, Google basically flags your emails as coming from nowhere. Here’s what fixed it for me: verify your domain in Google Search Console and connect it to your Google Cloud project. This gets rid of that nasty “unknown” header. Also make sure your OAuth consent screen lists your verified domain as authorized. Test mode makes this way worse too. Google’s really strict with unverified apps. I’d either push through the verification process or switch back to SMTP temporarily while you get the auth sorted out.

Had the same issue when I switched from SMTP to Gmail API last year. It’s usually authentication scope problems mixed with domain reputation stuff. Your code looks fine - the real issue is how Google treats API emails differently than regular ones.

Check your OAuth consent screen in Google Cloud Console first. Even in test mode, sketchy domain verification kills email delivery silently. That weird header you’re seeing? That’s Google saying your app isn’t properly identified in their system.

Fix it by setting up proper DMARC policies for your Google Workspace domain. Also try creating drafts first, then sending them instead of direct sends - sometimes this tricks Google into thinking it’s a normal email workflow.

What worked for me: I went back to SMTP for a few days to rebuild my sending reputation, then slowly moved API sends back in. Google’s spam filters hate sudden changes in how you send emails.