Re: Can cron e-mail HTML?

2007-07-19 Thread Tom Evans
On Sat, 2007-07-14 at 19:14 -0400, Mikhail Teterin wrote:
 On субота 14 липень 2007, Daniel Bye wrote:
 = So it's beginning to look as if your best bet is in fact to make your
 = script handle sending the mail.
 
 Yeah, seems like it...
 
 = Not the cleanest solution, but one that will get your messages formatted
 = exactly how you want them.
 
 Well, I started looking into how much effort would it be to translate the 
 strings returned by libmagic(3)'s routines into Content-Type.
 
 If it is easy enough, I could hack cron to analyze the job's output using 
 magic_buffer(3) and set Content-Type if anything recognizable is detected...
 
 The translation is the difficult part :-( Instead of the standardized
 
   text/html
 
 for example, libmagic returns:
 
   HTML document text
 
 It is trying to be human-readable, while I need the machine-readable strings.
 
 There is stuff on-line that does the translation, but it is in much 
 higher-level languages (like PHP), which think, hash-tables are free :-)
 
 Oh, well...
 
   -mi

Or you could just use sendmail?

30  4  *  *  1-6  ~/bin/foo 21 | sendmail [EMAIL PROTECTED]

Sendmail will read in any headers you put into the message. Eg:

 $ cat sample.htmlh 
Subject: really?

Really?


will work just fine and set the email subject header when piped into
sendmail.

Or you could patch cron to use libmagic, and have cron scripts that will
only work on one box. Hmmm, decisions, decisions...


signature.asc
Description: This is a digitally signed message part


Re: Can cron e-mail HTML?

2007-07-19 Thread Mikhail Teterin
On четвер 19 липень 2007, Tom Evans wrote:
= Or you could patch cron to use libmagic

Done:

http://aldan.algebra.com/~mi/cron-mime.diff

It even works now...

= and have cron scripts that will only work on one box.

And send-pr the diffs to FreeBSD :-)

-mi


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-19 Thread Tom Evans
On Thu, 2007-07-19 at 07:55 -0400, Mikhail Teterin wrote:
 On четвер 19 липень 2007, Tom Evans wrote:
 = Or you could patch cron to use libmagic
 
 Done:
 
   http://aldan.algebra.com/~mi/cron-mime.diff
 
 It even works now...
 
 = and have cron scripts that will only work on one box.
 
 And send-pr the diffs to FreeBSD :-)
 
   -mi
Sarcasm really doesn't work on the internet does it :)
Teaching cron about file types/mime types is an awful idea - sounds like
something you'd find in gentoo.

Just because something can be done, doesn't mean it should be :)


signature.asc
Description: This is a digitally signed message part


Re: Can cron e-mail HTML?

2007-07-19 Thread Mikhail Teterin
On четвер 19 липень 2007, Tom Evans wrote:
= Teaching cron about file types/mime types is an awful idea

Why? My particular cron-job generates HTML. Somebody else's might generate a 
JPG image -- from their telescope every morning. There is no reason for these 
jobs to have to do the e-mailing on their own.

Cron has this functionality, it just needs to be improved to match the 
modern-times expectations (MIME was introduced in the previous millennium.)

And if you are worried about feature-creep, well, you should've objected 
back when piping to sendmail was put into cron in the first place. After all, 
ALL cron jobs (including the purely textual ones) could have explicit piping 
into a mailer... If you don't mind cron generating the From: and the Subject: 
headers, you should not mind it generating the Content-Type:.

= - sounds like something you'd find in gentoo.

And then I plan to add magick-handling to mail(1) -- to allow you to e-mail a 
file with the properly-set Content-Type.

Yours,

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-16 Thread Ruben de Groot
On Sat, Jul 14, 2007 at 06:21:16PM -0400, Mikhail Teterin typed:
 = To accomplish this I have my cron job run a script like this
 
 Sorry, I missed the most important part. Your script just uses /usr/bin/mail, 
 the same way cron does. You are not adding anything, not already present in 
 cron -- your script should simply produce output to stdout. Cron will mail 
 all that to the address specified in MAILTO=... part of your crontab 
 automatically.
 
 AFAIK, to make the e-mail message treated as a MIME one, the MIME-Version: 
 1.0 and Content-Type: ... have to be among _headers_.
 
 I'm afraid, it is not possible to directly manipulate the message's headers 
 using mail(1), which is why I asked my question in the first place...

Just for the record, cron seems to be using /usr/lib/sendmail, not mail(1):

