App Inventor and Google Apps Script


How to send a 'Contact Us' mail to a predefined email address automatically from within App Inventor

For an overview of different eMail solutions, see here How to send an email including attachment from within App Inventor.

The idea of the example presented here is to find a simple solution without activity starter, without need to use a php server or App Engine part. I played around with Google Apps Script and this is the result.

For the example I'm using my Google Spreadsheet solution, which uploads a record into a Google spreadsheet. I used 3 columns: name, email address and message text. Then I just added a slightly modified script I found in the Script Gallery. Thank you toptrace7 for the script. Everybody who submits a row into that spreadsheet will trigger automatically a message to be sent to the recipient's email address.

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Preparation

  1. In your Google Spreadsheet open the Script Editor via Tools - Script Editor and select "Create Script for Blank Project" Rename the untitled project to your needs and copy the source code below.
  2. Now add a trigger via Ressources - Current Project Triggers. Click the "No Triggers set up. Click here to add one now." link. Select "on form submit" and click save.

    Then authorize.
  3. Ready! Everybody who submits a row into that spreadsheet will trigger automatically a message to be sent to the recipient's email address.

Script Source Code

function contactUsMailer(e) {
  //  author: toptrace7, slightly adjusted by puravidaapps.com
  //
  //  This script e-mails the contents of a form to a given recipient
  //  The form must have three fields in the order of: name; e-mail address; and message
  //  You must change the recipient variable below to your e-mail address
  try {
    var recipient = "<ADD YOUR EMAIL HERE>"
    var timestamp = e.values[0];
    var name = e.values[1];
    var email = e.values[2];
    var message = e.values[3];
    var body = name+' <'+email+'> sent the following message: '+message;
    var bodyHTML1 = '<p>'+name+' <a href="mailto:'+email+'">'+email+'</a> sent the following message: </p>';
    var bodyHTML2 = '<blockquote>'+message+'</blockquote>';
    var bodyHTML3 = '<p>Sent by the Pura Vida Apps <a href="http://www.puravidaapps.com/sendmailGS.php">Send Mail example</a>.</p>';
    var advancedArgs = {htmlBody:bodyHTML1+bodyHTML2+bodyHTML3 , replyTo:email};
    MailApp.sendEmail(recipient, "Contact Us Form", body, advancedArgs);
  } catch(e){
    MailApp.sendEmail(recipient, "Error - Contact Us Form", e.message);
  }
}

Screenshot of the eMail



Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact