You are not logged in.

#1 2021-09-11 21:50:45

leodova
Member
Registered: 2021-09-07
Posts: 14

[SOLVED] Sending email from the command line

Hi everyone...

I'm playing around with trying to send an email through gmail's smtp from my command line. I'll eventually be using it to send weather alerts to my phone. However when I try to install ssmtp is says that there is no installation candidate, that maybe it's obsolete or something.

Can I use sendmail instead?

Last edited by twoion (2021-09-11 23:34:09)

Offline

#2 2021-09-11 22:36:05

twoion
一期一会
Registered: 2015-08-10
Posts: 3,318

Re: [SOLVED] Sending email from the command line

ssmtp, msmtp, postfix, exim, sendmail itself etc provide an implementation of the sendmail command that interacts with the mailsystem currently installed.

Unless you know how to set up postfix, I recommend you get rid of ssmtp and use msmtp to get your mail out.

However, if you plan on having multiple Linux users (with different UID or GID) send email, then postfix would be preferred. This would for example be the case if you would run the daemon you mentioned that should be able to send email as a daemon process not tied to your user. However, if you run the daemon as your user, for example using systemd --user services, then you could stick with msmtp.

Using msmtp as a single user is simple. Install msmtp, create the ~/.msmtprc file with the account configuration. Then you can use the 'msmtp' command just like you would use sendmail, including echo body | msmtp style email sending. See https://wiki.archlinux.org/title/Msmtp for a good guide.  I think if you install msmtp-mta, it will just alias sendmail to msmtp. No experience with that setup on Debian or Ubuntu, though.

Personally, I've been using msmtp for more than 7 years together with the mutt email program.

On our BunsenLabs servers, however, we are using postfix to send email via smarthost/email relay (in our case, sendgrid), which is the pattern you would be using if you'd decide to setup postfix. THis could be convenient if you wanted to do more things with email later in your system. In order to set this up, you can follow any guide that is teaching you how to do "postfix with smarthost" or "postfix with email relay". The setup in this case is also fairly minimal. For example, here's main.cf from our forums server

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = mailer.bunsenlabs.org
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, forums.bunsenlabs.org, localhost.bunsenlabs.org, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
compatibility_level = 2
smtp_tls_loglevel = 1
smtpd_tls_loglevel = 1

The authentication for the $relayhost are in a postfix map, sasl_passwd. That's all you need to get postfix working.

Offline

#3 2021-09-11 23:05:43

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Sending email from the command line

Thanks. I set up msmtp like you said and it's working pretty well! There won't be anyone else sending emails, just me. My next step will be figuring out how to send the output of my weather command into an email and send it off to my phone!

I also stored my gmail password in a gpg file according to the instructions here

https://wiki.debian.org/msmtp


By the way, I had to turn on "less secure apps" in my gmail account for this to work. It kept returning authentication failure.

Last edited by leodova (2021-09-11 23:32:43)

Offline

#4 2021-09-11 23:33:31

twoion
一期一会
Registered: 2015-08-10
Posts: 3,318

Re: [SOLVED] Sending email from the command line

leodova wrote:

By the way, I had to turn on "less secure apps" in my gmail account for this to work. It kept returning authentication failure.

The only way around this is using oauth2 with google's API, which is only available in a downstream patch: https://wiki.archlinux.org/title/msmtp# … _for_Gmail. As long as you're using app passwords and have 2FA set up on your gmail account, this should be fine.

Offline

#5 2021-09-12 00:04:25

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Sending email from the command line

Thanks!

Last edited by leodova (2021-09-12 13:02:11)

Offline

Board footer

Powered by FluxBB