Email Templates in Scripts

When you create scripts, you can set up automatic emails to be sent about their successful execution. You can quote parameters and variables used in scripts in the email.
This approach is convenient to use when providing SaaS services: the administrator creates a virtual machine (VM) on which the required script is run, and after the execution the user receives an email with the necessary parameters. Thus, the client receives a ready-made service that can be used immediately: he does not need to install or run anything himself.

Setting up email templates

An email template is HTML code that is sent to the user's email address after the script has been executed.
Email templates are configured when creating the script. To create a script:
    .1Go to ScriptsCreate script.
    .2Enter script settings and body. Read more in  Creating a script for the VM .
    .3In the Notification to email section, click Add and configure the email templates. By default, you are offered to set up an English email template — Email for EN.
    .4Specify the Email subject for each template.
    .5Configure your template in the Content field. By default, this field specifies the HTML blocks that you can use to create your own template. These blocks are a part of the global template that defines the design of the email — the VMmanager logo, fonts, text color and buttons. In the current version, the global template cannot be edited.
    .6The Preview button allows you to view the layout of the email that the user will receive.
    .7Click Create to save the script and email template.
When running the script, if email templates have been configured for it, you can specify the address to which the email will be sent:
    .1Enable the Send the email associated with the script option.
    .2Select email template Language.
    .3Specify the Receiver email. If this field is left blank, the email will be sent to the owner of the virtual machine on which the script is run. You can also specify multiple recipients separated by commas.
Note
Specifying the receiver email is available only to the VMmanager administrator. Regular or advanced users can only send an email to themselves.
If you run several scripts on the same VM at the same time, you can send an email only regarding one script.

Script parameters and variables in templates

All parameters and variables that are used in the script can also be used in email templates. Read more about variables in   Variables for scripts . 
This allows you to insert the parameters appearing in the email sent to the user. For example, a login and password are generated when the script runs. Then you can specify these parameters in the email template.
You can send some parameters from the script to the template. The following constructions are specified in the script for this purpose:
  • vm_export_variable <var_name> <var_value> is used to send a variable to the email template. For example, the script generates a password and its value can be sent in an email;
Comments
<var_name> — the name of the variable, as it will appear in the email. For example, password.
<var_value> — the value of the variable. It is preferable to use quotation marks so that bash syntax identifies the specified variable correctly. The value of the variable can be set or generated internally in the script.
Note
All variables (from the script, global/local, parameters), which need to be added to the email template, should be specified in the body of the template in the ($var_name) format.
vm_export_file <file_name> <file_path> is used to send the file generated inside the script to the email template. The file is sent as an attachment to the email and does not need to be specified inside the email template;
 Comments
<file_name> — the name of the file, as it will appear in the email.
<file_path> — the path to the file.
vm_export_dir <dir_name> <dir_path> is used to send the directory in the .tar archive format to the email template. The archive is sent as an attachment to the email and does not need to be specified inside the email template.
 Comments
<dir_name> — the name of the .tar archive.
<dir_path> — the path to the directory.
Note
The vm_export_dir structure does not work on Windows VMs or on VMs that do not have tar installed.
By default, the maximum size of a file/archive attached to an email is 2 MB. To change this value, execute the following request:
Changing the size of an attachment
curl -k -H 'x-xsrf-token: user token' https://IP-Address/vm/v3/setting/max_export_file_size_mib -d '{"value":"Size of file in MB"}'

Example of configuring an email template

As an example, here is a script that creates and compiles a C++ file:
Script body
dnf install -y gcc git gcc-c++
mkdir /root/example
cd /root/example
echo '#include <iostream>
int main() {
std::cout << "Hello world!";
}' > ./example.cpp
g++ example.cpp -o /root/example/example
/root/example/example > /tmp/test.txt
output="$(cat /tmp/test.txt)"
vm_export_variable output "$output"
vm_export_file output.txt /tmp/test.txt
rm -rf /tmp/test.txt /root/example
The email displays the following information: IP address of the virtual machine on which the file is compiled, build version and output:
Email template content
<h1 style="margin-bottom: 10px">Your file compiled</h1>
<div>
<p><b>Сompiled on:</b> ($IP)</p>
<p><b>Build version:</b> ($build_ver)</p>
<p><b>Stdout:</b> ($output)</p>
</div>
The global variable $IP, the script parameter build_ver and the output variable specified in the script are used for this purpose.