Send e-mail to yourself with one line of code!
Tags: javascript, googlecloud, webdev
Published: 2021-11-30 08:12:15

:)
Ok, that sounds too good to be true. But it's possible, watch this:

1. Go to [script.new](script.new) (in a new tab, please!)
2. Make sure you're logged in with a gmail account
3. 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!");
```
4. Menu Run -> Run Function -> My Function
5. Save script & Allow permissions (from your own account)
6. VOILA! Look in your inbox


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5v9dgtjnq9cmkoswo8a.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rvqd0nn5qmq795pxcgh.png)

## Steps to allow permissions

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19vtq5uw55f5392rsor7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miv9xjpajl7m66o4q87s.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m43wg81c7mwkzxl8j3zz.png)

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leoqvr585faxh2obm6oc.png">

## Welcome to Google Apps Script
Welcome! You just learned how to code in [Google Apps Script](https://developers.google.com/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
1. Open up a new script: [script.new](script.new)
2. Go to [https://home.openweathermap.org/api_keys](https://home.openweathermap.org/api_keys)
3. 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);
}
```

4. Copy this code to your script that you opened up at step 1
5. Menu Run -> Run Function -> sendWeatherByEmail
6. Allow permissions (from your own account)
7. And again VOILA! Look in your inbox

## Every morning automatically
You can also run the function automatically every morning, if you want! For this: 
1. Menu Edit -> Current project's triggers
2. Right bottom corner -> Add Trigger
3. 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