Ssmtp - send mail using gmail from a shell script

OK - so I’ve spent the whole morning (apart from the bit where I mowed the front lawn and whippersnipped bits of it) working on this :

Automatically send me an email each week - on Wednesday arvo / evening about which bin(s) to put out for collection the following day (Thursday) - e.g. Red for General Waste, Yellow for recyclables - FOGO (i.e. rotting organic matter :smiley: ) just goes out every week - regardless…

Got most of the algorithm stored in my head… The crontab bit’s easy - just do it every “WED” :

15 16 * * WED /home/x/bin/whichbin.bash 2>&1

Sorted that bit… created a space separated table / array :

...
23	6	1	RED
23	6	8	YEL
23	6	15	RED
23	6	22	YEL
23	6	29	RED
...

i.e.

YY m d $COL

I might put in a 5th column (sic :smiley: ) of the day before, i.e. “TODAY” of the day it runs… But I think there’s some fancy-schmancy English like regex you can do with date’s these days, e.g. find out what tomorrow’s date is going to be…
date --date='tomorrow' ‘+%d’

╭─x@titan ~/sbin  
╰─➤  date --date='tomorrow' '+%d'
13

RED = General waste
YEL = recycling (aluminium, paper, glass etc)

So I get to the bit where I have to setup mailing via one of my gmail accounts (I have several) - and - I’ve actually done this before many years ago (I’d email myself if my home internet connection changed external IP Address). But Google have changed EVERYTHING and you now have to configure your google account to allow “Apps”.

So - I figured that out - and tested, I can send emails from my main Linux desktop machine (Pop!_OS 22.04), to my gmail inbox! Woohoo! That worked! Using package ssmtp (“sudo apt install ssmtp”).

But this is a test case… Ideally I want to run this on my Pi4 running buster as I know it’s on all the time, and it’s wired directly via UTP into a gigabit port on my WiFi/Router…

╭─x@beere253 ~/bin  ‹main*› 
╰─➤  sudo apt install ssmtp
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package ssmtp is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'ssmtp' has no installation candidate

Really? Lotsa BIG SWEARY WORDS!

And the READMEs and forums are all saying don’t use ssmtp it’s deprecated or no longer maintained (I really couldn’t give a rat’s - as long as it works and it DOES work with all the 2FA shit that google force these days) - BUT - HUGE BUT - Canonical, and System76 let it stay! IT works! The forums etc cetera are all saying use “msmtp” instead…

BUT THE CONFIGURATION for that package is completely different.

You know what? I AM SERIOUSLY contemplating wiping Debian Buster (Raspbian) arm64 off it and going with Ubuntu server 22.04 for arm64… Shock horror! Replace Debian with Ubuntu? Why NOT? Works for me… I can even test it first - I have a spare Pi4… and a Pi3!

Some of my answers might be here :

But you know what? I REALLY kinda HATE it when the answer’s “compile it yourself…”

At least this answer builds a DEB I can install later - or even save on my NAS…


Jeepers! Buster’s EOL already! Seems like we just got it…
Extra reason to go for an LTS Ubuntu… But first I’ll try and get ssmtp working on buster… Damn! I’m still running Stretch on my TVHeadend Pi3!


I’m just going to test if Ubuntu 22.04 server arm64 supports ssmtp (or has the package) with a VM on my MacBook (I use UTM to manage VM’s on Mac - and I can do native arm64 virtualization).


Spinoza was a great thinker - but the owner / writer at espinoza.tv hasn’t “future proofed” his guide - so maybe not such a great thinker? - as - ssmtp_2.64.9 does NOT exist… in fact - that URL hosts arm64 binary deb installers - but NOT any of 2.64.9 (not binary DEB or source) - I can’t install 2.64.10 'cause unmet dependancies (libgnutls-openssl27 is too old), and there’s a 2.64.8-+b (beta I assume) that dpkg installs - but DOES NOT CREATE ANY F–KING BINARIES!

ssmtp binary is supposed to go in /sbin/… that’s where it goes on Pop!_OS 22.04 - one would assume Ubuntu 22.04 does the same thing… Just about to test on Ubuntu 22.04 arm64 (had to do-release-upgrade a clone of a ubuntu 20 arm64 vm in UTM)…