malenfant# grep -r _PATH_SENDMAIL /usr/src/usr.sbin/cron/cron/config.h
#if !defined(_PATH_SENDMAIL)
# define _PATH_SENDMAIL /usr/lib/sendmail
#define MAILCMD _PATH_SENDMAIL  /*-*/
malenfant# grep _PATH_SENDMAIL /usr/include/paths.h
#define_PATH_SENDMAIL  /usr/sbin/sendmail

cheers,
Ruben

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-15 Thread Derek Ragona

At 03:03 PM 7/14/2007, Mikhail Teterin wrote:

Hello!

I have a script launched from cron every morning, that gets certain data over
the Internet from a remote computer, compares the new data with that from the
previous day, and outputs the difference (if any).

I'm relying on the fact, that cron e-mails me the output of each job.

However, I modified the script recently to produce the output (if any) in
HTML, rather than in plain-text format.

The HTML arrives by e-mail just as well as plain text used to, but no e-mail
program will render it as such, because neither the cron(8), nor the mail(1),
which cron uses to send e-mail, creates MIME messages...

How can I force the ``Content-Type: text/html'' header without hacking cron's
sources? I'd rather avoid poluting my script with e-mail sending code...

Maybe, cron should apply file(1)-like logic to the e-mailed content?

Thanks for any hints. Yours,


You need to change your script to send the email itself.  I have many 
scripts that email reports, legs, and html reports.  To accomplish this I 
have my cron job run a script like this (I have simplified the script you 
should be able to use it as a base):


#!/usr/local/bin/ksh
# set full paths for all commands needed, and files needed

MAIL=/usr/bin/mail
MAILFILE=/tmp/mail_file
#fill in your correct email or alias you wish to use
[EMAIL PROTECTED]
RM=/bin/rm
DATE=/bin/date
AWK=/usr/bin/awk
LS=/bin/ls
GREP=/usr/bin/grep
FIND=/usr/bin/find
TODAY=`$DATE +%m-%d-%Y`
REPORT_LOG_HEADER=/usr/local/etc/report_log_header
REPORT_LOG_FOOTER=/usr/local/etc/report_log_footer

# start the mailfile with the proper header,
# in this case it is an HTML header
cat $REPORT_LOG_HEADER  $MAILFILE

#put more stuff into the report . . .
echo$MAILFILE
echo$MAILFILE
# Add any processing or log files to the middle of the mail file here
# you can even put HTML codes in here
echo BR BR  $MAILFILE
echo$MAILFILE
# add the correct HTML footer
cat $REPORT_LOG_FOOTER  $MAILFILE
# send it to yourself
$MAIL -s the report name $MAILTO  $MAILFILE
$RM $MAILFILE


-Derek

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
MailScanner thanks transtec Computers for their support.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-15 Thread Daniel Bye
Mikhail Teterin wrote:
 On субота 14 липень 2007, Daniel Bye wrote:
 = So it's beginning to look as if your best bet is in fact to make your
 = script handle sending the mail.
 
 Yeah, seems like it...
 
 = Not the cleanest solution, but one that will get your messages formatted
 = exactly how you want them.
 
 Well, I started looking into how much effort would it be to translate the 
 strings returned by libmagic(3)'s routines into Content-Type.
 
 If it is easy enough, I could hack cron to analyze the job's output using 
 magic_buffer(3) and set Content-Type if anything recognizable is detected...
 
 The translation is the difficult part :-( Instead of the standardized
 
   text/html
 
 for example, libmagic returns:
 
   HTML document text
 
 It is trying to be human-readable, while I need the machine-readable strings.

Is /usr/share/misc/magic.mime of any use? Apparently it is consulted by
file(1) when called with -i. According to libmagic(3), magic_open() with
the MAGIC_MIME flag should do the same.

Cheers,

Dan
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-15 Thread Mikhail Teterin
On неділя 15 липень 2007, Daniel Bye wrote:
= Is /usr/share/misc/magic.mime of any use? Apparently it is consulted by
= file(1) when called with -i. According to libmagic(3), magic_open() with
= the MAGIC_MIME flag should do the same.

Yes, indeed -- just the ticket... Thanks.

Now, I have not received any e-mails from the cron modified as per the
linked patch yet, but if anyone cares to review it, please, do so. It 
compiles :-)

http://aldan.algebra.com/~mi/cron-mime.diff

It increases the application's buffering of the job's output from 1 character 
to BUFSIZ and, if requested, passes the first thus-read buffer to 
magic_buffer().

If that succeeds, the Mime-Version and Content-Type headers are injected into 
the outgoing e-mail...

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-15 Thread Derek Ragona

At 05:02 PM 7/14/2007, Mikhail Teterin wrote:

Derek Ragona wrote:
= = I'd rather avoid poluting my script with e-mail sending code...

= You need to change your script to send the email itself.

