Reputation: 21
I created once service in angular9 and call google api for sending email but it doesn't work. may someone help what should I do? I tried many other ways but no one of them worked.
sendEmail() {
const scopes = [
'https://www.googleapis.com/auth/gmail.send',
].join(' ');
const message =
"From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Subject: As basic as it gets\r\n\r\n" +
"This is the plain text body of the message. Note the blank line between the header information and the body of the message.";
const encodedMessage = btoa(message)
const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
gapi.load('client:auth2', () => {
gapi.client.load('gmail', 'v1', () => {
console.log('Loaded Gmail');
gapi.client.init({
apiKey: this.apiKey,
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
client_id: this.clientId,
immediate: true,
scope: scopes
}).then(res => {
console.log('pop')
return gapi.client.gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: reallyEncodedMessage
}
}).then(res => {
console.log("done!", res)
});
})
});
});
}
response from gapi:
kind: "discovery#restDescription"
id: "gmail:v1"
name: "gmail"
version: "v1"
rootUrl: "https://www.googleapis.com/"
servicePath: "gmail/v1/users/"
batchPath: "batch/gmail/v1"
...
Upvotes: 2
Views: 2550
Reputation: 1104
finally I made it and below is my code:
checkForGmailLogin() {
console.log('onLoadCallbackFunction');
const scope = ['https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send'].join(' ');
gapi.auth.authorize(
{
'client_id': this.clientId,
'scope': scope,
'immediate': true,
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
}, authResult => {
if (authResult && !authResult.error) {
gapi.client.load('gmail', 'v1', () => this.sendEmail());
} else {
console.log('Error in Load gmail');
}
});
}
sendEmail() {
const subject = 'Code';
const code = '123'
const mimeData = [
"From: [email protected]",
"To: [email protected]",
"To: [email protected]",
"Subject: =?utf-8?B?" + window.btoa(unescape(encodeURIComponent(subject))) + "?=",
"MIME-Version: 1.0",
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: 7bit",
"",
"Your code is " + code].join("\n").trim();
const raw = window.btoa(unescape(encodeURIComponent(mimeData))).replace(/\+/g, '-').replace(/\//g, '_');
gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': raw
}
}).execute(res => {
console.log('Email sent', res);
this.snackBar.success('Email has send Successfully')});
}
Upvotes: -1
Reputation: 2014
Following the JavaScript example on the Gmail api reference, you have to change your request:
From:
gapi.client.gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: reallyEncodedMessage
}
})
To:
gapi.client.gmail.users.messages.send({
'userId': userId,
'resource': {
'raw': base64EncodedEmail
}
})
If this doesn't solve it please comment on the answer or provide more information on the question.
Upvotes: 2