how to send message with attachment from crontab?
I wanted to do this using Alpine, but I figured out how to do it using
mutt (info below). I still want to know if Alpine can do this.
I want my system to send me an email message with an attachment at a
certain time of day. I see that Alpine has this option:
-attach file Send mail with the listed file as an attachment.
The problem I'm is that Alpine does not seem to work non-interactively.
So Alpine always appears on the screen waiting for me to send the message.
Is there a way to make Alpine just send the message without interaction?
If not, I can't use it from a crontab.
I found a way to do it with mutt:
some_process | mutt -s "my subject" -a file_to_attach -- my_address
The output of "some_process" becomes the message body.
I was seeing some inconsistency in the way mutt is handling a text/plain
'file_to_attach': It sometimes uses the wrong charset and alters the
appearance of the file (specifically messing up the degree symbol °).
I'd rather have it always use base64 encoding like Alpine does with the
same file, but I don't know how to fix that.
More below on what I'm actually doing in case any of you are interested.
Mike
--------------------------------------------------------------------------
What I'm doing with Google Calendar
I use Google Calendar and I am having gcalcli send me my schedule
("agenda") every evening to remind me of appointments and meetings for the
next several days. So the command is...
gcalcli agenda "$DATE"
...where $DATE represents the desired start date (5 days of output is
given). The output is ANSI color coded. To get output for five days
starting tomorrow, I do this:
gcalcli agenda "$(date -d tomorrow)"
To remove the color I can do this:
gcalcli --nc agenda "$(date -d tomorrow)"
However, the color is informative. My wife's calendar entries are mixed
in with mine, but mine are in cyan and hers are in green. Holidays and
weather info are in magenta. I can use grep to retain the cyan and
magenta parts of the output along with the date info (36 is cyan and 35 is
magenta)...
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]'
...and I can add back in the the removed line between dates...
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]' | perl -pe 's/^([FMSTW])/\n$1/'
...or I can do that while also removing the ANSI color:
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]' | perl -pe 's/\e.*?m//g ; s/^([FMSTW])/\n$1/'
Doing it that way had some benefits, but I have decided that I'll just
stick with output for myself alone and without the weather info by using
the --cals owner option:
gcalcli --nc --cals owner agenda "$(date -d tomorrow)"
Alpine won't display ANSI color (it used to do that), but if the ANSI
color is in an attachment, then it works if I view attachments using less
with the -R option. I had two lines in /etc/mailcap that evoked "less"
with no options, so I added -SR as options to less (I also have those in
my bash aliases, but that seems to be ignored by mailcap):
text/plain; less -SR '%s'; needsterminal
text/*; less -SR '%s'; needsterminal
I want to see my calendar info two ways in these email notifications:
(1) the no-color calendar in the message body showing only my own
appointments and (2) the ANSI-color calendar in an attachment showing
everything (e.g., my wife's appointments, weather, holidays).
This script is doing exactly what I want, even when called from cron, but
see the note below about cron:
-----------begin script on next line-----------------------
#!/bin/bash
RECIPIENT=$(whoami)
TMPFILE=$(mktemp /tmp/delete_me.XXXXXX) || exit 1
gcalcli agenda "$(date -d tomorrow)" > $TMPFILE
gcalcli --nc --cals owner agenda "$(date -d tomorrow)" | mutt -s "Schedule for Tomorrow" -a $TMPFILE -- $RECIPIENT
rm -f $TMPFILE
----------end script on previous line----------------------
At first that script was working differently from cron than from the
command line. I got some help from this web page and fixed it:
http://www.logikdev.com/2010/02/02/locale-settings-for-your-cron-job/
I had to add this line to /etc/environment:
LANG=en_US.UTF-8
Then I had to restart cron:
sudo service cron restart
After that it worked perfectly.
For reasons I cannot explain, mutt used to do this when called from the
command line or from cron:
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="delete_me.KRE97M"
That looked OK in Alpine, but mutt had turned the degree symbols (°) to
double question marks:
Tue May 21 12:00am Forecast for Minneapolis, MN (70?? | 50??)
For some reason the attachents are now looking perfect in Alpine with
proper degree characters and this encoding and charset info:
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: attachment; filename="delete_me.Ohil3z"
Content-Transfer-Encoding: quoted-printable
I don't know what caused mutt to change, but it might have been the file
extension: I was using .txt at first and now I am using a random number
string.
Best,
Mike
I wanted to do this using Alpine, but I figured out how to do it using
mutt (info below). I still want to know if Alpine can do this.
I want my system to send me an email message with an attachment at a
certain time of day. I see that Alpine has this option:
-attach file Send mail with the listed file as an attachment.
The problem I'm is that Alpine does not seem to work non-interactively.
So Alpine always appears on the screen waiting for me to send the message.
Is there a way to make Alpine just send the message without interaction?
If not, I can't use it from a crontab.
I found a way to do it with mutt:
some_process | mutt -s "my subject" -a file_to_attach -- my_address
The output of "some_process" becomes the message body.
I was seeing some inconsistency in the way mutt is handling a text/plain
'file_to_attach': It sometimes uses the wrong charset and alters the
appearance of the file (specifically messing up the degree symbol °).
I'd rather have it always use base64 encoding like Alpine does with the
same file, but I don't know how to fix that.
More below on what I'm actually doing in case any of you are interested.
Mike
--------------------------------------------------------------------------
What I'm doing with Google Calendar
I use Google Calendar and I am having gcalcli send me my schedule
("agenda") every evening to remind me of appointments and meetings for the
next several days. So the command is...
gcalcli agenda "$DATE"
...where $DATE represents the desired start date (5 days of output is
given). The output is ANSI color coded. To get output for five days
starting tomorrow, I do this:
gcalcli agenda "$(date -d tomorrow)"
To remove the color I can do this:
gcalcli --nc agenda "$(date -d tomorrow)"
However, the color is informative. My wife's calendar entries are mixed
in with mine, but mine are in cyan and hers are in green. Holidays and
weather info are in magenta. I can use grep to retain the cyan and
magenta parts of the output along with the date info (36 is cyan and 35 is
magenta)...
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]'
...and I can add back in the the removed line between dates...
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]' | perl -pe 's/^([FMSTW])/\n$1/'
...or I can do that while also removing the ANSI color:
gcalcli agenda "$(date -d tomorrow)" | grep -P '\e\[0;36m|\e\[0;35m|^[FMSTW]' | perl -pe 's/\e.*?m//g ; s/^([FMSTW])/\n$1/'
Doing it that way had some benefits, but I have decided that I'll just
stick with output for myself alone and without the weather info by using
the --cals owner option:
gcalcli --nc --cals owner agenda "$(date -d tomorrow)"
Alpine won't display ANSI color (it used to do that), but if the ANSI
color is in an attachment, then it works if I view attachments using less
with the -R option. I had two lines in /etc/mailcap that evoked "less"
with no options, so I added -SR as options to less (I also have those in
my bash aliases, but that seems to be ignored by mailcap):
text/plain; less -SR '%s'; needsterminal
text/*; less -SR '%s'; needsterminal
I want to see my calendar info two ways in these email notifications:
(1) the no-color calendar in the message body showing only my own
appointments and (2) the ANSI-color calendar in an attachment showing
everything (e.g., my wife's appointments, weather, holidays).
This script is doing exactly what I want, even when called from cron, but
see the note below about cron:
-----------begin script on next line-----------------------
#!/bin/bash
RECIPIENT=$(whoami)
TMPFILE=$(mktemp /tmp/delete_me.XXXXXX) || exit 1
gcalcli agenda "$(date -d tomorrow)" > $TMPFILE
gcalcli --nc --cals owner agenda "$(date -d tomorrow)" | mutt -s "Schedule for Tomorrow" -a $TMPFILE -- $RECIPIENT
rm -f $TMPFILE
----------end script on previous line----------------------
At first that script was working differently from cron than from the
command line. I got some help from this web page and fixed it:
http://www.logikdev.com/2010/02/02/locale-settings-for-your-cron-job/
I had to add this line to /etc/environment:
LANG=en_US.UTF-8
Then I had to restart cron:
sudo service cron restart
After that it worked perfectly.
For reasons I cannot explain, mutt used to do this when called from the
command line or from cron:
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="delete_me.KRE97M"
That looked OK in Alpine, but mutt had turned the degree symbols (°) to
double question marks:
Tue May 21 12:00am Forecast for Minneapolis, MN (70?? | 50??)
For some reason the attachents are now looking perfect in Alpine with
proper degree characters and this encoding and charset info:
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: attachment; filename="delete_me.Ohil3z"
Content-Transfer-Encoding: quoted-printable
I don't know what caused mutt to change, but it might have been the file
extension: I was using .txt at first and now I am using a random number
string.
Best,
Mike