SendEmail Official Guidelines

About the project

Starting from TrueNAS 24.10.10, the sendemail builtin function is no longer available (removed for security reason).
The mail.send method is still available, but is quite trivial to use due to some payload limitations/encoding/ecc. This standalone script provides the ability to send emails and attachments using the TrueNAS native mail.config, in the simplest possible way.
Originally designed to be a wrapper for Joe's Multi Report, it also can be used to simplify sending email overall in many other scenarios.

Actually, there are basically 2 different usage methods:
  • Passing --subject, --to_address, --mail_body_html (either a file path and plain text), plus other optionally args explained belove.
  • Passing only the full email base64 encoded (either a file path and plain text) as --mail_bulk, trying to emulating the old sendemail function, and all the info will be retrieved there.

  • Truenas Scale stable (nightly, alpha, beta, ... are not granted to be fully functional)
  • Truenas Core 13.* ( with some limitations, like no Outlook provider, some notification are in plain text instead of full HTML)
  • Working email settings (under System --> General Settings --> Email)
  • Context user with READONLY_ADMIN or SHARING_ADMIN roles
  • The script should reside in a secured dataset/folder

To retrieve TN mail configuration data, at least READONLY_ADMIN or SHARING_ADMIN roles are needed for the user running the script.
So is highly advised to only use the script in a secured folder, not accessible to un-priviliged users, to avoid unexpected behaviour.
The script will advise you in those scenarios, so pay attention if some warning are raised on first usage and fix your dataset permission accordingly.
There are other checks performed to improve security (attachment black list, avoiding symlink, CRLF injection, ...), any suggestiona are welcome and i will do my best to keep things as safe and flexible for everyone.

Advanced usage methods

Is possible to override the default TN sender name - sender email to fit more scenarios in those ways:
  • using directly --override_fromemail or --override_fromname args calling the script
  • for multi report users, editing own standard mr_config file, value involved are FromName and From, as a fallback
📌 The priority is: override data > fallback data > default

If sendemail and mr_config file are in the same folder, fallback values will be still retrieved although you will call the script outsite the multi_report.
Also to mention, only override_fromname and FromName can be used, without an email override, and they will be applied to the default TN email.

Customize the sender name has no particoular restrictions, is pretty safe, instead you have to pay attention on customizing the sender (reject ratio depending on your provider):
  • GMAIL GMAIL: this provider will override your sender with the account email if it is not a validated alias, and the deliver shouldn't fail; you can safely send email with a sender plus address (example, your account is myemail@gmail.com and you use myemail+multireport@gmail.com as sender)
  • OUTLOOK OUTLOOK: every sender that is not a valid alias of the account will be rejected
  • SMTP SMTP: every provider has own policy, but the most reject sender that is not a validated account/alias; also to mention, despite the email is send, using a sender with a different domain can be checked as spoofing with the direct conseguence that the email is delivered but go into the SPAM folder

In case of need, passing --debug_enabled as arg to the script will activate the debug mode: a log file will be generated and placed into sendemail_log folder.
Folder itself and files can be safely deleted manually anytime the script is not running.
Calling the script from crontab, with the debug enabled, can lead to problem if the root user is the one used and, before invoke the script, the correct working directory is not selected with a cd command.
Log files not obviously expose credentials or access token, but don't forget to cleanup after a debug session anyway to not expose more-or-less sensitive data in your usage context arising from exceptions.

If you wanna just test the basic function fast and quickly, use the --test_mode and the script will compose and send a test email to the actual user email address.
In test mode, every arg will be overwritten, also the debug will be enabled automatically. If the script is in the same Multi Report folder, the fallback from the multi_report_config.txt FromName and From will be used, so you can ensure that the fallbacks are working properly. test preview

Managing updates

From version 1.50, the sendemail has been equipped with several tools, that will help both end users and other scripts (that rely on sendemail) keep everything updated.
These tools aim to ensure to keep things easy in different scenarios, providing notifications, outputs (that can be parsed), and an internal auto-update mechanism.

With --notify_update the script uses the TrueNAS builtin mail.send command to notify that a new version has been released.
This arg is intended to be used alone in a TrueNAS cronjob, with a "slow" schedule (e.g. weekly, since it has no queue management) to help keep the script manually updated.

With --check_update the script returns a tiny json object that other scripts can use to decide whether to perform or notify about an available update.
This arg is intended to be used alone.
{"version": "1.**", "latest_version": "1.**", "need_update": false} 

With --self_update the script will perform an auto update, only if a new stable version is available on Github.
This arg can be used with --notify_self_update, and the script will generate a notification using the TrueNAS builtin mail.send command, whether it a success or fail.
Also to mention, everytime the --self_update is used, the debug mode will be enabled.

Usage example

#!/bin/bash
python3 sendemail.py --test_mode 

#!/bin/bash 
subject='test' 
recipient='myemail@gmail.com'

# Email Body: can be an HTML file path or plain text/html
html_file="<h1>Send Email Test</h1><p>Hello World!</p>"

# Attachments (optional): add file paths
attachment=() 
attachment+=("path/to/first/attachment")  # Replace with the actual path
#attachment+=("path/to/second/attachment") # Add additional files as needed

python3 sendemail.py \
    --subject "$subject" \
    --to_address "$recipient" \
    --mail_body_html "$html_file" \
    --attachment_files "${attachment[@]}" 

#!/bin/bash
mail_bulk= '/path/to/base64encode/email'
python3 sendemail.py --mail_bulk "$mail_bulk" 

#!/bin/bash
subject='test' 
recipient='myemail@gmail.com'
html_file="<h1>Send Email Test</h1><p>Hello World!</p>"

python3 sendemail.py \
    --subject "$subject" \
    --to_address "$recipient" \
    --mail_body_html "$html_file" \
    --override_fromname "Pippo" \
    --override_fromemail "myemail+pippo@gmail.com"

#!/bin/bash
subject='test' 
recipient='myemail@gmail.com'
html_file="<h1>Send Email Test</h1><p>Hello World!</p>"

python3 sendemail.py \
    --subject "$subject" \
    --to_address "$recipient" \
    --mail_body_html "$html_file" \
    --debug_enabled

#!/bin/bash
python3 sendemail.py --notify_update

#!/bin/bash
python3 sendemail.py --self_update --notify_self_update