2

I was trying to troubleshoot why my Raspberry Pi isn't sending emails with ssmtp / mail-utils, and so I opened up my gmail account that I use for the Pi. To my surprise, there were 15k unread emails where it had tried to send an email to the email address "pi@pi_name", which was obviously rejected (was trying to send to 123456789@myprovider.com). These emails/rejections occur about once every couple of minutes. The rejections are one thing (I must just have the configuration wrong), but it puzzles me what's going on here. In my crontab -u pi -l I have the following (root's crontab is empty):

30 8 * * * python /.../text.py
* * * * * python /.../record.py

text.py is supposed to send me reminders at 8:30 every day, and record.py is supposed to run every minute to record some audio. In the emails that are being sent, the body of the email is the output from record.py (recorded audio details). My intuition is that the emails are related to the cron job that runs every minute (even though the emails are approximately once every 3 minutes), but nowhere in record.py is there any mention to sending email; it just writes to a MySQL db. text.py sends emails using the command echo “body” | mail -s “subject” x@example.com

I'm perplexed as to what's going on here. I have no idea where the emails are being sent from, and furthermore, everything was working as intended in January of this year (I put my Pi into storage when I went abroad for a few months), and I haven't made any changes to it.

Also weird and maybe related is that when I enter the command python text.py, it randomly works (maybe a 5% chance?) and texts me, otherwise it errors with send-mail: Cannot open smtp.gmail.com:587. I'm not sure under what circumstances it succeeds.

  • The cron and mail logs (usually hidden somewhere under /var/log) might be worth looking at, and also mailq or appropriate to see what's in the local mail queue, if anything. – thrig Sep 15 '15 at 21:56
  • is record.py writing anything to standard output? cron will normally mail that to the owner of the cron – Andy Dalton Sep 15 '15 at 21:57
4

If a cron job prints anything to standard output or standard error, or if the command returns a failure status (i.e. any nonzero exit code), then cron sends the user an email with the output and the status code.

The email is sent to the user via the local email facility, unless you overrode that by setting the MAIL variable in the crontab to a local address. If your local email is forwarded via ~/.forward or /etc/aliases or similar, the emails from cron will obviously be forwarded like the rest of them. If the user's email is undeliverable but the administrator's email works, the administrator (technically: postmaster) will receive bounce messages.

Read the content of these emails to find out what was wrong. If the per-minute job sometimes fails, that's probably what you're getting those emails about.

If your ISP sometimes rejects your emails, that may be due to your sending a large volume of email and being rate-limited as a potential spammer.

3

I would look at the code for text.py to see where it's sending email. I'd then use that as a starting point to find out why it's sending it. Be aware that it may not be directly trying to send to gmail, but to a local address (see below).

Your record.py probably writes either to stdout or to stderr at some point. cron picks this up as output to be mailed to you (as the owner of the crontab).

I suspect that at some point you have configured your Pi to forward emails addressed to your local account to the offsite address 123456789@myprovider.com. I would start by looking at /etc/aliases and ~/.forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.