Scheiße!
Debian Buster DEB installer of ssmtp 2.64-8beta installs to /usr/sbin/ssmtp - which just happens to NOT be on my path!


Going to run two Pi4 off gigabit ethernet off my Router in “parallel” while I cutover to Ubuntu 22.04 Server arm64… Once all’s good, I’ll just switch 512 GB USB 3 SSDs around so my 4 GB headless Pi now boots off the other SSD I setup with an 8 GB Pi4… Too easy…


Within 10 minutes of my Pi4 with Jammy Jellyfish (22.04.2) being up and updated - I’ve now successfully tested ssmtp (sent an email to myself) - so soon it shall be GOODBYE to Raspbian / Debian / Buster… I’ll still have Stretch running my TVHeadend setup, and bullseye on my Pi Zero 2W… but that’s it…

  1. armbian 16 (ubuntu 16 - headless) 32 bit arm on Orange Pi+ 2E 2GB RAM (transmission-daemon server)
  2. raspbian stretch 32 bit armhf on Pi3
  3. raspbian bullseye 32 bit armhf on Pi Zero 2W (arm64 bit capable)
  4. red hat EL 8 on a Gigabyte Brix (2 celeron cores, 16 GB RAM 512 m2 SATA SSD) - x86_64
  5. Pop!_OS 22.04 - Ryzen 7 desktop - x86_64
  6. Pop!_OS 22.04 - Ryzen 5 Thinkpad - x86_64
  7. Pop!_OS 22.04 - Raspberry Pi4B (8 GB) arm64
  8. MacOS Monterey 12.61 - MBP M1 - 16 GB RAM 512 GB SSD (arm64)
  9. MacOS Monterey 12.61 - MBP M1 - 8 GB RAM 256 GB SSD (arm64)
  10. FreeBSD 12.2 (TrueNAS 12) - 16 GB ECC RAM 256 GB SSD 4x4TB RAIDZ+1 (amd64)

I don’t actually >>need<< “need” my old Raspbian Buster Pi4 instance… I reckon I’m going to cutover this evening… i.e. just swap SSD’s - too easy… I’m WFH tomorrow and Tuesday, and I NEVER go into the office on Wednesdays, so I got bucketloads of time to get this sorted… and I can always resort to looking at the old SSD from my Pop!_OS thinkpad (the desktop machine is having issues with some USB3 stuff - but annoyingly, not EVERYTHING).
It will be a shame, my old Buster system has 197 days uptime! I HATE breaking reliable long term uptimes! But it’s goodbye methinks… adieu mon cherie!
(there’s a machine at one customer - that only I use, which has 6-7 years uptime - and I DO NOT WANT to disturb that kinda rock solid stability [CentOS])

1 Like

All that sounds pretty fun and illustrates my thoughts too. We’ve been running on CentOS 6 for 15 years at work and only fairly recently (4 years?) started using CentOS 7 for new servers. We have a couple odd ball RHEL 5/6 and SUSE servers, but just for a specific purpose. With all the upheaval in the CentOS world “they” are evaluating what we should move to as our new standard server OS. “They” are leaning towards something RPM based like Rocky or AlmaLinux or even Amazon Linux. My thought, as one of the people who have to maintain these servers, is to use Ubuntu 22.04 LTS for all the reasons you hit upon in your struggles.

Ubuntu just seems to work for lots and lots of stuff. Desktop especially, but also server side. 10 years ago Red Hat was supported by everyone. Now days Ubuntu is just as likely to be supported. We don’t have to subscribe to any support agreement to get updates, but could if it happens to be a critical system.

I think I’ve mentioned this before but Ubuntu seems to be the Microsoft of the Linux world. So easy to hate on, but just works for the most part.

Also, I’ve never gotten sucked into the Pi world at all (other than memorizing 51 digits of it :slight_smile: ). I always have enough spare old desktops and laptops to run tests on and use for some odd purpose. Laptops are pretty small and low energy like a Pi. All the stuff works without worrying about some ARM incompatibility.

If I did get sucked in I would probably have even more clutter than I do now. Probably just as well I’m not in the ecosystem.

As far as the reminders…I happen to have a reminder on my Google calendar (not a big Google fan overall) every Wednesday evening to take out both the trash and recycling. It just pops up on my Android phone without bothering to email me. I just about always have my phone on me or nearby. It works for me. Another reminder is the first Saturday of each month to change the furnace filter and check the water softener salt. That’s more often than it really needs to be changed, but it does avoid issues and is probably much more efficient (less restricting on airflow).

