this post was submitted on 07 Jan 2024
18 points (100.0% liked)

Programming

13344 readers
10 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS
 

EDIT: Thanks y'all! I got this working by installing mutt and configuring it with my Gmail info. Please note the warning from u/jherazob below--if this were something mission critical I would not want to rely on this solution.

================

Noob question incoming, thanks in advance for any help with this!

I have a specific use case in which I want to send an automated email or text to myself once a day (the message is different each time--otherwise I would just set an alarm, lol!). I'm running Pop_OS on an old desktop computer. Where I'm stuck is getting an email to successfully send from the command line. I'm looking for easy-to-follow instructions that would help me do that, and none of the articles or videos I've come across thus far have helped.

I'm aware of Twilio and other services that send SMS messages, but I'm looking for something free. Especially since I only need to text one person (myself), and infrequently at that.

Below is my attempt to send an email with the telnet command. Nothing ever came through...

XXXXXXXX@pop-os:~$ telnet localhost smtp
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 pop-os ESMTP Exim 4.95 Ubuntu Sun, 07 Jan 2024 15:12:28 -0500
HELO gmail.com
250 pop-os Hello localhost [::1]
mail from: XXXXXXXX@gmail.com
250 OK
rcpt to: XXXXXXXX@gmail.com
250 Accepted
data
354 Enter message, ending with "." on a line by itself
Subject: Test
Body: Is this working?
.
250 OK id=1rMZW4-0002dj-Uy
quit
top 30 comments
sorted by: hot top controversial new old
[–] Radiant_sir_radiant@beehaw.org 15 points 8 months ago* (last edited 8 months ago) (2 children)

I see several issues with your SMTP session.

First, gmail.com will be protected by SPF and DKIM and your message will likely be flagged as spam (or outright rejected) because it's clear that you're not sending on behalf of the real gmail.com.

Second, commands should be in all-caps. A server may accept or reject lowercase keywords.

Third, you need to leave a blank line between the mail headers and the body, so that part of your session would look like so: ...

DATA
354 Go ahead
From: ...
To: ...
Subject: ...

This is the first line of text.
This is the second line.
.
250 Queued

Having said that, many SMTP will require an encrypted connection (SMTPS), many ISPs will block port 25 for residential customers as an anti-spam measure, ESMTP should be preferred over SMTP etc.
If at all possible, you should use a full-featured mail library for this.

[–] pineapplelover@lemm.ee 1 points 8 months ago

Feel free to share the code or code repository. I'm interested in this as well

[–] friendly_ghost@beehaw.org 1 points 8 months ago

Thank you so much for the detailed reply! I'm learning a lot from this thread

[–] SplicedBrainwrap@beehaw.org 10 points 8 months ago (1 children)

If you’re ok using Signal I use https://github.com/AsamK/signal-cli for sending myself texts through command line

[–] friendly_ghost@beehaw.org 4 points 8 months ago (1 children)

Oh sick, thank you! I'll check it out

[–] SplicedBrainwrap@beehaw.org 3 points 8 months ago

Awesome, I actually use this docker container that implements it https://github.com/bbernhard/signal-cli-rest-api

[–] jendrik@discuss.tchncs.de 7 points 8 months ago

Implementing a telegram or discord bot might be easier

[–] Penguincoder@beehaw.org 6 points 8 months ago (1 children)

Yes, you can. Though as another commenter mentioned, doing it like you currently attempted to, is way too much of a hassle.

That is, don't try and negotiate your own SMTP session and content. Being POP_OS is Ubuntu based, you should be able to use the mail command from mailutils package

echo "Is this working?" | mail -s "Subject" recipient@email-address.org

Also might want to consider something like Apprise (Everything and the kitchen sink) or NTFY (Does one thing, does it well) for other types of notification methods.

[–] friendly_ghost@beehaw.org 2 points 8 months ago (1 children)

This didn't work for me, but what I'm gathering from the other comments is I need an SMTP configuration on my machine before I can do this. Thanks so much for the help!

[–] noddy@beehaw.org 2 points 8 months ago

If you use gmail you can create an app password that can be used for this. Or if you have a domain you can e.g. use free tier zoho mail or something to create an email address.

[–] ono@lemmy.ca 6 points 8 months ago* (last edited 8 months ago) (1 children)

Your current approach of talking raw SMTP is likely to be more hassle than is worthwhile, and since the days of permissive SMTP servers are long gone, might not work at all.

Since you appear to be using an Debian-based Linux distro, I suggest this approach:

  • If you don't specifically need exim, consider replacing it with the lightweight dma package (DragonFly Mail Agent): apt install dma
  • Configure dma (or exim) to use your ISP's SMTP server as a smart host. (Or the Gmail SMTP server if your ISP doesn't provide one.)
  • Use the /usr/sbin/sendmail command (which comes with dma or exim) to send messages from your scripting language of choice.

If you prefer to receive messages as SMS, note that most major mobile carriers maintain an email-to-sms gateway for this purpose. Some web searches will probably lead you to the one for your carrier. They usually accept email at an address like 123456789@sms-gateway.example.com

[–] friendly_ghost@beehaw.org 1 points 8 months ago

Thank you so much for this!

[–] bizdelnick@lemmy.ml 5 points 8 months ago

Standard tool to send email is mailx, it is also often aliased as mail.

[–] Fal@yiffit.net 5 points 8 months ago (1 children)

You could probably do something with tasker to create these daily notifications

[–] friendly_ghost@beehaw.org 3 points 8 months ago

Ah, you mean run the script directly on the phone without involving the desktop computer? I hadn't considered that but it's a great suggestion. Thanks!

[–] furrowsofar@beehaw.org 4 points 8 months ago* (last edited 8 months ago)

Many cell providers have a mail to SMS gateway. Just sent email to the correct address and you'll get SMS. As far as sending mail either with bash or with Python. That is quite possible and not hard.

Sorry I do not have the code at hand. Memory is Python standard library has SMTP capability and Bash you can just use the mail command. May have to configure mail on your Linux box too or just use a remote server.

[–] zaphod@lemmy.ca 4 points 8 months ago (1 children)

Any reason not to use something like Gotify and handle it as a notification instead?

[–] friendly_ghost@beehaw.org 1 points 8 months ago

Hadn't heard of it till now but I'll look into it! Thank you!

[–] lps2@lemmy.ml 3 points 8 months ago (1 children)

You're going to have to use an external service for email perhaps connected to something like Postfix or just using the email provider's API - something else to think of if you just need something on your phone as a message / reminder is to use KDE Connect like in this tutorial https://doronbehar.com/articles/using-kdeconnect-to-comfortably-send-sms-messages-from-the-shell/

[–] friendly_ghost@beehaw.org 1 points 8 months ago
[–] ericjmorey@beehaw.org 3 points 8 months ago* (last edited 8 months ago) (1 children)
[–] friendly_ghost@beehaw.org 2 points 8 months ago

Thank you so much!!

[–] Nomecks@lemmy.ca 3 points 8 months ago (1 children)

You can fire a notification to AWS SNS and have it email, text, fire a program, anything.

[–] friendly_ghost@beehaw.org 1 points 8 months ago

Amazing, looking into this now

[–] AVincentInSpace@pawb.social 2 points 8 months ago

If all you need is to send a notification from a script to your phone, I'd like to suggest https://ntfy.sh/

[–] urda@lebowski.social 2 points 8 months ago

I personally use python plus AWS SES to handle outgoing mail. Sure you have to have an AWS account, setup the SES feature, and get out of the AWS sandbox but once that's front loaded you have a great little e-mail service.

[–] jherazob@beehaw.org 2 points 8 months ago (1 children)

As somebody who has been there before, it's 100% possible to use email for notifications and have that fully scriptable, but given how extremely stringent email providers have become over the decades and still getting tougher as we speak, i would very much recommend not depending on email for this. Ideally you'd have multiple channels in case one fails for the REALLY vital stuff, but at least you should have one that is not email. There's many:

  • One of the various ways to generate push notifications that have been mentioned in the thread
  • Chat messages, people have mentioned using Telegram, Signal or others, and some of those have desktop version too
  • Yes, email, but not as the only way, you don't wanna stop receiving notifications because Google just decided your IP is spamming and is bouncing all your alerts
  • One of the various cloud notification things like the AWS ones mentioned (pretty sure all the cloud providers have their own version)
  • Something like a monitoring service or self-hosted server (I've used Zabbix in the past and like it, although this is FAR more involved)
  • Text messages? This will depend on services on your area, here they're so rare i never consider this option
  • Others i can't think about right now (need more coffee)

Whatever you do have in mind that ANY of these can fail, so if there's anything truly critical be sure to both have a way to know if one notification system is failing, and to have a plan B to receive notifications/alerts even if one is down. Depends on how critical stuff is of course, for tests you don't need triple redundancy or whatever, but for the service the company's income depends on you don't wanna have a system that stopped working and you never knew.

[–] friendly_ghost@beehaw.org 2 points 8 months ago

This is great advice, thank you so much! I can feel the weight of countless bitter experiences in your post, and I am taking that shit to heart. Thank you again

[–] DrJenkem@lemmy.blugatch.tube 1 points 8 months ago* (last edited 8 months ago)

Lots of valid suggestions already here. But I'll throw one more to the pile. You could use the Pushbullet free plan (100 notifications per month). And then either use a python library or Google for a bash script to help with getting the notifications out. Then have Pushbullet installed on your phone. It might also support email or SMS or something else if you don't want the Pushbullet app installed.

[–] pineapplelover@lemm.ee 1 points 8 months ago

Feel free to share the code or code repository. I'm interested in this as well