:)
Ok, that sounds too good to be true. But it's possible, watch this:
- Go to script.new (in a new tab, please!)
- Make sure you're logged in with a gmail account
- Add this line to your function myFunction:
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),"An email","Wim taught me how to send myself e-mails with one line of code!");
- Menu Run -> Run Function -> My Function
- Save script & Allow permissions (from your own account)
- VOILA! Look in your inbox
Steps to allow permissions
Welcome to Google Apps Script
Welcome! You just learned how to code in Google Apps Script.
This opens the door to many, many possibilities. The cool thing about Apps Script is that you can access Google api's very easily. For example to read a value from a sheet, document or your gmail inbox. Or scrape from an external site for example. You could write a script that sends an email with the weather every morning. Here's how you do this:
Send the weather to yourself
- Open up a new script: script.new
- Go to https://home.openweathermap.org/api_keys
- Get your api keys here and copy them to this script in line 2 and change the location in line 6
const cfg = {
apiKey: '' // <-- FILL IN YOUR API KEY FROM https://home.openweathermap.org/api_keys
}
function sendWeatherByEmail() {
const city = 'amsterdam'; // <-- CHANGE YOUR LOCATION HERE
const url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=' + cfg.apiKey;
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText());
const temperatureTodayMin = (Number(data.main.temp_min) - 273.15).toFixed(1);
const temperatureTodayMax = (Number(data.main.temp_max) - 273.15).toFixed(1);
const body = 'Temperatuur vandaag is: ' + temperatureTodayMin + ' - ' + temperatureTodayMax;
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), 'weather', body);
}
- Copy this code to your script that you opened up at step 1
- Menu Run -> Run Function -> sendWeatherByEmail
- Allow permissions (from your own account)
- And again VOILA! Look in your inbox
Every morning automatically
You can also run the function automatically every morning, if you want! For this:
- Menu Edit -> Current project's triggers
- Right bottom corner -> Add Trigger
- Choose when you want to execute 'sendWeatherByEmail' and save
You can check out the github code here:
https://github.com/wimdenherder/DailyWeatherMailer/blob/master/index.gs
You can see how i coded the weather script here:
https://www.youtube.com/watch?v=rsNFQJsZ8vM
Top comments (0)