smtpgoogle-apismtp-authgoogle-email-settings-api

How do I reliably verify if I can send an email via Google's smtp servers, for an email address that may or may not be a send-as alias?


I'm using Google's smtp servers to send email using oauth2. If I want to send with a different 'from' address, Google lets me specify one. If the from address is a verified send-as alias for the account, Google will use it in the email. If it's not a verified send-as alias, Google will silently (I think) swap the from address.

Is there any way I can tell via API whether Google will use the supplied email address? Maybe I'm asking if there's any way I can tell if an email is a send-as address for an account, but I'm open to other solutions for the bigger problem of knowing whether Google will honor the supplied from address.


Solution

  • If these are Google Apps accounts, you can use the Email Settings API to list send as addresses for the account.

    If they're not Google Apps accounts or you prefer a more universal approach you could:

    1. Send an email from the address to the authenticated user directly via SMTP. Be sure to set a unique message ID before sending that you can track the message via.

    2. Connect to the user's mailbox via IMAP using OAuth authentication (same API scope as SMTP) and search for the sent message via message ID (rfc822msgid).

    3. Grab the headers of the message and look at the From: header. If it matches what you sent, the user is allowed to send as that address. If it's been reset to the authenticated user's primary address, the user is not allowed to send as that address.