Having trouble getting a Python script to send emails from a Gmail alias

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.

New contributor

Marky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật