4

I want to use cron to execute a mutt script to send an email periodically. I've got the mutt installed and successfully sent emails by executing the mutt script like:

#!/bin/bash
mutt -s "Test from mutt" user@yahoo.com < /tmp/message.txt -a /tmp/file.jpg

Then I tried to execute this script in cron, but cron doesn't send the email as I expect. I'm sure that my cron is working because I used cron to execute another email tool:

#!/bin/bash
mail -s "Hello" "user@gmail.com"

This time cron did the work and sent the email at the expected time. So I'm thinking there maybe some problem when using cron to execute mutt command. Can anyone help me? My cron script is like following:

15 * * * * bash /Users/DengWenzhe/Study/SportsMedia101/sendEmail.sh

EDIT: The reason I want to use mutt is that I want to send email with attachment, which 'mail' version couldn't. If anyone know a command email tool that can work well with cron, that would also be helpful.

user3734471
  • 41
  • 1
  • 2
  • Maybe you have some mutt specific environment variables set in your .bashrc which is not loaded during cron execution? – flob Jun 24 '14 at 15:33
  • @flob I have the same thought, but what should I do to fix it? – user3734471 Jun 24 '14 at 15:37
  • Prefix your command with the missing variables `EMAIL=me@domain.com /command.sh` or set them in you shell script. – flob Jun 24 '14 at 15:38

4 Answers4

2

It might be that you just mixed up your arguments?

mutt -s "Test from mutt" -a /tmp/file.jpg -- user@yahoo.com < /tmp/message.txt 

If that doesn't work check your environment variables. You don't get a login shell during cron execution, so you might be missing EMAIL, MAIL, MAILDIR, REPLYTO or some of those environment variables your mutt configuration depends on.

flob
  • 3,760
  • 2
  • 34
  • 57
  • You might need to put `--` after the last file attachment and before the "to" address. Otherwise `user@yahoo.com` will be treated as a file attachment. This comment is with respect to mutt version 1.5.23.1. – scottkosty Feb 19 '16 at 04:50
  • Thanks, I fixed that :-) – flob Mar 30 '16 at 10:41
1

Maybe a little late, but hopefully this helps anyone who comes across this post in the future.

I have mutt sending an email within a script run by cron. The email wasn't sending until I specified my user's mutt rc file with the flag -F /home/user/.mutt/muttrc in the mutt command.

#!/bin/sh

mutt -F /home/user/.mutt/muttrc -s "Subject" -- destination@email.com < /pathtofile/file.txt

This is using Mutt version 1.9.4 on Ubuntu 18.04.5 LTS. It didn't require anything fancy such as setting environment variables or specifying a particular user to run the cronjob as.

Hope it helps!

bigWeld33
  • 11
  • 1
0

I had the same problem, noticing that the emails would show up in the mailbox for sent messages, yet they didn't reach their recipient.

As flob mentioned, I was missing the MAIL environment variable; in the end, the crontab line that did the job was

# Use a login shell, this provides Mutt with the environment variables it needs
* * * * * bash -l -c 'echo "Body" | mutt -s "Subject" recipient@example.com'

For different ways to provide environment variables, see this answer.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
0

I had same issue and in my case I forgot to add . /home/user/.profile as to make cron able to execute mutt. I hope this helps...