-
I need a little help with my setup. Any help will be greatly appreciated. I am trying to send emails from my website on behalf of any/all users on my domain in my Workspace account. Some of these are forms and some are cron jobs. Note: This is a "service account" so this does not require individual users to authenticate using a consent screen. Here's what I have done:
Now I am trying to figure out how to set up phpMailer to use send mails using this information. I have read so many "suggestions" and nothing has worked. I keep getting connection errors.
I have tried methods passing the JSON file, getting a token from the file and passing that, and several others. I would greatly appreciate any input from someone who has gotten this to work using a service account. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
This is not getting as far as attempting to authenticate; something is going wrong prior to that. I suggest you enable low-level debug output, which should show you more info about what's going on. At a guess, I'd say you're selecting an invalid auth mechanism, but I can't tell without seeing your code and then debug detail. As far as I'm aware, Gmail service accounts use "app passwords", which work just like regular passwords. There isn't a way to pass a JSON file to SMTP login mechanisms. |
Beta Was this translation helpful? Give feedback.
-
I assume that in your example you are using normal client_credentials flow and not a JWT signed by your client (both are valid). When your client received the offer of the six authentication methods (LOGIN PLAIN XOAUTH2, etc), it should have responded:
followed by a base64 access token (see below)
Your access token is set up when you initially call Google API, e.g.
and returned when you use e.g.
and then select the token itself from this four-element array with:
GMAIL_XOUTH2_CREDENTIALS (above) is typically your JSON's 'gmail-xoauth2-credentials.json', and mail.google.com is probably the only scope value you need (too ‘wide’ perhaps, but you can always refine it later). |
Beta Was this translation helpful? Give feedback.
-
Okay, solved this. Going to leave this here for anyone else who is looking to make this work. Using the Gmail API directly with PHPMailer is not the typical approach because PHPMailer is primarily designed for SMTP communication, while the Gmail API uses HTTP requests to interact with Gmail. However, you can still leverage PHPMailer's capabilities for building MIME email messages, which can be useful if you're comfortable with PHPMailer's API for composing emails but want to send them through the Gmail API. Here's the approach on how you might combine PHPMailer for creating the email message and the Gmail API for sending the message: Compose the email with PHPMailer: Use PHPMailer to create your MIME message. This is helpful if you want to take advantage of PHPMailer's features, like adding attachments or using HTML templates. `$mail = new PHPMailer(); $mail->setFrom('from@example.com', 'From Name'); // PHPMailer 6.5.0 introduced getSentMIMEMessage() method to retrieve the MIME message Convert the PHPMailer-generated email to a format suitable for the Gmail API: Extract the MIME string from PHPMailer without actually sending it via SMTP. PHPMailer generates the email as a MIME string, which is exactly what the Gmail API expects. However, the Gmail API requires the message to be base64url encoded.
Send the email using the Gmail API: Use the Gmail API to send the MIME message. `$client = new Client(); $service = new Gmail($client); $message = new Message(); try { Hope this helps someone. |
Beta Was this translation helpful? Give feedback.
-
Your post above of Thu 15/02/2024 15:20 UTC just beat the one I was composing, but I though it worth posting anyway as your post and (just possibly) mine may help others. A couple of quick points (I am travelling at the moment):
a. the ‘aud’ claim in the token referring to something other the resource to be used b. the scope not allowing SMTP sending (which may not be the same thing as when sending direct from the account – it isn’t in the Microsoft world where the scope operands can even determine the ‘aud’!) or being ‘wider’ in permission terms than that registered centrally for the app. It is also worth noting that using PHPMAiler with Google authorisation code grant and Gmail seems straightforward and works out of the box. I have however experienced a number of oddball problems with service accounts: although obtaining access tokens has never been an issue, Gmail SMTP seems to be a delicate flower when accepting a proffered access token and returning ‘authorised’ status. |
Beta Was this translation helpful? Give feedback.
Okay, solved this. Going to leave this here for anyone else who is looking to make this work.
Using the Gmail API directly with PHPMailer is not the typical approach because PHPMailer is primarily designed for SMTP communication, while the Gmail API uses HTTP requests to interact with Gmail. However, you can still leverage PHPMailer's capabilities for building MIME email messages, which can be useful if you're comfortable with PHPMailer's API for composing emails but want to send them through the Gmail API.
Here's the approach on how you might combine PHPMailer for creating the email message and the Gmail API for sending the message:
Compose the email with PHPMailer: Use PHPMailer to crea…