Reputation: 13304
After much study and repairing the problems, I arrived at the following location that is in my github. But I do not know if I'm mounting json properly. For the following error is appearing:
{
error:
{
errors: [
{
domain: global,
reason: parseError,
message: This API does not support parsing form-encoded input.
}
],
code: 400,
message: This API does not support parsing form-encoded input.
}
}
I'm setting up the post as follows, for more details the project is in my github
// scope for send email
GoogleSignIn googleSignIn = new GoogleSignIn(
scopes: <String>[
'https://www.googleapis.com/auth/gmail.send'
],
);
await googleSignIn.signIn().then((data) {
testingEmail(data.email, data.authHeaders);
});
// userId is the email
Future<Null> testingEmail(userId, header) async {
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: await header,
body: {
'from': userId,
'to': userId,
'subject': 'testing send email',
'text': 'worked!!!'
}
);
}
What am I doing wrong, to not be able to send an email through the Google API? Could you help me with this problem?
Upvotes: 2
Views: 7161
Reputation: 13304
A few changes were made, the main one being that the http post body needed to be a json with the raw
key and its contents in base64
And this text that has been converted to base64 must be a MIMEText, so the specific format, as below.
To change the html to text simply change the Content-Type: text/html
from the string toContent-Type: text/plain
The following is a clipping of the code. The complete code is in github
await googleSignIn.signIn().then((data) {
data.authHeaders.then((result) {
var header = {'Authorization': result['Authorization'], 'X-Goog-AuthUser': result['X-Goog-AuthUser']};
testingEmail(data.email, header);
});
});
Future<Null> testingEmail(String userId, Map header) async {
header['Accept'] = 'application/json';
header['Content-type'] = 'application/json';
var from = userId;
var to = userId;
var subject = 'test send email';
//var message = 'worked!!!';
var message = "Hi<br/>Html Email";
var content = '''
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: ${to}
from: ${from}
subject: ${subject}
${message}''';
var bytes = utf8.encode(content);
var base64 = base64Encode(bytes);
var body = json.encode({'raw': base64});
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: header,
body: body
);
if (response.statusCode != 200) {
setState(() {
print('error: ' + response.statusCode.toString());
});
return;
}
final Map<String, dynamic> data = json.decode(response.body);
print('ok: ' + response.statusCode.toString());
}
Upvotes: 6