Send email from shell, using smtpauth, how to?

gsloop

Dabbler
Joined
Jan 23, 2019
Messages
10
TrueNAS Core 13.0 U4

Looks like the "old" method was msmtp - but it's not packaged any more.
mailx doesn't seem like you can do smtpauth with...

I've done quite a lot of searching and don't find any fairly simply packaged way to send email at the CLI/Shell.
Can someone point me at something?

(I've got some rsync scripts that sync directories/files and need to send mail to alert on success/failure of those processes. And I'll have other scripted things I want email from, so I really DO need a way to send mail from the CLI/shell.

TIA
 

Dice

Wizard
Joined
Dec 11, 2015
Messages
1,410
This script solves that, maybe there's some points?
a lot to look through tho lol

I found somewhere a glimmer where you can add email notification cloud sync tasks by using post-hooks and environment variables. Maybe you can cook something from this:

Code:
echo -e "Name: $CLOUD_SYNC_DESCRIPTION \r\n Direction: $CLOUD_SYNC_DIRECTION \r\n Local path: $CLOUD_SYNC_PATH \r\n Remote Path: $CLOUD_SYNC_FOLDER" | mail -v -s "[$HOSTNAME] $CLOUD_SYNC_DESCRIPTION Backup successfully completed" admin@yourmail.com
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
You can set up a mail server like postfix for local relaying in a jail. That's what I would do.
 

gsloop

Dabbler
Joined
Jan 23, 2019
Messages
10
Sure I *could* setup postfix, but that's like drilling your own well to get a drink of water.

After some extensive reading, testing etc - it appears this works.
Edit this and drop in a bash script.
[Tweak as needed.]
(Note that this hasn't seen any significant testing - and I'm not sure exactly what works in what conditions. I thought it was better to post _something_ even if it kind of sucks, rather than wait till it's perfect [which will never happen] and so no one gets anything.)

---
#!/bin/bash

# Replace these values with your own
to="Display Name <emailaddy@somdom.com"
from="Display Name <emailaddy@somdom.com"
subject="Test Email"
body="This is a test email."
attachment="/somepath/somefile.txt"
smtp_server="mail.your-mail-server.com"
smtp_port="587"
smtp_user="some-auth-user@somdom.com"
smtp_pass="sompword"

# Use sendmail to send the email
(
echo "From: Tester <$from>"
echo "To: $to"
echo "Subject: $subject"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"BOUNDARY\""
echo ""
echo "--BOUNDARY"
echo "Content-Type: text/plain"
echo ""
echo "$body"
echo "--BOUNDARY"
echo "Content-Disposition: attachment; filename=\"$attachment\""
echo "Content-Transfer-Encoding: base64"
echo ""
base64 $attachment
echo "--BOUNDARY--"
echo ""
) | sendmail -t -oi \
--smtp-server=$smtp_server \
--smtp-port=$smtp_port \
--smtp-auth=login \
--smtp-auth-user=$smtp_user \
--smtp-auth-password=$smtp_pass \
--from=$from \
--to=$to \
--mime-version=1.0 \
--content-type="multipart/mixed; boundary=\"BOUNDARY\""

---
I can't believe how hard it is to do simple tasks in FreeNAS/TrueNAS - it just reminds me of why I hate the FN/TN environment.

Next I need rdiff-backup which has also been deprecated. Sheesh.
 

gsloop

Dabbler
Joined
Jan 23, 2019
Messages
10
BTW, Thanks @Dice for the pointer to that script.
I'd seen the thread before but hadn't taken the time to actually grok the script itself. That was the last thing I needed to get the example above working. Stupid process, but at least it works.
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
I can't believe how hard it is to do simple tasks in FreeNAS/TrueNAS - it just reminds me of why I hate the FN/TN environment.

Well, that's hardly fair. This is something that generally isn't available in the FreeBSD, Linux, or for that matter most UNIX-like systems without a customized program to do it. UNIX-like systems generally expect you to use the onboard mail transport agent. Since the NAS appliance doesn't have an administrator to set that up, AND because there are compelling reasons such as SMTP auth that require an alternative solution (such as building in an MSA into the NASware), this leaves you needing to code an MSA if you want to be able to submit mail. It's not unusual for this to be the solution for this type of application, because the solution needs are very different from the conventional user-sending-via-Sendmail-MSA needs of a UNIX system with shell users.
 

gsloop

Dabbler
Joined
Jan 23, 2019
Messages
10
"Well, that's hardly fair"
Oh, yes it is.
msmtp is simple to install on Ubuntu;
So are probably 3-4 other MTA "helper" apps. (esmtp, sendEmail, etc.)

(Pretty much any task I go to do on TN/FN is way way harder than on Debian or Ubuntu or any other Linux distro. And yes, I understand that TN/FN is not your "regular distro," but the difficulties of doing pretty much bog-standard stuff is still way harder. Perhaps I should just move to Scale - perhaps I can add my own repositories and just add what I need there. But the case still stands. Doing anything outside of the very small cookie-cutter mold is just, inevitably, a huge PITA. I'm not going to detail all the weird cases, I've run into over the last 10 years or so, but they are numerous.)

For example, I need rdiff-backup in FN/TN. NAK.
Deprecated.
Really? rdiff-backup? Seems like nearly a core utility to me.

(This and the smtp problem above are just really simple parts of a script that will pull dirs/files and handle backup in a script from other systems and send an email as needed. Since the FN/TN box is the storage hub, having it do that work just seems logical. Having to build a jail or hop through a bunch of other hoops when I just need to rsync/rdiff-backup or perhaps a CIFS copy (smbclient) seems insane.)
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
msmtp is simple to install on Ubuntu;
So are probably 3-4 other MTA "helper" apps. (esmtp, sendEmail, etc.)

This is an appliance. You're not allowed to install random software; if you want to install stuff, put it in a jail. Which isn't really that hard to do, even allows for a complete MSA setup.

(Pretty much any task I go to do on TN/FN is way way harder than on Debian or Ubuntu or any other Linux distro. And yes, I understand that TN/FN is not your "regular distro," but the difficulties of doing pretty much bog-standard stuff is still way harder. Perhaps I should just move to Scale - perhaps I can add my own repositories and just add what I need there. But the case still stands. Doing anything outside of the very small cookie-cutter mold is just, inevitably, a huge PITA. I'm not going to detail all the weird cases, I've run into over the last 10 years or so, but they are numerous.)

Perhaps you'd be better off with just installing a FreeBSD or Linux OS and doing as you please. TrueNAS isn't oriented towards this sort of usage, and you both risk breaking what IS on the platform along with also risking whatever you've installed breaking when the system is updated.

For example, I need rdiff-backup in FN/TN. NAK.
Deprecated.
Really? rdiff-backup? Seems like nearly a core utility to me.

Never heard of it. And what do you mean by "deprecated"? As far as I can tell, it hasn't been in any recent release.

(This and the smtp problem above are just really simple parts of a script that will pull dirs/files and handle backup in a script from other systems and send an email as needed. Since the FN/TN box is the storage hub, having it do that work just seems logical. Having to build a jail or hop through a bunch of other hoops when I just need to rsync/rdiff-backup or perhaps a CIFS copy (smbclient) seems insane.)

Can't make everyone happy all the time. You could just put some custom scripting in your home directory and call it a day.
 
Top