Python, 无法从 Google Workspace 邮件地址发送邮件到个人 Gmail 地址。

huangapple go评论55阅读模式
英文:

Python, unable to send mail from google workspace mail address to individual gmail address

问题

Here's the translated code portion:

import base64
from google.oauth2 import service_account
from googleapiclient.errors import HttpError
from googleapiclient.discovery import build
from googleapiclient import errors
from email.mime.text import MIMEText 

# 使用JSON文件中的凭据进行身份验证
creds = credentials
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
SERVICE_ACCOUNT_FILE = 'C:/Users/Desktop/service-account-gmail-app.json'
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# 连接 Gmail API
service = build('gmail', 'v1', credentials=creds)

def send_email(service, to, subject, body, sender):
    try:
        message = create_message(to, subject, body, sender)
        send_message(service, message)
        print('电子邮件发送成功')
    except HttpError as error:
        print(f'电子邮件未发送:{error}')

def create_message(to, subject, body, sender):
    message = MIMEText(body)
    message['to'] = to
    message['subject'] = subject
    message['body'] = body
    message['from'] = sender
    return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}

def send_message(service, message):
        message = (service.users().messages().send(userId="me", body=message).execute())

to = '个人 Gmail 地址在此'
subject = '测试邮件'
body = '你好,这是一封测试消息。'
sender = 'Google Workspace 电子邮件地址在此(包括域信息)'
message = create_message(to, subject, body, sender)
print(message)
send_email(service, to, subject, body, sender)

I've provided the translated code as requested. If you have any further questions or need assistance with the issue you described, please feel free to ask.

英文:
import base64
from google.oauth2 import service_account
from googleapiclient.errors import HttpError
from googleapiclient.discovery import build
from googleapiclient import errors
from email.mime.text import MIMEText 

# Authenticate with credentials from JSON file
creds = credentials
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
SERVICE_ACCOUNT_FILE = 'C:/Users/Desktop/service-account-gmail-app.json'
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# connect gmail api
service = build('gmail', 'v1', credentials=creds)

def send_email(service, to, subject, body, sender):
    try:
        message = create_message(to, subject, body, sender)
        send_message(service, message)
        print('E-mail was send successfully')
    except HttpError as error:
        print(f'E-mail was not sent: {error}')

def create_message(to, subject, body, sender):
    message = MIMEText(body)
    message['to'] = to
    message['subject'] = subject
    message['body'] = body
    message['from'] = sender
    return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}

def send_message(service, message):
  
        message = (service.users().messages().send(userId="me", body=message).execute())
       

to = 'individual gmail adress here'
subject = 'Test e-mail'
body = 'Hello this is a test message.'
sender='google workspace e-mail address here (including domain information) '
message = create_message(to, subject, body,sender)
print(message)
send_email(service, to, subject, body, sender)

The error:

E-mail was not sent: <HttpError 400 when requesting https://gmail.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Precondition check failed.". Details: "[{'message': 'Precondition check failed.', 'domain': 'global', 'reason': 'failedPrecondition'}]">

Hello friends, I have done the following operations, but I cannot send an e-mail to my e-mail address if it is an individual from my google workspace account:

  1. I added the recipient e-mail address to google workspace > Manage address lists
  2. I have activated the gmail api in the new project I created google cloud.
  3. I added 'google workspace e-mail address' to OAuth consent screen test users.
  4. In Authorized domains section of OAuth consent screen, I added the domain address that I received in the google workspace.
  5. In the Gmail scopes section of the OAuth consent screen, I granted the following permissions:
  • Gmail API .../auth/gmail.modify Read, compose and send emails from your Gmail account

  • Gmail API .../auth/gmail.compose Manage drafts and send emails

  • Gmail API .../auth/gmail.readonly View your email messages and settings

  1. I choosed "actions admin" role for the service account
  2. I created a service account from the Create cerdientials section and saved the resulting
    key in json format where I ran the code

Why can't I send e-mails from my google workspace account to my personal e-mail address even though I have done these procedures? How will I solve the problem?

答案1

得分: 1

你需要将权限委派给您域上的用户。

delegated_creds=credentials.with_subject(&quot;someuser@yourdomain.com)`

如果未配置域范围的委派,则服务帐户没有权限代表用户发送电子邮件。

英文:

you need to deligate to a user on your domain

delegated_creds=credentials.with_subject(&quot;someuser@yourdomain.com

without domain wide deligation configured the service account doesn't have permission to send an email on the users behalf

huangapple
  • 本文由 发表于 2023年4月17日 23:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定