How to get MAILTO in cron to work?
I installed ssmtp in Ubunutu 20.x. and configured it correctly. The ssmtp program works fine on the command line and in a bash script run by cron.
The cron also has MAILTO="aaa@xxxx.com" just above
* * * * * echo "this is a test"
It does not work. I get the error:
Nov 5 23:49:05 saturn cron[204539]: sendmail: RCPT TO:aaa@xxxx.com (550 unable to verify sender address.)
It puts the message "this is a test" in "dead_letter" file in my home directory.
How do I get the MAILTO in cron to send me an email?
12 Replies
If xxx.com is not your Linode and you don't have a mail server that can send mail to xxx.com, you're seeing the expected behavior.
If your Linode is xxx.com and you don't have a mail server, then use
MAILTO=aaa
This will deliver the message to the local account aaa.
-- sw
I got lost here. I do not run a full mail server on my Linode. I only have the ssmtp program which runs great from command or from instide a bash script:
/usr/sbin/ssmtp aaa@xxx.com < email.txt
… but not with cron!
If xxx.com is not your Linode
aaa@xxx.com is an email address on my tiny $5/mo. Pair Networks (pair.com) shared server that I use for email (via MX record). It is too much trouble to run my own incoming (postfix) mail server. I route web traffic to my Linode and mail to my little Pair server via DNS records at the registry. Works great.
The MAILTO=aaa@xxx.com should work. I've seen all sorts of examples where people us MAILTO="my-address@gmail.com"
Perhaps I need to install mailutils?
What am I not understanding here? I've researched and worked for hours on this!
I route web traffic to my Linode and mail to my little Pair server via DNS records at the registry. Works great.
Obviously, cron doesn't know about any of that. cron is submitting the cron job stdout/stderr to mail directly using the mail command line… Probably something at the tail end of a pipe that looks like:
mail -s "This is the subject" aaa@xxx.com
mail hands the message off to sendmail for transmission. sendmail is contacting xxx.com to transfer the mail. xxx.com is asking "Who is this cron person? Say more…" sendmail doesn't know what to say so it probably just hangs up and the receiver rejects the message. That's what 550 means. See:
https://www.enginemailer.com/blog/email-error-550-what-is-it-and-how-to-fix-it
sendmail is putting the message in dead letters so as to not lose it in the ether…sendmail is trying to be nice to you.
You need to look at the From header in one of your dead letters and make sure the destination can authenticate the sending account. You'll probably need to add some configuration to sendmail to accomplish that. See man sendmail or man ssmtp. Until you do, you should assume that MAILTO only works for local accounts; e.g., MAILTO=aaa. Installing mailutils might give you the wherewithal to change that. I don't know…
You may be able to change your MAILTO to something like:
MAILTO="-x -y -z aaa@xxx.com"
where -x -y -z are command-line parameters to mail according to
https://linux.die.net/man/1/mail
that add the authentication info the receiver needs. Then again, cron may just puke on your cron tab file, too.
I've seen all sorts of examples where people use MAILTO="my-address@gmail.com"
Do you know if these "all sorts of examples" have a mail server at the sender…or use a correctly-configured sendmail? I already know the answer to that question…
-- sw
Maybe the issue is "sendmail"?
~$ sendmail -V
sSMTP 2.64 (Not sendmail at all)
Perhaps I need to install something besides the 'ssmtp' program? Maybe Postfix?
Thanks.
Maybe the issue is "sendmail"?
~$ sendmail -V
sSMTP 2.64 (Not sendmail at all)
Perhaps I need to install something besides the 'ssmtp' program? Maybe Postfix?
Thanks.
According to this:
https://linux.die.net/man/8/ssmtp
ssmtp and sendmail are aliases. According to this:
https://linux.die.net/man/5/ssmtp.conf
ssmtp has a configuration file where you can set up the proper authentication.
Perhaps I need to install something besides the 'ssmtp' program? Maybe Postfix?
You can install the real sendmail if you want but configuring it will be a nightmare. You can install postfix if you want (and it will be easier to set up than sendmail). Both will require care/feeding, SPF, DKIM, DMARC, reputation management, etc.
Frankly, I'd just try setting up ssmtp.conf correctly first. This is going to have side effects on your web email setup so make sure you test both. When you finally get it right, your web email should work a lot better…be interoperable with a lot more systems (like mine, for example, it's pretty anal about a sender identifying himself properly).
-- sw
I'm sure I have /etc/ssmtp/ssmtp.conf set up correctly since ssmtp works just fine from both the command line and within a bash script.
Mail comes right to the server I send it to… in this case a service I use called smtp2go.com and followed their advice:
https://www.smtp2go.com/setupguide/ssmtp/
This just forwards email to my pair.com mailboxes. (Yes I could do that directly but I like using SMTP2GO because I can easily see stats on delivery, headers, etc.)
But ssmtp is not working with cron and there is no way I want to spend a day trying to configure Postfix.
I think the issue is the crontab system is just not seeing/finding the ssmtp program. What it does instead is create a file called "dead_letter" and writes to that… and just adds to it each time.
Thanks.
Here is the ssmtp.conf file I have:
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=my-address@my-domain.com
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail.smtp2go.com:2525
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=pluto
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES
AuthUser=my-address@my-domain.com
AuthPass=xxxxxxxxxxxxxxxxxx
UseSTARTTLS=YES
FromLineOverride=YES
I think the issue is the crontab system is just not seeing/finding the ssmtp program.
Of course it doesn't… mail is default part of the system. It's always there. The admin has to install ssmtp. ssmtp has an alias, sendmail. ssmtp probably ignores ssmtp.conf if it's invoked as sendmail (yes, it can do that)
mail only knows about sendmail…not any of the aliases/work-alikes you might have installed/created for it…and the sendmail alias is not as compatible with the real sendmail as much as mail thinks it ought to be. None of that is mail's fault…or crons. It's ssmtp's (which you explicity installed…probably overwriting the sendmail that got installed when you installed your distro).
What it does instead is create a file called "dead_letter" and writes to that… and just adds to it each time.
That is what mail is supposed to do if it can't send the message (i.e., sendmail returns an error).
Here is the ssmtp.conf file I have:
Sorry, I can't help you with this. I have a postfix server that takes care of all this for me.
You are either SOL or you have to bring up a mail server that acts like a "real" mail server from mail's perspective (absolutely compatible with sendmail). You can also re-engineer your crontab entry so that you don't depend on mail:
- set MAILTO=""; and
- change your command to something like:
/the/path/to/my_program 2>&1 | /usr/bin/ssmtp
or, better yet, a wrapper script:
#!/bin/bash
#
tmpfl=$(mktemp)
/the/path/to/my_program 2>&1 >> $tmpfl || /usr/bin/ssmtp < $tmpfl
rm -f $tmpfl
and invoke it in your crontab as
/the/path/to/my_wrapper_script
You can figure out the details…
-- sw
Sorry, I can't help you with this. I have a postfix server to take care of this for me.
-- sw
Yes, I have workarounds as you mentioned. I guess I thought this would work out of the box as I've seen a number of examples that show that MAILTO should work with ssmtp.
This works fine:
05 05 * * * create-email-file.sh && /usr/sbin/ssmtp me@my-domain.com < email.txt
Although it it is easier to just send the email out of the .sh itself.
code to create email.txt file
.
.
/usr/sbin/ssmtp me@my-domain.com < email.txt
At the end of the day this was more about "Can I get this to work, and if not, why not?" In other words I was curious… and bored. Now that I've spent several hours on it, I'm moving on to something else.
Thanks for your help… as always.
I guess I thought this would work out of the box as I've seen a number of examples that show that MAILTO should work with ssmtp.
Can you point me to one of these? Can you point me to an ssmtp setup that works with MAILTO? Maybe I can puzzle out the differences between a working example and what you have…
-- sw