send email using gmail-api and google-api-php-client

后端 未结 5 1419
感动是毒
感动是毒 2021-01-16 03:24

I am using https://github.com/google/google-api-php-client and I want to send a test email with a user\'s authorized gmail account.

This is what I have so far:

5条回答
  •  情深已故
    2021-01-16 04:01

    Perhaps this is a bit beyond the original question, but at least in my case that "test email" progressed to regularly sending automated welcome emails "from" various accounts. Though I've found much that is helpful here, I've had to cobble things together from various sources.

    In the hope of helping others navigate this process, here's a distilled version of what I came up with.

    A few notes on the following code:

    1. This assumes that you've jumped through the hoops to create and download the JSON for a Google Service Account (under credentials in the developer dashboard it's the bottom section.)
    2. For clarity, I've grouped all the bits you should need to set in the "Replace this with your own data" section.
    3. That me in the send() method is a key word that means something like "send this email with the current account."
    // This block is only needed if you're working outside the global namespace
    use \Google_Client as Google_Client;
    use \Google_Service_Gmail as Google_Service_Gmail;
    use \Google_Service_Gmail_Message as Google_Service_Gmail_Message;
    
    // Prep things for PHPMailer
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\PHPMailer;
    
    // Grab the needed files
    require_once 'path/to/google-api/vendor/autoload.php';
    require_once 'path/to/php-mailer/Exception.php';
    require_once 'path/to/php-mailer/PHPMailer.php';
    
    // Replace this with your own data
    $pathToServiceAccountCredentialsJSON = "/path/to/service/account/credentials.json";
    $emailUser = "sender@yourdomain.com"; // the user who is "sending" the email...
    $emailUserName = "Sending User's Name"; // ... and their name
    $emailSubjectLine = "My Email's Subject Line";
    $recipientEmail = "recipient@somdomain.com";
    $recipientName = "Recipient's Name";
    $bodyHTML = "

    Paragraph one.

    Paragraph two!

    "; $bodyText = "Paragraph one.\n\nParagraph two!"; // Set up credentials and client putenv("GOOGLE_APPLICATION_CREDENTIALS={$pathToServiceAccountCredentialsJSON}"); $client = new Google_Client(); $client->useApplicationDefaultCredentials(); // We're only sending, so this works fine $client->addScope(Google_Service_Gmail::GMAIL_SEND); // Set the user we're going to pretend to be (Subject is confusing here as an email also has a "Subject" $client->setSubject($emailUser); // Set up the service $mailService = new Google_Service_Gmail($client); // We'll use PHPMailer to build the raw email (since Google's API doesn't do that) so prep it $mailBuilder = new PHPMailer(); $mailBuilder->CharSet = "UTF-8"; $mailBuilder->Encoding = "base64"; // Not set up the email you want to send $mailBuilder->Subject = $emailSubjectLine; $mailBuilder->From = $emailUser; $mailBuilder->FromName = $emailUserName; try { $mailBuilder->addAddress($recipientEmail, $recipientName); } catch (Exception $e) { // Handle any problems adding the email address here } // Add additional recipients, CC, BCC, ReplyTo, if desired // Then add the main body of the email... $mailBuilder->isHTML(true); $mailBuilder->Body = $bodyHTML; $mailBuilder->AltBody = $bodyText; // Prep things so we have a nice, raw message ready to send via Google's API try { $mailBuilder->preSend(); $rawMessage = base64_encode($mailBuilder->getSentMIMEMessage()); $rawMessage = str_replace(['+', '/', '='], ['-', '_', ''], $rawMessage); // url safe $gMessage = new Google_Service_Gmail_Message(); $gMessage->setRaw($rawMessage); // Send it! $result = $mailService->users_messages->send('me', $gMessage); } catch (Exception $e) { // Handle any problems building or sending the email here } if ($result->labelIds[0] == "SENT") echo "Message sent!"; else echo "Something went wrong...";

提交回复
热议问题