You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Service Gmail创建Send As账户时遇500内部错误

问题描述

我正在更新PHP应用,通过Google Service Gmail创建新的Send As账户。代码能正常连接账户并查看别名信息,连接无异常,但在添加带SMTP MSA信息的Send As账户时触发500内部错误,且该代码此前运行正常,近期突然报错。

相关代码

$scopes = array(
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.domain',
    'https://www.googleapis.com/auth/siteverification',
    'https://www.googleapis.com/auth/gmail.settings.sharing',
    'https://www.googleapis.com/auth/gmail.settings.basic'
);

$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject('mymainemail@email.com');
$client->setScopes($scopes);  

try {
    $gmail = new Google_Service_Gmail($client);

    $smtpadd = new Google_Service_Gmail_SendAs(array(
        'displayName' => 'display name',
        'sendAsEmail' => 'myalias@email.com',
        'treatAsAlias' => false, 
        'isPrimary' => 'true', 
        'isDefault' => 'false', 
        'smtpMsa' => array(
            'host' => 'smtp.host',
            'port' => 587,
            'username' => 'username',
            'password' => 'mypassword'
        )
    ));

    $results = $gmail->users_settings_sendAs->create('mymainemail@email.com',$smtpadd);

} catch (Exception $e) {
    print "Error" . $e->getMessage();
}

错误信息

Google\Service\Exception: { 
    "error": 
        { 
        "code": 500, 
        "message": "Internal error encountered.", 
        "errors": [ 
            { 
            "message": "Internal error encountered.", 
            "domain": "global", "reason": "backendError" 
        } ], 
        "status": "INTERNAL" 
        } 
    }

排查方案

  • 修正参数类型:isPrimary和isDefault需传入布尔值而非字符串,将代码中的'true'改为true,'false'改为false。API可能近期收紧了参数类型校验,这是常见触发原因。
  • 校验SMTP配置:确认smtp.host为有效SMTP地址,username和password未过期、权限正常,测试手动使用该SMTP配置能否发送邮件。
  • 检查权限与配额:登录Google Cloud控制台,确认服务账号仍持有gmail.settings.basic和gmail.settings.sharing权限;查看Gmail API配额,确认未达请求上限。
  • 添加重试逻辑:500错误属于后端临时故障,实现指数退避重试机制,避免单次请求失败终止流程。
  • 验证别名合法性:确认myalias@email.com所属域名已在Google Workspace完成验证,且该别名未被其他账户占用。

内容的提问来源于stack exchange,提问作者stylez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 02:55:42