Как переслать одно сообщение с помощью API Gmail и PHP? - PullRequest
1 голос
/ 05 февраля 2020

Я хотел бы иметь один скрипт для пересылки одного выбранного сообщения. Когда я выполняю свой скрипт, у меня появляется следующая ошибка:


    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"
     }
    }
    

Я использую этот скрипт:

<code><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);

}

Это одно сообщение только с текстом, без каких-либо крепление. Кто-нибудь может мне помочь, пожалуйста?

Спасибо.

...