Need PHP code to Oauth2 authenticate and send email via Gmail API without using google-api-php-client

1 week ago 9
ARTICLE AD BOX

I've learned that Digital Ocean doesn't allow outgoing SMTP requests on port 587 from their droplet servers so I am trying to find the most compact PHP code available to send email using the Gmail API. It's not practical for me to use Google's official PHP client for the api, google-api-php-client, which consists of a ludicrous 3,984,473 lines of PHP code in 32,365 files -- 180MB!.

I therefore need some PHP code which can

Obtain the access token to be used for sending mail. I don't think I'll have or need any redirect url -- I'm not trying to set up a page for users of my site to authorize stuff, I simply want to send email from my own gmail account. Recognize if the access token is old and refresh the token, if necessary. Use the access token to contact the Gmail API and send an email message using my gmail account.

I've enabled the Gmail API in my google cloud console, but the process to obtain credentials assumes that I'm constructing a public-facing form to deploy to a website so I can obtain credentials from visitors to my site. This is not what I'm trying to do. I simply want to obtain some kind of API key so I can send email from my website using my own Gmail account. So that's Question 1: How do I obtain credentials (presumably Oauth2) to access Gmail API?

Question 2: What PHP code do I need to obtain and/or refresh the OAuth2 access token that I will use to contact the Gmail API? I cannot use the google-api-php-client. I've seen this post but it appears to have a $client_id and $client_secret for credentials and also a $redirect_url, which I don't think I need?

And Question 3: Assuming I have a valid access token, what PHP code (presumably using CURL) can I use to send an email message generated in PHP? My google search yielded some pretty interesting code but it assumes I have a $client_id, $client_secret, and $access_token:

// Manually construct the email headers and body to comply with MIME standards. function create_mime_message($to, $from, $subject, $body) { $mime_message = "To: $to\r\n"; $mime_message .= "From: $from\r\n"; $mime_message .= "Subject: $subject\r\n"; $mime_message .= "MIME-Version: 1.0\r\n"; $mime_message .= "Content-Type: text/plain; charset=utf-8\r\n"; $mime_message .= "Content-Transfer-Encoding: base64\r\n\r\n"; $mime_message .= base64_encode($body) . "\r\n"; // Body should also be encoded return $mime_message; } // The entire raw MIME message string needs to be base64url-encoded for the Gmail API. function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } $raw_message = create_mime_message("[email protected]", "[email protected]", "Test Subject", "This is the email body."); $encoded_message = base64url_encode($raw_message); // Use PHP's cURL extension to send a POST request to the https://www.googleapis.com endpoint, including the access token in the headers and the encoded message in the JSON payload $access_token = "YOUR_VALID_ACCESS_TOKEN"; // Must be obtained via OAuth2 $post_data = json_encode(['raw' => $encoded_message]); $ch = curl_init('https://www.googleapis.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Consider carefully in production curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $access_token", 'Accept: application/json', 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { echo "Email sent successfully!"; } else { echo "Failed to send email. Response: " . $response; }

If someone could help me figure out how to obtain client_id, client_secret, and access_token, I might be able to try out this code.

In the meantime, I'm begging Digital Ocean to just let me send SMTP mail on port 587.

Read Entire Article