App Inventor provides an easy mechanism to send mails using the client installed in the device. It’s based on the startActivity component and is extremely simple to implement as explained in the App Inventor reference documentation. Unluckily using the device mail client is not adequate when the mail needs to be sent without the user noticing it, or at least with minimal interaction (maybe just a notification). For example, I recently created a Login Template where it was unsuitable to show the mail prior to sending it because it contains a secret password and hence only the email receiver should be able to read it.
Therefore we’ll show an alternative way to send mails without user interaction.
What are we going to build
Actually the solution is not original but rather provided in a project by ShivalWolf (see next chapter for more details). However this post adds some practical details that were necessary to integrate the solution in my app, and which I think will be common to most apps:
- How to set up the mail sending service together with the TinyWebDB service. Since we’ll usually have an application service to provide de DB capabilities, it would be a pity to use a different one for sending mails (remember Google AppEngine’s limitation to 10 free services).
- How to deal with security issues for the service, i.e. the ability to restrict its utilization to privileged users. This is probably more relevant for the TinyWebDB than for the mail sending itself.
- Upgrade the service to Python2.7. For an explanation of the steps involved you may be interesed in this post: Python 2.7 migration demystified.
By the way when explaining the Web DB service we’ll also mention a useful trick to ease DB editing which you may not be aware of since the TinyWebDB describes an alternative way of doing it.
Mail sender service (App Engine)
Assuming we really intend to provide a shared service for both the TinyWebDB and mail sending, I recommend following these steps to install the solution:
- Create and deploy the TinyWebDB service as explained in the tutorial Creating a Custom TinyWebDB Service
- Download the files app.yaml and main.py and replace the files with the same name located in the folder where you downloaded the TinyWebDB.
- Edit the main.py and replace the contents of the variables EmailFrom and AuthKey as explained below
- Deploy again the application as in the first step (i.e. with the AppEngine console)
Client side (App Inventor)
Download these sources and upload them into App Inventor. You’ll realise there’s some functionality that is not explained in this tutorial because this is actually a more general application (which is explained in the Login template post). The part that concerns us is just the mail sending window (see below). Alternatively, since both user interface and code blocks are very simple, you may consider developing the client side from scratch.
How it works
The service deployed in this tutorial serves two purposes: unify both services (TinyWebDB and SendMail) and introduce security. We’ll just explain the small adaptations required for these two purposes since the services are fully explained in the above mentioned posts.
The combination of both services consists of merging the contents of the respective main.py files. This can be done for example starting from the TinyWebDB file and including into it the classes from SendMail. Additionally we’ll need to indicate the function associated to each GET request, i.e. modify the following line:
Also in the main.py file we need to inform the configuration variables as indicated by ShivalWolf:
Regarding security, configurations must be made in the app.yaml file, concretely informing the value ‘login: admin‘ in the handlers section to indicate the service can only be called by the administrator. However this alone would not work because this restriction would also prevent App Inventor to access the services, so we must specify that our services are excluded from this restriction:
That’s all regarding the two purposes of this tutorial. However, since we’re modifying the app.yaml file, I’ll profit to mention there is a useful parameter that allows using the Datastore administration for App Engine.
Among other things, this will allow us to consult and modify our database records from the App Engine administrative console using a more user-friendly interface than the one provided by the TinyWebDb service.
As stated above, we’re interested in just a small part of the app:
- HiddenMail_SEND: A textbox containing the address where the mail must be sent
- ButtonOK_SEND: The button that provokes the mail to be sent
- WebMailSend: The web component that performs the actual job. No need to inform any attribute since it’s done by code
Here’s a screenshot of the core function, both for AI classic and AI2:
The first thing to do is indicate the value for the two configurations variables:
- APPENGINE_ADDRESS: You must replace this variable by the URL where you’ve set up the service
- AUTH_KEY: Replace it by the authentication key you’ve informed in the main.py file
After this, sending the mail consists simply in posting a string to our service, containing five pieces separated by ‘|||‘:
- Authentication key
- Email address
- Email subject
- Email body
- Indicator of attachment. It must be informed as no because I’ve removed this part from the service.