Discussions » Development

write email with Tampermonkey script using gmail API

§
Posted: 2023/03/10
Edited: 2023/03/10

Hi, I am trying to write a script that basically repeatedly checks a certain side for new content and if some is found,
it displays a push notification and also sents an email from my gmail email adress to another email adress using the gmail api.

I had the code running without the email stuff but now that I wanna expand it with email notifications, I am stuck.

I have zero knowledge regarding gmail api or such things, code so far comes from chatgpts suggestions.
obviously already had a gmail account, also have some google cloud accoutn , project there with oauth2.0 thing and an api key.
so got everything exept the actual code that (wrorkingly) does the email sending.
anyways, my code so far.
first "gapi is not defined" was an error.
after some @require magic, I get other errors.
the email related parts:



// ==UserScript==
// @name Preisfehler Alert with Email+Push
// @namespace mydealz-preisfehler-alert-push-email
// @version 0.7
// @description Email and push notification if a new article appears on the Preisfehler page of mydealz
// @author ...
// @match https://www.mydealz.de/gruppe/preisfehler
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_addValueChangeListener
// @grant GM_notification
// @grant GM_registerMenuCommand
// @connect accounts.google.com
// @connect content.googleapis.com
// @grant GM_addStyle
// @connect smtp.gmail.com
// @require https://apis.google.com/js/api.js
// ==/UserScript==

// Replace the following values with your own client ID, API key, and email address
const CLIENT_ID = '...';
const API_KEY = '...';
const USER_EMAIL = '...';

await (async()=>{
// Load the Gmail API library
const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js';
document.head.appendChild(script);
document.body.appendChild(script);
})();

// Load the Gmail API client library
await gapi.load('client:auth2', initClient);

// Function to initialize the Gmail API client
async function initClient() {
// Initialize the client with your API key and OAuth 2.0 client ID
await gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
scope: 'https://www.googleapis.com/auth/gmail.send'
});
}

// Function to send an email
async function sendEmail(sender, receiver, subject, body) {
// Construct the message object
let message = '';
message += 'From: ' + sender + '\r\n';
message += 'To: ' + receiver + '\r\n';
message += 'Subject: ' + subject + '\r\n';
message += '\r\n' + body;

// Encode the message in base64url format
let encodedMessage = btoa(message).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

// Send the email using the Gmail API
let request = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': encodedMessage
}
});

// Execute the request
let response = await request.execute();
console.log(response);
}

await gapi.load('client:auth2', initClient);
await sendEmail(USER_EMAIL,'...','PREISFEHLER!!!','email body text');

//await sleep(20000);//20 sekunden
//location.reload();

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

Deleted user 821489
§
Posted: 2023/03/17

push notification

by default available in the web interface itself

to another email adress

why not use forward or filter (Forward to)?

Post reply

Sign in to post a reply.