Бортовой журнал Ктулху

Gmail API message.send using PHP and curl

This code example shows how to send email using Gmail API method 'message.send' and PHP.

 

First of all, you need obtain access token and only after that you may send email.

Here an example of PHP code:

$AccessToken = '>>> place your access token here <<<';
$message = "TO: RECIPENT@EXAMPLE>COM\r\nFrom: SENDER@EXAMPLE>COM\r\nSubject: GMail test.\r\n My message";
$ch = curl_init('https://www.googleapis.com/upload/gmail/v1/users/me/messages/send');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PROXY, PROXY);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, PROXYAUTH);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $AccessToken", 'Accept: application/json', 'Content-Type: message/rfc822'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
$data = curl_exec($ch);
echo "<pre>";
var_dump($data);
echo "</pre>";
exit;

I've got this response:

 

string(97) "{

"id": "1872d93878xxxx",

"threadId": "1872d93878xxxx",

"labelIds": [

"SENT"

]

}
"

2023 03 29 16.52.40