Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Gmail API和PHP转发一条消息?_Php_Google Api_Google Api Php Client - Fatal编程技术网

如何使用Gmail API和PHP转发一条消息?

如何使用Gmail API和PHP转发一条消息?,php,google-api,google-api-php-client,Php,Google Api,Google Api Php Client,我想有一个脚本,以便转发一个选定的消息。执行脚本时,出现下一个错误: An error occurred: { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "'raw' RFC822 payload message string or uploading message via /uplo

我想有一个脚本,以便转发一个选定的消息。执行脚本时,出现下一个错误:

An error occurred: { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required" } ], "code": 400, "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required" } } 发生错误:{ “错误”:{ “错误”:[ { “域”:“全局”, “理由”:“无效辩论”, “消息”:“原始”RFC822有效负载消息字符串或通过/upload/*URL上传消息需要” } ], “代码”:400, “消息”:“原始”RFC822有效负载消息字符串或通过/upload/*URL上传消息需要” } } 我使用以下脚本:

<pre>

require __DIR__ . '/vendor/autoload.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Send Message.
 *
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  Google_Service_Gmail_Message $message Message to send.
 * @return Google_Service_Gmail_Message sent Message.
 */
function sendMessage($service, $userId, $message) {
  try {
    $message = $service->users_messages->send($userId, $message);
    print 'Message with ID: ' . $message->getId() . ' sent.';
    return $message;
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
}


//MAIN

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
$user = 'me';

// Get the messages in the user's account.
$messages = listMessages($service, $user, [
    'maxResults' => 20, // Return 20 messages.
    'labelIds' => 'INBOX', // Return messages in inbox.
    'q' => 'From:xxx@yyy.zzz'
]);

$email_forward = 'email_to_forward@gmail.com';

foreach ($messages as $message) {
    print 'Message with ID: ' . $message->getId() . "|";
    sendMessage($service, 'me', $message);

}
</pre>

需要“目录”/供应商/autoload.php';
如果(php_sapi_name()!='cli'){
抛出新异常('此应用程序必须在命令行上运行');
}
/**
*发送消息。
*
*@param Google_Service_Gmail$Service授权Gmail API实例。
*@param string$userId用户的电子邮件地址。“我”的特殊价值
*可用于指示经过身份验证的用户。
*@param Google_Service_Gmail_Message$要发送的消息。
*@return Google\u Service\u Gmail\u Message发送消息。
*/
函数sendMessage($service、$userId、$message){
试一试{
$message=$service->users\u messages->send($userId,$message);
打印“ID为的邮件”。$Message->getId()。“已发送”;
返回$message;
}捕获(例外$e){
打印“出现错误:”。$e->getMessage();
}
}
//主要
//获取API客户端并构造服务对象。
$client=getClient();
$service=新的Google\u服务\u Gmail($client);
$user='me';
//获取用户帐户中的消息。
$messages=listMessages($service,$user[
'maxResults'=>20,//返回20条消息。
'LabelID'=>'INBOX',//在INBOX中返回邮件。
“q'=>”来自:xxx@yyy.zzz'
]);
$email\u forward='email\u to_forward@gmail.com';
foreach($messages作为$message){
打印“ID为的消息”。$Message->getId()。“|”;
sendMessage($service,$me',$message);
}
这是一条只有文本的消息,没有任何附件。有人能帮我吗


谢谢。

如果是一次性的,你可以使用一个免费的Zapier帐户。我注意到你从未包含要查询的google API url。这就是你用过的所有代码吗?不,这不是一次性的。为了编写更大的脚本,这只是一个小测试。谢谢。我有一个“包含”来包含url和其他信息。我没有任何代码错误。感谢您使用的是一个过时的库,不支持名称空间。你应该切换到v2 php api:如果是一次性的,你可以使用一个免费的Zapier帐户。我注意到你从未包含要查询的google api url。这就是你用过的所有代码吗?不,这不是一次性的。为了编写更大的脚本,这只是一个小测试。谢谢。我有一个“包含”来包含url和其他信息。我没有任何代码错误。感谢您使用的是一个过时的库,不支持名称空间。您应该切换到v2 php api: