How to send special characters via mail from a shell script? How to send special characters via mail from a shell script? shell shell

How to send special characters via mail from a shell script?


My /usr/bin/mail is symlinked to /etc/alternatives/mail which is also symlinked to /usr/bin/bsd-mailx

I had to specify myself the encoding in the mail header. (The -S is not supported here.)

cat myutf8-file | mail -a "Content-Type: text/plain; charset=UTF-8" -s "My Subject" me@mail.com


You're right in assuming this is a charset issue. You need to set the appropriate environment variables to the beginning of your crontab.

Something like this should work:

LANG=en_US.UTF-8LC_CTYPE=en_US.UTF-8

Optionally use LC_ALL in place of LC_CTYPE.

Reference: http://opengroup.org/onlinepubs/007908799/xbd/envvar.html

Edit: The reason it displays fine when you run it in your shell is probably because the above env vars are set in your shell.

To verify, execute 'locale' in your shell, then compare to the output of a cronjob that runs the same command.

Re-Edit: Ok, so it's not an env var problem.

I am assuming you're using mailx, as it is the most common nowdays. It's manpage says:

The character set for outgoing messages is not necessarily the same as the one used on the terminal. If an outgoing text message contains characters not representable in US-ASCII, the character set being used must be declared within its header. Permissible values can be declared using the sendcharsets variable,

So, try and add the following arguments when calling mail:

-S sendcharsets=utf-8,iso-8859-1


i've written a bash function to send an email to recipients. The function send utf-8 encoded mails and work with utf-8 chars in subject and content by doing a base64 encode.

To send a plain text email:

send_email "plain" "from@domain.com" "subject" "contents" "to@domain.com" "to2@domain.com" "to3@domain.com" ...

To send a HTML email:

send_email "html" "from@domain.com" "subject" "contents" "to@domain.com" "to2@domain.com" "to3@domain.com" ...

Here is the function code.

# Send a email to recipients.## @param string $content_type Email content mime type: 'html' or 'plain'.# @param string $from_address Sender email.# @param string $subject Email subject.# @param string $contents Email contents.# @param array $recipients Email recipients.function send_email() {  [[ ${#} -lt 5 ]] && exit 1  local content_type="${1}"  local from_address="${2}"  local subject="${3}"  local contents="${4}"  # Remove all args but recipients.  shift 4  local encoded_contents="$(base64 <<< "${contents}")"  local encoded_subject="=?utf-8?B?$(base64 --wrap=0 <<< "${subject}")?="  for recipient in ${@}; do    if [[ -n "${recipient}" ]]; then    sendmail -f "${from_address}" "${recipient}" \        <<< "Subject: ${encoded_subject}MIME-Version: 1.0From: ${from_address}To: ${recipient}Content-Type: text/${content_type}; charset=\"utf-8\"Content-Transfer-Encoding: base64Content-Disposition: inline${encoded_contents}"    fi  done  return 0} # send_message()