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:
- Go to Scripts → Create script.
- Enter script settings and body. Read more in Creating a script for the VM.
- In 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. To create an email template in Russian, click Add the email for RU. - Specify the Email subject for each template.
- Configure 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. - The Preview button allows you to view the layout of the email that the user will receive.
-
Click Create to save the script and email template.
Creating an 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:
- Enable the Send the email associated with the script option.
- Select email template Language.
-
Specify the Reciver 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.
Sending the email
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;
CommentsNoteAll 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 -
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.
CommentsNoteThe 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:
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:
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
Script settings
The email displays the following information: IP address of the virtual machine on which the file is compiled, build version and output:
<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.
Setting up the email template in English
Example of a received email