2 Likes

Yes the merit of a distro is about the people (or organisation) who build and maintain it.

1 Like

Mint also qualifies, and MX Linux, and KDE Neon.

2 Likes

I’m surprised @nevj didn’t pipe up here about me using a steam hammer on a mosquito :smiley:
you’re 100% “on the money” and I don’t even know why I didn’t think of that! Simple Google Calendar entry reminder - I could even share it with my family so we always know which bin goes out!

But the idea of writing the shell script and configuring email seemed like fun, and I reckon I’ll still pursue that avenue.

My ‘old’ Pi4 4 GB RAM model is now booting off a Ubuntu 22.04.2 Server arm64 image on a USB3 external SSD - got port forward working to it from my router, fail2ban installed and banning. Just got to get OpenVPN (server - free OSS version) working on it now - figure out if I can migrate my old OpenVPN config over to the new instance…

Trouble is - just about ALL THE DOCO that comes up in searches, is for OpenVPNAS (commercial Access Server product) - I actually support an insance of this at a customer, but the OSS thing is a bit different.

1 Like

As far as I can tell, there’s no difference between using a steam hammer on a mosquito and writing a shell script, especially when a simple calendar reminder will do the job. I’ve used Evolution and Thunderbird calendars to do the same thing for years.

Maybe my life is more complicated…or you and Nev have too much time on your hands!

1 Like

@daniel.m.tripp is trading his time for the calender’s time
You are more concerned about your time, so you make the computer do more work, so its (it’s its’) you who are using the steam hammer.

I’m happy to ignore the script writing–let the computer do the work!

2 Likes

If Linux didn’t have scripting capabilities - I wouldn’t use it… I’d be using one of the BSD’s, or actual “UNIX” like on my MacBooks…

The shell and scripting are the biggest drawcards to using Linux for me - without them it would just be Windows anyway (sure there’s powershell, and I’ve recently been learning bits of it [e.g. wrote a powershell script to keep a remote Windows session “alive”] but it’s awful and really spartan compared to the richness of say bash, or ksh or zsh - but again - I’m sick of having to fire up a GUI app to see what Active Directory groups a user’s in - so I think I’m going to automate that in PowerShell).

Anyway - I used my Google Calendar and created alternate fortnightly reminders about which bin to put out - and yeah - that was heaps easier - but - I still want to try my scripting method - for SHITS N GIGGLES!

In other news - I tried to copy the OpenVPN config I was running on Raspbian Buster (Debian 10) (the config is just /etc/openvpn folder - AFAIK) - and it started up just fine and didn’t log any errors - but - I was unable to connect (I test it by disconnecting my phone from wifi, and using its 3G / 4G).

Anyway - I reckon I’m just gonna set OpenVPN up again from scratch (to run on Ubuntu 22.04 headless server on arm64)… I don’t remember it being that tricky when I did it ~3 or so years ago…

1 Like

So many things seem more complicated than what they were years ago .

It was INCREDIBLY EASY - almost “too easy…”

I’m usually very wary of things like :

curl -L https://install.pivpn.io | bash

but I did it anyway - and - it worked!

This time I chose wireguard…

I then ran pivpn add, and created my profile - then I did :

pivpn -qr

and typed my profile in - and it generated a QR code in my terminal - I grabbed that with my phone from the WireGuard app - and HEY PRESTO!

I’m connected to my home network, from 3G / 4G “outside big bad world”!

That was SO easy - I won’t be wary of doing it again…


piping curl into a shell? I think last time I did this - I downloaded the thing at “https://install.pivpn.io” and checked what it did…


OK - I backed out wireguard, and re-installed and default to OVPN… Managed to get my Android phone a VPN session… but can’t get the client to work on Linux… Just about to try and get this working on MacOS… only way I can reliably test, is tether to my phone, then VPN…


Damn! I can’t get OVPN clients to work on MacOS or Linux! But works a treat on Android… Reckon in the morning - I’m gonna work for a couple hours at a nearby cafe in the winery area near my place (Swan Valley) - there’s no WiFi - I’ll just tether to my phone…
I’ll use my personal MacBook I reckon (the work corporate issued MacBook is a RIGHT PITA - constantly prompts to allow certificate - sometimes EIGHT TIMES a day - and it’s not just confirmation, I have to type my f–king password, and if it happens too many times - I HAVE go through the whole hideous rigmarole of it forgetting who I am, typing out my username, network password, MFA, THEN “Do you want to allow Certificate?” question - on my personal MacBook? I hit connect and it connects to work VPN!)…

there’s no WiFi - I’ll just tether to my phone

there’s no WiFi - I’ll just tether to my Glass of Red :yum:

not sorry you left the door, gate, glass open
Have a good nite you old Roo

Oi! I resemble that remark mate :laughing:

I should have added “morning” in there…

I don’t need to go to no steeking winery - I’ve got plonk here… Besides, I might be a pisshead, but I have standards, and never start drinking (alcohol) till the sun’s gotten “decently” past the yardarm!

There’s a spot up there though, in the winery area, where there’s a decent roadside cafe, in a caravan, that makes a decent espresso (it’s not difficult to make a good one IMHO, but it’s exceptionally EASY to make a terrible espresso)… I get up there - order a triple (Tripple) espresso…

Setup my laptop (Macbook) - tether to my phone (pretty good signal) - work away for a bit - then get another one…

BTW - I get up there (Crema Cruiser cafe) on my scooter - no, not the Harley (that’s being repaired) - my e-scooter… Considering that the mostly vacant land across the road from my house backs onto the Swan River - I’m already in “The Swan Valley” - and the e-scooter jaunt up to my “other office” (outdoor cafe) is only about 10 minutes and quite a pleasant jaunt, some bushland, some agricultural land - and I’m there - it’s not a major effort…

that maketh no difference ,
Morning is just a continuum - a continuous nonspatial whole or extent or succession in which no part or portion is distinct or distinguishable from adjacent parts (in this example) of last nites festivities
Time is an illusion

What a crook anytime is 10.00 somewhere !

1 Like

I’m glad your using some PowerShell. It’s pretty capable I think. Most all of what I do is short simple scripts in PS, but pretty decent stuff.

I read lots of people say how poor the command prompt is in Windows. I have to say I don’t see it that way. There is just about nothing I can accomplish at the prompt. What they probably mean is they don’t know how to do something in a Windows command prompt that they can easily do in a Linux shell. But also, the things they are lacking could indeed run under Windows if someone would just write a port or recompile for Windows. Rather than complain about Windows, why not help the situation. That’s not something I can do personally since I’m not really a developer, just a former COBOL programmer that ended up in infrastructure / DevOps.

Lots of what we do is muscle memory or ingrained from decades before. I still miss things I could do with my history or command line editing in Windows that I can’t do as easily in a Linux shell. At least I don’t know how to or don’t think it’s as easy if I do know how.

In BSD we used to be able to pipe stuff directly to the mail program
cat file | mail nevj

100% - I used that method in Solaris for a number of years (decades) - and still use it on modern Linux distributions like Oracle Linux 7 (basically Red Hat or CentOS 7)

The “mailx” binary / command is inordinately more sophisticated than “mail” or even the old mailx on SystemV systems like Solaris or Digital Unix…

I can still remember the rigmarole around uuencode’ing attachments before sending them… (and sometimes, e.g. building an SMTP virus scanner, uuencode decoding attachments)…

These days?

You can still build the whole body of a message as a text file - e.g. with To: and Subject: andFrom: fields near the top…

To: whomsoever-might-care@somebody.com
From: concerned-third-party@bigbrother.com
Subject: dodgy behaviour
BODY of the email message

Then “mailx -a” an attachment (we don’t care about encoding or even filetype!) … And even after all that - we can do something like whack on an footer to the bottom fo the email…

Beautifully elegant, but still, simple, solution, conforming the the KISS UNIX principle…

1 Like

I had forgotten that.
What happens now, is it done behind your back?

Example :

cat $MSGBUFFER | mailx -S smtp="$RELAY" -r "$SENDFROM" -s "$SUBJECT : $SHORT" -c "$CCTO" -b "$BCCTO" -a $PDF "$SENDTO"

-a $PDF

Does all the work… don’t care about what kinda file it is - just plonk it in there!

Under old systems, you’d have to uuencode the file to be attached, then append that stream of text (uuencoded email) into the body of email…

1 Like