DEV Community

Cover image for Send e-mail to yourself with one line of code!

Posted on

Send e-mail to yourself with one line of code!

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

  1. Go to (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!");
Enter fullscreen mode Exit fullscreen mode
  1. Menu Run -> Run Function -> My Function
  2. Save script & Allow permissions (from your own account)
  3. VOILA! Look in your inbox

Image description

Image description

Steps to allow permissions

Image description

Image description

Image description

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

  1. Open up a new script:
  2. Go to
  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

function sendWeatherByEmail() {
  const city = 'amsterdam'; // <-- CHANGE YOUR LOCATION HERE
  const url = '' + 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);
Enter fullscreen mode Exit fullscreen mode
  1. Copy this code to your script that you opened up at step 1
  2. Menu Run -> Run Function -> sendWeatherByEmail
  3. Allow permissions (from your own account)
  4. 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:

You can see how i coded the weather script here:

Top comments (0)