Email rejected by the Gmail API because of To header - google-api

I'm trying to send a simple email via the Gmail API that's failing because of the RFC822-compliant To: undisclosed-recipients:; header. Sending with a regular address in the To header works fine. The RFC822 message is created by PHPMailer and recovered using preSend() and getSentMIMEMessage().
What am I doing wrong?
The message
Date: Tue, 21 Sep 2021 09:13:17 +0000
From: Rec1 <fictive1#gmail.com>
Cc: "Rec2 (fictive2#gmail.com)" <fictive2#gmail.com>
Bcc: fictive3#gmail.com
Message-ID: <vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg#vps>
X-Mailer: PHPMailer 6.5.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg"
Content-Transfer-Encoding: 8bit
To: undisclosed-recipients:;
Subject: test
This is a multi-part message in MIME format.
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/plain; charset=us-ascii
body
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/html; charset=us-ascii
<div dir="ltr">body</div>
--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg--
The answer
{
"error": {
"code": 400,
"message": "Invalid To header",
"errors": [
{
"message": "Invalid To header",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}

Try manually adding undisclosed-recipients:; then add bcc.
$mail->AddAddress("undisclosed-recipients:;");
$mail->AddBCC("username#domain.com");
Worked on this post:
Sending mail with Phpmailer, BCC only, hiding TO header field
You have different issues but it might also solve the issue regarding invalid to header if set manually.

Related

Create email with Gmail API

I'm testing Gmail API here https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create but is it possible to create a full email with sender, subject, body and load a gmail template and send it through curl command?
This is what I tried but I am getting an empty message:
How to send an email based on a draft with the Gmail API
This is one approach you could take:
Using users.draft list
{
"drafts": [
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx"
}
}
],
"resultSizeEstimate": 1
}
Then users.draft get with one of the id in the previous request:
You can retrieve the current MIME message contained in the draft by calling drafts.get with the parameter format=raw.
(The data is sanitized)
{
"id": "r-1561xxx",
"message": {
"id": "179891xxx",
"threadId": "179891xxx",
"labelIds": [
"DRAFT"
],
"snippet": "Name: Reason: Company:",
"sizeEstimate": 635,
"raw": "TUlNRS1WZXJzaW9uOiAxLjAKRGF0ZTogRnJpLCAxMyBBdWcgMjAyMSAxNjoxMjowOSArMDIwMApNZXNzYWdlLUlEOiA8Q0FGZHVrYm5yZHhReHh4eEBtYWlsLmdtYWlsLmNvbT4KU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6CgpSZWFzb246CgpDb21wYW55OgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6PGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=",
"historyId": "67xxx",
"internalDate": "1628xxx"
}
}
The raw attribute is where the message is. It's encoded in base64. Decoded it looks like this:
MIME-Version: 1.0
Date: Fri, 13 Aug 2021 16:12:09 +0200
Message-ID: <CAFdukbnrdxQxxxx#mail.gmail.com>
Subject: draft1
From: Sender <sample#gmail.com>
To: Receiver <sample#gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name:
Reason:
Company:
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name:<div><br></div><div>Reason:</div><div><br></div><div>Company:</div></div>
--000000000000eb4b5805c9716fd2--
Sending
In the guide it says:
Create the email content in some convenient way and encode it as a base64url string.
Create a new message resource and set its raw property to the base64url string you just created.
The text should conform to the rfc2822.
So make a plain text email like this (some field you can omit, like date and message ID):
MIME-Version: 1.0
Subject: draft1
From: Sender <sample#gmail.com>
To: Receiver <sample#gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"
--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"
Name: TooNetCreation
Reason: TooNetCreation
Company: TooNetCreation
--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Name: TooNetCreation<div><br></div><div>Reason: TooNetCreation</div><div><br></div><div>Company: TooNetCreation</div></div>
--000000000000eb4b5805c9716fd2--
Which encodes to:
TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=
Which you can send with:
curl --request POST \
'https://gmail.googleapis.com/gmail/v1/users/me/messages/send?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"raw":"TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0="}' \
--compressed
After which it should return an instance of the message and you will have send an email!
References
users.draft list
users.draft get
users.messages send
Sending email guide
Working with drafts guide

Comment on a post using linkedin api returns 403 Although i have all permissions required

i am trying to comment on a post using SocailActions Api
i am using the following permissions w_organization_social r_organization_social w_member_social
and i am logged in as a an admin of the page that i am trying to comment on its behalf
my request is :
POST https://api.linkedin.com/v2/socialActions/urn%3Ali%3Ashare%3AXXXXXXXXXX/comments HTTP/1.1
Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Host: api.linkedin.com
cache-control: no-cache,
X-Restli-Protocol-Version: 2.0.0
Accept: application/json
Content-Type: application/json
Content-Length: 136
{
"actor":"urn:li:organization:23741470",
"object" :"urn:Al:share:6664163994204549120",
"message":{
"text":"tessst"
}
}
and i am getting 403
{
"serviceErrorCode": 100,
"message": "Field Value validation failed in REQUEST_BODY: Data Processing Exception while processing fields [/actor]",
"status": 403
}
the same thing happened when i tried to use the ugcPost Api to post an new comment
any ideas what might cause this ?
i was able to solve this finally, i remove all header property except the content type and content length then it worked

500 Internal Server Error when trying to delete certain courses

Example:
Courses.Get successfully gets the course like this:
Request
GET https://classroom.googleapis.com/v1/courses/1053592526?key={YOUR_API_KEY}
Response
200
- HIDE HEADERS -
cache-control: private
content-encoding: gzip
content-length: 232
content-type: application/json; charset=UTF-8
date: Wed, 27 Apr 2016 11:45:29 GMT
server: ESF
vary: Origin, X-Origin, Referer
{
"id": "1053592526",
"name": "Test course 18",
"ownerId": "118150671162893595524",
"creationTime": "2016-03-11T12:49:19.459Z",
"updateTime": "2016-03-11T12:49:18.734Z",
"enrollmentCode": "aaiauvf",
"courseState": "ACTIVE",
"alternateLink": "http://classroom.google.com/c/MTA1MzU5MjUyNlpa"
}
Deleting it however gives the following:
Request
DELETE https://classroom.googleapis.com/v1/courses/1053592526?key={YOUR_API_KEY}
Response
500
- HIDE HEADERS -
cache-control: private
content-encoding: gzip
content-length: 104
content-type: application/json; charset=UTF-8
date: Wed, 27 Apr 2016 11:47:21 GMT
server: ESF
vary: Origin, X-Origin, Referer
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
Result is the same through code or through the web interface at https://developers.google.com/classroom/reference/rest/v1/courses/delete?authuser=0#try-it
This seems to happen if the user behind the OwnerId no longer exists. It only happens to some old courses that have been lying around for some time, and the owner have been deleted long ago. I also tried adding a teacher to the course, but the result is still the same. I did also try to create a new course with an ownerId referencing an existing user, deleting the user and finally deleting the course. Works.
There are 20 something courses on the domain that this happens for. I was able to delete another 50 something courses where the owning users still existed.
So, is this a bug that we can have fixed? And if not, how do I get rid of the courses?
It looks like this is a bug. Can you please file an issue for it?

How to validate response with dredd?

I'm trying to check my api implementation with my documentation written in blueprint. I've expected that dredd will fail when json returned from server will be different than specified in documentation. To check this I've copied dredd-example. First I've run dredd with original apib file to make sure that all is green. Then I've modified response in documentation and expected dredd to show me some red... But it doesn't.... it looks like tool is only checking response headers but not the response body. Here is output from console:
pass: GET /machines duration: 18ms
request:
host: localhost
port: 3000
path: /machines
method: GET
headers:
User-Agent: Dredd/0.2.1 (Darwin 13.0.0; x64)
expected:
headers:
Content-Type: application/json
body:
[
{
"_id": "52341870ed55224b15ff07ef",
"type": "bulldozer",
"name": "willyxxxxxx" #HERE IS WHERE I CHANGED RESPONSE IN DOCUMENTATION
}
]
status: 200
actual:
headers:
x-powered-by: Express
content-type: application/json
content-length: 95
date: Thu, 20 Mar 2014 08:22:40 GMT
connection: keep-alive
body:
[
{
"_id": "532aa5507dcdfff362931799",
"type": "bulldozer",
"name": "willy"
}
]
status: 200
Can I check response body using dredd? And how can I do this?
In JSON bodies Dredd is checking only for keys not for values. When you change key in the expected JSON body document, it will definitely fails.

Blogger API suddenly giving Insufficient Permission error

I've been publishing posts to Blogger blogs for a while now with the v3 API. Suddenly, this morning I'm getting 403 "Insufficient Permission" errors when I try to insert posts. To make sure it wasn't something in my code, I tried inserting a post with the API Explorer. I authorized the request with the provided scope (https://www.googleapis.com/auth/blogger) and tried to insert the following post resource:
{
"kind": "blogger#post",
"blog": {
"id": "xxxxxxxx"
},
"title": "Test Post",
"content": "This is a test post"
}
And when I tried to execute the request, I got the following error:
403 Forbidden
- Hide headers -
cache-control: private, max-age=0
content-encoding: gzip
content-length: 136
content-type: application/json; charset=UTF-8
date: Wed, 11 Sep 2013 18:18:21 GMT
expires: Wed, 11 Sep 2013 18:18:21 GMT
server: GSE
www-authenticate: Bearer realm="https://www.google.com/accounts/AuthSubRequest", error=insufficient_scope, scope="https://www.googleapis.com/auth/blogger"
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
Anyone know why this is suddenly not working today? Thanks!
Looks like this was a Blogger issue. The problem has been resolved on their side and my app works fine again.
I have this problem as well, to be honest I am glad that I am not the only one having this problem! This error also occurs if you try to do things via the Oauth playground, for me it was getting a list of the users blogs.

Resources