I have an email address, [email protected], via Google Workspace; it’s at a custom domain name but it functions as a Gmail address that I use via the Gmail web interface. I’ve also made an alias for it: [email protected]. Via the web interface, emails can manually be sent from this alias instead of from info.
I have a Python script that I want to send emails from the reminders alias. This WAS working correctly. Then, I made some small tweaks to how the script handles tokens, deleted the token.pickle file, and generated a new one. Since then, the script has sent emails only from the info address, not the reminders alias. Nothing else has changed, so I assume that the problem is somehow token-related, but I can’t figure out what it could possibly be.
Here’s the relevant bit of the script:
token_path = os.getenv('TOKEN_PATH')
creds_gmail = None
if os.path.exists(token_path):
with open(token_path, 'rb') as token:
creds_gmail = pickle.load(token)
if not creds_gmail or not creds_gmail.valid:
if creds_gmail and creds_gmail.expired and creds_gmail.refresh_token:
try:
creds_gmail.refresh(Request())
except RefreshError:
raise Exception("Failed to refresh token.")
else:
flow = InstalledAppFlow.from_client_secrets_file(os.getenv('CREDENTIALS_GMAIL'), scopes)
creds_gmail = flow.run_local_server(port=0)
with open(token_path, 'wb') as token:
pickle.dump(creds_gmail, token)
try:
service_gmail = build('gmail', 'v1', credentials=creds_gmail)
except Exception as e:
print(f'Failed to build service: {e}')
service_gmail = None
And here’s the separate script I used to regenerate a new token.pickle file:
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle
client_secrets_file = 'credentials2.json'
scopes = ['https://www.googleapis.com/auth/gmail.send',
'https://www.googleapis.com/auth/spreadsheets']
def main():
flow = InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes=scopes)
creds = flow.run_local_server(port=8080)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
print("token.pickle has been created.")
if __name__ == '__main__':
main()
Can anyone help me figure out what I need to do in order to get it to send from the reminders alias again? (I don’t think the answer involves anything else in the code, i.e. how emails are constructed, as all of it is exactly the same as what previously worked perfectly.)
I’ve tried regenerating the token several times, and I’ve tried revoking and reauthorizing permission.
Marky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.