Cronjob emails and html

zerot

Verified User
Joined
Aug 24, 2013
Messages
10
Location
Serbia
Hi,

As you probably know, cronjob emails are sent in plain text format (Content-Type: text/plain; charset=UTF-8).
I wanted to change that to html (Content-Type: text/html; charset=UTF-8) without sending cron output to custom script for sending emails.

After little googling, I discovered that this can be achieved by adding this line to users crontab file:

Code:
CONTENT_TYPE="text/html; charset=utf-8"

And it works great. But, there is a problem: when I change crontab file through DA interface, crontab is re-created and manually made changes are gone (CONTENT_TYPE parameter). Then I have to go to crontab file and add CONTENT_TYPE again.

Is there some easy solution for this problem?
 
Are these cronjobs managed by DirectAdmin? I'm presuming the answer is that yes, they are.

If so, have you checked to see if DirectAdmin includes a template file for writing crontab jobs?

Jeff
 
Are these cronjobs managed by DirectAdmin?

Yes.


If so, have you checked to see if DirectAdmin includes a template file for writing crontab jobs?

Well, I was hoping that I will find something like this (that would be perfect, to simply add it to template), but I have no idea where to look for that.
 
Somewhere under the /usr/local/directadmin directory path you'll find a templates directory. Look for it :).

Jeff
 
Let's investigate a bit further; since you're the admin with the need, I'll let you do the work :).

How do cronjobs send email? Do they simply use the built-in system level method of sending cronjob output, or do the cronjobs include mail commands?

If the former you're generally out of luck unless you want to rewrite how the system sends cron-job output at the OS level.

If the individual cronjobs use some kind of code (perhaps the mail command), you can perhaps change see how the email is built, and change the process there.

If the cronjobs use some proprietary method of sending the meail you're going to need to ask DirectAdmin support for help.

Personally I don't like HTML emails, and I encourage my users to not read them (or at least not preview them in a mail client until they make a conscious decision to do so, because they're a major distribution method for tricking usres into downloading dangerous software onto their computer.s But that's just my opinion.

Jeff
 
Yes, cron use the built-in system level method of sending cronjob output, and it sends ugly email with ugly subject, but I am the only one who gets that email report from cron, so that doesn't matter. Only thing I need is HTML format of emails (not to be sent as plain text).

So, after a bit of googling, found a few answers "no, that is not possible, you need to use custom command/script for sending emails in HTML". After more and more googling, found this: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 , and here is important part:

By default, cron will send mail using the mail 'Content-Type:' header
of 'text/plain' with the 'charset=' parameter set to the charmap /
codeset of the locale in which crond(8) is started up - ie. either the
default system locale, if no LC_* environment variables are set, or the
locale specified by the LC_* environment variables (see locale(7)).
You can use different character encodings for mailed cron job output by
setting the CONTENT_TYPE and CONTENT_TRANSFER_ENCODING variables in
crontabs, to the correct values of the mail headers of those names.

So, when I add
Code:
CONTENT_TYPE="text/html; charset=utf-8"
to appropriate file in: /var/spool/cron folder, emails are sent in HTML format. Files in /var/spool/cron (they are crontab files, right?) are created by DirectAdmin, for example: when user (actually, there is no "users", I am only admin and user on that server) change existing or add new cronjob, file in /var/spool/cron is also changed and there appears new cronjob. And that overwrites that content_type code that I added previously.

So, I was thinking that there is some way to add this piece of code in DirectAdmin procedure for creating cronjobs. I get this idea because of this: nv2hax8.png. When I put email address here, It appears in crontab file like this: http://img9.imageshack.us/img9/3663/4m2b.png
And, it would be nice if there is another field where I could add this code:
Code:
CONTENT_TYPE="text/html; charset=utf-8"
, and It would be saved in crontab file (or to add this directly to procedure of creating crontab file). And, finaly, crontab would look like this: http://i.imgur.com/5DKjE6X.png

Let's say it short: ability to change content type of cronjob email reports (cronjob output).

But, now, after some thinking, I see that this probably isn't so easy thing to do (especially with my level of DA background programming knowledge). And I probably have too much free time :D
 
I want to setup a cron job (I think this is my best option) to automatically email me a table of open orders from my mySQL DB each morning.
 
Hello,

@zerot: Totally forgot about coding this, but it does exist:
http://www.directadmin.com/features.php?id=621

so in your case, you'd use:
Code:
SHELL=/bin/sh
MAILTO=|EMAIL|
CONTENT_TYPE="text/html; charset=utf-8"
|CRONS|

Not that it really matters, but DA does allow you to specify the email address:
http://www.directadmin.com/features.php?id=952
which doesn't look like the token makes it into the variables for the custom cron_template.txt.
So.. if you use the custom template, it will override the setting if anyone has it set.. but the default |EMAIL| token grabs the email= value from the user.conf, which might be sufficient.


Personally, I'd hide all output from the script being called (so no cron email is generated), and have the called script itself generate the email.
That way, you can make it send whatever you want with 100% control.


-----

@Freya45:
You'd want to code a php script to scan your database for that case, and use the mail() command in php to send you the email.
You wouldn't use the cron email in this case... it doesn't have as much control.

John
 
@DirectAdmin Support

That's it! :) That feature is just what I need. Thanks a lot.
 
Back
Top