cron job sending emails in a wrong charset
I have following command in a cronjob:
*/5 * * * * php /var/www/domain/yii rss/parse
It outputs email in a wrong charset:
Content-Type: text/plain; charset=ANSI_X3.4-1968
But when i launch that command directly in CLI and output it to a log:
php /var/www/domain/yii rss/parse > log
I get the right encoding - UTF-8
Already tried to set lang in /etc/environment:
LANG=en_US.UTF-8
Restarted the cron, but still it uses ANSI via CRON. Any ideas?
Solved my issue by adding in to a crontab:
crontab -e
At the top of file i wrote:
CONTENT_TYPE="text/plain; charset=utf-8"
Now all of my cron job email's are in UTF-8 Charset.
Referring to this problem I found in Debian Jessie in /etc/default/cron the following:
# Whether to read the system's default environment files (if present)
# If set to "yes", cron will set a proper mail charset from the
# locale information. If set to something other than 'yes', the default
# charset 'C' (canonical name: ANSI_X3.4-1968) will be used.
#
# This has no effect on tasks running under cron; their environment can
# only be changed via PAM or from within the crontab; see crontab(5).
READ_ENV="yes"
In other words the default is that in this distribution it reads the environment files. After rerunning dpkg-reconfigure locales
(in my case the default was already set to UTF8) I looked at /etc/environment and discovered that it was empty. Once I inserted LC_ALL=en_US.UTF-8
there, the cron job emails were with correct character set header.
When you give the command by CLI you got the utf-8 chatset, i think, because you using a MAC OS PC or Linux PC
I told this because the current LANG of your PC begin copied in your ssh session
grep -i LANG /etc/ssh/sshd_config
AcceptEnv LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
from man sshd_config
AcceptEnv
Specifies what environment variables sent by the client will be copied into the sessionâs environ(7). See SendEnv in ssh_config(5) for how to
configure the client. Note that environment passing is only supported for protocol 2. Variables are specified by name, which may contain the
wildcard characters â*â and â?â. Multiple environment variables may be separated by whitespace or spread across multiple AcceptEnv directives.
Be warned that some environment variables could be used to bypass restricted user environments. For this reason, care should be taken in the
use of this directive. The default is not to accept any environment variables.
Your crond process are using charset=ANSI_X3.4-1968, maybe this is the default system LANG, but if want to change this
man 5 crontab
I had to solve this issue globally for all users and not specific one. I've tried to set up /etc/environment and /etc/default/locale then restart cron. This didn't help. Right answer for me was using env command in upstart script(i am running ubuntu server):
env LC_ALL=en_US.UTF-8
cat /etc/init/cron.conf
# cron - regular background program processing daemon
#
# cron is a standard UNIX program that runs user-specified programs at
# periodic scheduled times
description "regular background program processing daemon"
start on runlevel [2345]
stop on runlevel [!2345]
expect fork
respawn
env LC_ALL=en_US.UTF-8
exec cron
Then i've restarted cron and got correct mail in utf-8.