Titanium Cloud Service: sending emails without calling the email dialog

    Titanium Cloud Service

    Titanium Cloud Service - is a simple and convenient way to avoid creating a server for working with a mobile application, using ready-made solutions from Appcelerator.



    Cloud Service supports the use of Push Notifications, sending email messages, storing a large volume of photos and much more.

    It should be noted that there are several tariff plans for Cloud, but to start the free version is more than enough. This version provides features such as: 5 million Push Notifications, 5 million API calls, 20 gigabytes of free server space, 100 thousand mail messages per month, which, in my opinion, is very good.



    In addition, there are 2 versions of working with Cloud - these are Production and Development, which is undoubtedly very convenient. At the same time, for example, when compiling an application on a device connected to a computer and using it, the application will be regarded as a development version, otherwise, as production.

    Connecting to a project is easy.

    In turn, connecting a Cloud to a Titanium Studio project is elementary. In the tiapp.xml file, you must set the Enable value for Cloud Services. The necessary keys will be added to the project, which will also be displayed on the project page at https://cloud.appcelerator.com .

    Data management

    So, to work with the project, you need to configure it on the Cloud page. All that will be needed is the tabs: settings, email templates, and user creation in app management. All actions must be duplicated in two versions of the project (production and development).

    What you need to send email is working with the SMTP Settings fields. Here username is the email from which letters will be sent, and password is the password from this email. TLS must be set to true. In this example, I will use gmail smtp address, which looks like smtp.gmail.com, you can use 587 port.

    Let's move on to the email templates tab. This creates a message template that will be passed to the final recipient. I will describe a small example:

    name: order (необходимо для обращения в коде приложения)
    subject: Order from app (тема письма)
    body: (шаблон тела письма)
    <!DOCTYPE html>
    <htmllang="en"><head><body>
            Name: {{name}}<br />
            Surname: {{surname}}<br /><br /><br />
            {{order}}
        </body></html>

    As you can see, in the body of the letter there are 3 variables that will be filled with data during the application.

    Finally, let's move on to the last step - creating a user. The user is required to access the Cloud from the application and transfer specific information. We press the Create user button and fill in the fields with data that will be further used in the application code, namely, username and password will be required.

    Working with the Titanium.Cloud API

    Let's move on to working with the API. To get started, connect the necessary library in the code and make the user login that was created in Cloud a little higher.

    var Cloud = require('ti.cloud');
    Cloud.Users.login({
    	login : 'username',
    	password : 'password'
    	}, function(e) {
    		if (e.success) {
            ...
    		}
    	}
    });
    

    If the user login was successful, you should start uploading images to the server. At this stage, you should save the image id in a variable, for further checking this image for processing by the server, because it may take a different amount of time and a situation may occur when the program runs faster than the server.

    var currentItem = Ti.Filesystem.getFile(pathToFile);
    var blob = currentItem.read();
    Cloud.Photos.create({
    		photo : blob
    	}, function(e) {
    		if (e.success) {
    			//отображение прогресса загрузки изображения в процентах (в созданный заранее label)
    			Cloud.onsendstream = function(e) {
    				messageLabel.text = String.format(L('uploading_photo'), + (Math.floor(e.progress * 0.5 * 100) * 2) + '%');
    			};
    			var photo = e.photos[0];
    			id = photo.id;
    			...
    		} else {
    			alert(e.message);
    		}
    });
    

    Now, directly, we will check for image processing by the server and if everything is successful, then we will move on to the function of sending an email.

    Cloud.Photos.show({
    	photo_id : id
    	}, function(e) {
    		if (e.success) {
    			Cloud.ondatastream = function(e) {
    				messageLabel.text = L('server_processes_data');
    			}
    			var photo = e.photos[0];
    			if (photo.processed) {
    				sendEmail();
    			}
    });
    

    Consider the function of sending letters. Here, template is the name on the Cloud page in the email templates tab, recipients is the message recipient. Name, surname and order are the variables described in the email templates template, respectively.

    functionsendEmail() {
    	Cloud.Emails.send({
    			template : 'order',
    			recipients : 'someone@gmail.com', //to whom
    			name : name,
    			surname : surname,
    			order : order
    		}, function(e) {
    			if (e.success) {
    				...
    			} else {
    				alert(e.message);
    			}
    		});
    }
    

    Letters arrive quickly, without any delay.

    Thus, the Appcelerator Cloud is convenient for connecting to projects and further use, and its free version has all the features to familiarize yourself and get started with this product.

    Also popular now: