Skip to content

Latest commit

 

History

History
153 lines (104 loc) · 9.84 KB

EmailForRPi.md

File metadata and controls

153 lines (104 loc) · 9.84 KB

Send and Receive e-mail on Raspberry Pi

I don't process much email on my Raspberry Pis. Perhaps that's why I missed the fact that the Lite distribution of RPi OS no longer includes any mail-related apps. The need for e-mail came up the other day, and so I launched an effort to install that capability. This recipe chronicles that effort.

Since I didn't need much e-mail firepower I decided to install GNU's mailutils package. It lends itself to scripting, and I wanted to minimize the overhead of all of the e-mail componentry. After getting into the install, I was reminded of some things I'd forgotten... namely that there are several components involved in setting up email on a host system. A simplified view of the SMTP (that's Simple Mail Transport Protocol) is shown in the figure below - and this is just for sending email... receiving email is covered by POP3, IMAP, etc. So much for simple!

But let's not be deterred by the unknowns - what's the worst result possible from such experiments?

The SMTP Soup

Figure 1: The SMTP SoupFigure source

Installing mailutils on Raspberry Pi OS, bookworm "Lite":

sudo apt update && sudo apt -y instll mailutils

You'll see quite a lot of output from these commands. A quick scan reveals that exim4 is being installed (as the MTA?), and then there's this line:

Setting up exim4-daemon-light (4.96-15+deb12u2) ...

I take heart from the use of the word -light. That suggests that configuration of the MTA will take something less than a man-year of effort :/ . I also wonder at the Raspberry Pi organization's selection of exim over postfix which is considered by some a better alternative. Oh!... now I get it - exim was developed at the University of Cambridge - they're next-door neighbors with the RaspberryPi.com crowd. Well - I'm sorry RaspberryPi.com, but there will be no home-cooking here! LOL

Let's see if there's a tool to tell us what MTA is in use by our system... Turns out there is, because while sendmail is no longer in widespread use, it was the first MTA, and so is still used as the de facto MTA program in usr/sbin. We can use the dpkg command to learn what's been installed to respond to /usr/sbin/sendmail calls:

$ dpkg --search /usr/sbin/sendmail
exim4-daemon-light: /usr/sbin/sendmail
# A-ha! home-cooking confirmed! 

All of the update-alternatives entries from the mailutils installation log refer to /usr/bin/*.mailutils scripts, so we'll assume for now that replacing exim4-daemon-light can be accomplished without entanglements. As it turns out, there is another man page for exim4-daemon-light and other potentially useful documentation, but unfortunately it also contains a mind-numbing array of options to deal with. That's too bad... wanting to use email on my RPi is not the same as wanting to wallow around in the muck and mire of endless details and SMTP minutiae.

If you want to try exim4-daemon-light, you can start by running the debconf-driven configuration program as follows (after installing mailutils of course):

$ dpkg-reconfigure exim4-config

I'll be taking a different route because I simply do not see the need for something this complex to provide email service for a single (or several) users from a Raspberry Pi!

Lightweight MTA alternatives to exim4 and postfix:

Some alternatives to exim/postfix (NB I'm not sure exactly what all of these actually do!?) :

  • dma, a.k.a. DragonFly Mail Agent
  • msmtp (+ an adjunct: msmtp-mta); msmtp homepage
  • nullmailer; useful w systems not always online; laptops, intermittent ISP service
  • ssmtp; no maintained since 2019, but still popular & available
  • extsmail; not exactly an MTA alternative
  • esmtp; appears to be deprecated/not maintained since 2009

I'm not going to expend much effort analyzing these alternatives. I'll choose one based on dead reckoning.

I choose msmtp.

Install, configure & test msmtp:

sudo apt update && sudo apt install msmtp msmtp-mta

Poking around afterwards:

$ dpkg --search /usr/sbin/sendmail
msmtp-mta: /usr/sbin/sendmail

$ ls -la /usr/sbin | grep msmtp
lrwxrwxrwx  1 root root        12 Feb  5  2023 sendmail -> ../bin/msmtp 

$ man msmtp
# gives a good overview & suggestions for configuration

I'm going to use the sample config file ~/.msmtprc from the Baeldung blog:

# Default settings
defaults
auth    on
tls    on
tls_trust_file    /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
#
account    mail
host       smtp.gmail.com
port       587
from       [email protected]
user       [email protected]
password   d**************s

account default : mail

Now, testing:

Using msmtp:

echo -e "Subject: regardez moi\n\nSending regards from my RPI Terminal." | msmtp -a mail [email protected]

Using mail/mailutils; -A option designates attachment:

mail -s "the subject is testing" -A message.txt [email protected]

This all works for me. FWIW, as shown in the ~/.msmtprc file above, I am using my gmail.com account as the external SMTP server. "The Trick" for getting that to work is actually in two steps conducted from your Gmail accounts page:

  1. Enable 2FA
  2. Create an app password; I followed these instructions to do that.

What's next?

I suppose at some point, I will have to try setting up either the exim or postfix MTAs (maybe when I get my Raspberry Pi 5?). And I'll have to get a "real" email client (neomutt?) installed on the Pi for thos occasions when I need/want to send & receive email. So, that's all for now.

REFERENCES:

  1. Postfix vs. Sendmail vs. Exim; fm mailtrap.io; good article comparing several MTAs
  2. Send Emails Using Bash; fm mailtrap.io; decent article, but ignore their cmts re Gmail
  3. Sending Emails From Terminal In Linux; fm Baeldung; good, quick overview (recommended)
  4. Windows Subsystem for Linux: Tips and Tricks; consider lightweight MTA ssmtp
  5. mail; a discussion in the RPi forums, mailutils & smtp; some useful observations :)
  6. Mail on Buster; another (older) discussion in the RPi forums; mailutils & smtp
  7. sSMTP - Simple SMTP; no longer maintained; probably not a good choice
  8. Setup Exim4 to Send Email from Terminal With Raspberry PI (with examples)
  9. msmtp; a better lightweight server, easy to configure
  10. Q&A: Light program (mini MTA?) for system mail; brief comparison of lightweight smtp
  11. Sending Email via Remote SMTP in Linux (SSMTP)
  12. Configure Postfix on Linux To Use Gmail SMTP Relay
  13. Send an email notification when a user logs into your raspberry pi via SSH
  14. Q&A: How to make crontab email me with output?
  15. Q&A: How is mail actually sent when I use the Linux "mail" command?
  16. Q&A: install ssmtp in Debian buster
  17. Q&A: Which option ("internet site", "internet with smarthost", "satellite system") should I choose in postfix configuration?
  18. Mailutils smtp
  19. Difference Between IMAP, POP3, and SMTP Email Protocols
  20. A Search: 'is exim simple to configure'
  21. A Search: 'configure exim4 for raspberry pi'
  22. A Search: 'raspberry pi install & configure mailutils'
  23. Package: mailutils-mda (1:3.15-4)
  24. How to Setup IMAP, 2FA, and App Passwords on Gmail Accounts