user3734471
user3734471

Reputation: 41

How to use cron to execute mutt to send email?

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" [email protected] < /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" "[email protected]"

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.

Upvotes: 4

Views: 6927

Answers (4)

bigWeld33
bigWeld33

Reputation: 11

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" -- [email protected] < /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!

Upvotes: 1

Hikari Olsen Berg
Hikari Olsen Berg

Reputation: 1

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...

Upvotes: 0

Matthias Braun
Matthias Braun

Reputation: 34373

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" [email protected]'

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

Upvotes: 0

flob
flob

Reputation: 3908

It might be that you just mixed up your arguments?

mutt -s "Test from mutt" -a /tmp/file.jpg -- [email protected] < /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.

Upvotes: 2

Related Questions