Send mail with org agenda for upcoming days

Tagged:  
I previously posted a small Emacs script to send a mail with upcoming events from the Emacs calendar. I used the Emacs calendar for years, but now I think I need something a bit more powerful, so I'm slowly migrating to org-mode. Since I like to get regular reminders for upcoming events, I adapted the previous script to use org-agenda:

#!/usr/bin/emacs --script (setq number-of-days 3 org-agenda-show-all-dates nil org-agenda-files `(,(expand-file-name "~/org")) org-agenda-include-diary t european-calendar-style t calendar-week-start-day 1 diary-show-holidays-flag nil diary-file "~/.calendar" mail-addr "address@nomail.invalid") (org-agenda-list nil nil number-of-days) (with-current-buffer org-agenda-buffer-name (unless (eobp) (setq text (buffer-substring-no-properties (point-min) (point-max))) (mail) (mail-to) (insert diary-mail-addr) (mail-subject) (insert "Diary entries generated " (calendar-date-string (calendar-current-date))) (mail-text) (insert text) (mail-send-and-exit nil)))


Adapt the variables to your liking, make the script executable and use it e.g. with cron. Since org-agenda-show-all-dates is set to nil, no mail will be sent if there are no items in your agenda. I also include my diary file, since I still keep some stuff there, but you may want to disable that.
I just set up something similar last night based on the org-mode manual's suggestion about batch mode. The only trouble I had is I usually leave Emacs open forever, and I was getting prompted to steal/proceed/quit because some of the org-agenda-files were open -- no good if I want to put this into my crontab and email myself the output. After mucking about with expect and Expect.pm (surprisingly complicated), I found it was simplest to just put this into my crontab: /usr/bin/yes p | /usr/local/bin/emacs -batch -l ~/.emacs -eval '(org-batch-agenda "#")' 2>/dev/null | /usr/bin/mailx -s'Stuck projects' me@example.com Never even thought about trying to send email from Emacs itself...this idea of Emacs as scriptable is still foreign to me. :-)
Ah, org-batch-agenda. I should have known that there is already something like that. But since I already had that script for the Emacs calendar, I was straightforward to just put org-agenda-list in there. As I wrote, my migration to org is pretty slow. I currently only use it as a beefed up calendar mode, and I haven't yet spend much time with the manual.

The thing I like about Emacs scripting is that you can easily extend it later. When I used "-eval", I often found myself producing longer and longer commands because I added some feature, until I finally decided to put the code into a file. For example, in this case it was important for me to not get any mail when there are no items in the agenda.
Thank you!