Thank you, Derek, but -- as I stated already -- I wanted to see, if this can
be avoided...


Doing so makes it very non-portable.  This would make your cron changes 
needed on any system and version you want to implement this behavior.



Since you posted your script, I'll comment on it. First of all, you don't 
need

ksh for anything you are doing in this script. FreeBSD's /bin/sh is enough
(we aren't Solaris :-) -- but is 9 times smaller here (amd64).


I don't need ksh for the particular sample I gave you, which is pared down 
from any I use.  I posted a simple boilerplate that anyone who reads the 
list can use.  For most things /bin/sh works well, but the overhead of ksh 
on a modern server is negligible.




Now, instead of redirecting each line of output into MAILFILE, then mailing,
and removing it, you should be either outputing everything directly into
mail:

{
cat $REPORT_LOG_HEADER
echo$MAILFILE
echo$MAILFILE
echo BR BR  $MAILFILE


cat $REPORT_LOG_FOOTER
} | $MAIL -s the report name $MAILTO


This was done as an example.  In many cases I have reports generated in 
html, and simple email the URL of the report.




or, if you want to use the temporary file, use exec to redirect into it
_once_, instead of _on every line_:

exec  $MAILFILE
cat $REPORT_LOG_HEADER

printf  \n \nBRBR\n
$MAIL -s the report name $MAILTO  $MAILFILE
$RM $MAILFILE

This may look nicer, but is not, because temporary files are nasty, and you
need to be sure, that you remove them in case you are interrupted (trapping
signals, etc.)


I removed the signal handling from my example as I didn't want to add that 
much complexity.  It is trivial to add proper signal handling.




I was surprised, one can not redirect into a pipe directly. The following did
not work, as I expected, with neither /bin/sh nor /usr/local/bin/ksh93. The
following, I thought, would be the same as my first example, only
nicer-looking:

exec | $MAIL -s the report name $MAILTO
cat .

But it is not. So you have to chose from one of the first two examples.


This is UNIX.  Meaning there are many ways to accomplish tasks.  Just 
choose the tools and methods you want.  I strive to make cron scripts 
simple and portable.  I support a multitude of servers running different 
UNIX versions and flavors.


-Derek
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Mikhail Teterin
Derek Ragona wrote:
= = I'd rather avoid poluting my script with e-mail sending code...
 
= You need to change your script to send the email itself.

Thank you, Derek, but -- as I stated already -- I wanted to see, if this can 
be avoided...

Since you posted your script, I'll comment on it. First of all, you don't need 
ksh for anything you are doing in this script. FreeBSD's /bin/sh is enough 
(we aren't Solaris :-) -- but is 9 times smaller here (amd64).

Now, instead of redirecting each line of output into MAILFILE, then mailing, 
and removing it, you should be either outputing everything directly into 
mail:

{
cat $REPORT_LOG_HEADER
echo$MAILFILE
echo$MAILFILE
echo BR BR  $MAILFILE


cat $REPORT_LOG_FOOTER
} | $MAIL -s the report name $MAILTO

or, if you want to use the temporary file, use exec to redirect into it 
_once_, instead of _on every line_:

exec  $MAILFILE
cat $REPORT_LOG_HEADER

printf  \n \nBRBR\n
$MAIL -s the report name $MAILTO  $MAILFILE
$RM $MAILFILE

This may look nicer, but is not, because temporary files are nasty, and you 
need to be sure, that you remove them in case you are interrupted (trapping 
signals, etc.)

I was surprised, one can not redirect into a pipe directly. The following did 
not work, as I expected, with neither /bin/sh nor /usr/local/bin/ksh93. The 
following, I thought, would be the same as my first example, only 
nicer-looking:

exec | $MAIL -s the report name $MAILTO
cat .

But it is not. So you have to chose from one of the first two examples. Yours,

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Mikhail Teterin
= To accomplish this I have my cron job run a script like this

Sorry, I missed the most important part. Your script just uses /usr/bin/mail, 
the same way cron does. You are not adding anything, not already present in 
cron -- your script should simply produce output to stdout. Cron will mail 
all that to the address specified in MAILTO=... part of your crontab 
automatically.

AFAIK, to make the e-mail message treated as a MIME one, the MIME-Version: 
1.0 and Content-Type: ... have to be among _headers_.

I'm afraid, it is not possible to directly manipulate the message's headers 
using mail(1), which is why I asked my question in the first place...

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Mikhail Teterin
On субота 14 липень 2007, Daniel Bye wrote:
=  How can I force the ``Content-Type: text/html'' header without hacking
=  cron's sources? I'd rather avoid poluting my script with e-mail sending
=  code...
= 
= Alter your script to add the 'Content-Type: text/html' header.

No, I'm afraid, doing this will make the quoted text part of the _body_ of the 
message.

=  Maybe, cron should apply file(1)-like logic to the e-mailed content?

= No, cron doesn't need any knowledge of how to render email.

I was not advocating adding such knowledge. My suggestion was to make cron add 
proper Content-Type, so that the /recepient's e-mail program/ will render the 
message correctly. My scripts generate HTML, someone else could be generating 
JPG images (from their web-camera, every morning)...

= The script itself doesn't have to send the mail - cron will handle that if
= there is any output when it exits, but you /can/ add headers to the message
= as you need.
= 
= Just make sure any custom headers come before the empty line delimiter
= between headers and body, and most mail readers should do the right thing.

The empty line is inserted by cron before any of the job's own output...

This method will not work, unless the e-mail reader (incorrectly) acts upon 
parts of the body as if they were headers...

Thanks! Yours,

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Daniel Bye
Mikhail Teterin wrote:
 Hello!
 
 I have a script launched from cron every morning, that gets certain data over 
 the Internet from a remote computer, compares the new data with that from the 
 previous day, and outputs the difference (if any).
 
 I'm relying on the fact, that cron e-mails me the output of each job.
 
 However, I modified the script recently to produce the output (if any) in 
 HTML, rather than in plain-text format.
 
 The HTML arrives by e-mail just as well as plain text used to, but no e-mail 
 program will render it as such, because neither the cron(8), nor the mail(1),
 which cron uses to send e-mail, creates MIME messages...
 
 How can I force the ``Content-Type: text/html'' header without hacking cron's 
 sources? I'd rather avoid poluting my script with e-mail sending code...

Alter your script to add the 'Content-Type: text/html' header.

 Maybe, cron should apply file(1)-like logic to the e-mailed content?

No, cron doesn't need any knowledge of how to render email. Make sure
the output of your script includes the Content-Type header, which your
mail reader will spot and act upon accordingly. The script itself
doesn't have to send the mail - cron will handle that if there is any
output when it exits, but you /can/ add headers to the message as you need.

Just make sure any custom headers come before the empty line delimiter
between headers and body, and most mail readers should do the right thing.

HTH

Dan
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Daniel Bye
Mikhail Teterin wrote:
 = Alter your script to add the 'Content-Type: text/html' header.
 
 No, I'm afraid, doing this will make the quoted text part of the _body_ of 
 the 
 message.

Ack. Yes, you're quite right. Sorry for the bum advice.

 =  Maybe, cron should apply file(1)-like logic to the e-mailed content?
 
 = No, cron doesn't need any knowledge of how to render email.
 
 I was not advocating adding such knowledge. My suggestion was to make cron 
 add 
 proper Content-Type, so that the /recepient's e-mail program/ will render the 
 message correctly. My scripts generate HTML, someone else could be generating 
 JPG images (from their web-camera, every morning)...

Hmm, an interesting idea. Now I understand what you meant, that is.

 = The script itself doesn't have to send the mail - cron will handle that if
 = there is any output when it exits, but you /can/ add headers to the message
 = as you need.
 = 
 = Just make sure any custom headers come before the empty line delimiter
 = between headers and body, and most mail readers should do the right thing.
 
 The empty line is inserted by cron before any of the job's own output...
 
 This method will not work, unless the e-mail reader (incorrectly) acts upon 
 parts of the body as if they were headers...

Yep, absolutely right. Got my wrong brain on today!

So it's beginning to look as if your best bet is in fact to make your
script handle sending the mail, and to only have cron itself mail
anything that is the result of an error arising from your script. Not
the cleanest solution, but one that will get your messages formatted
exactly how you want them.

Cheers,

Dan

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Can cron e-mail HTML?

2007-07-14 Thread Mikhail Teterin
On субота 14 липень 2007, Daniel Bye wrote:
= So it's beginning to look as if your best bet is in fact to make your
= script handle sending the mail.

Yeah, seems like it...

= Not the cleanest solution, but one that will get your messages formatted
= exactly how you want them.

Well, I started looking into how much effort would it be to translate the 
strings returned by libmagic(3)'s routines into Content-Type.

If it is easy enough, I could hack cron to analyze the job's output using 
magic_buffer(3) and set Content-Type if anything recognizable is detected...

The translation is the difficult part :-( Instead of the standardized

text/html

for example, libmagic returns:

HTML document text

It is trying to be human-readable, while I need the machine-readable strings.

There is stuff on-line that does the translation, but it is in much 
higher-level languages (like PHP), which think, hash-tables are free :-)

Oh, well...

-mi
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]