note 27997 modified in function.mail by danbrown

From: Date: Mon, 20 Jun 2011 15:07:11 +0000
Subject: note 27997 modified in function.mail by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180710@lists.php.net to get a copy of this message
If your server doesn't have mb_send_mail() enabled but you want to use non-ascii (multi-byte) chars in an email's subject or name headers, you can use something like the following: <?php $charset = "iso-2202-jp"; // japanese $to = encode("japanese name 01", $charset) . " <to@example.com>"; $from = encode("japanese name 02", $charset) . " <from@example.com>"; $subject = encode("japanese text"); $message = "does not need to be encoded"; mail($to, $subject, $message, $from); function encode($in_str, $charset) { $out_str = $in_str; if ($out_str && $charset) { // define start delimimter, end delimiter and spacer $end = "?="; $start = "=?" . $charset . "?B?"; $spacer = $end . "\r\n " . $start; // determine length of encoded text within chunks // and ensure length is even $length = 75 - strlen($start) - strlen($end); /* [EDIT BY danbrown AT php DOT net: The following is a bugfix provided by (gardan AT gmx DOT de) on 31-MAR-2005 with the following note: "This means: $length should not be even, but divisible by 4. The reason is that in base64-encoding 3 8-bit-chars are represented by 4 6-bit-chars. These 4 chars must not be split between two encoded words, according to RFC-2047. */ $length = $length - ($length % 4); // encode the string and split it into chunks // with spacers after each chunk $out_str = base64_encode($out_str); $out_str = chunk_split($out_str, $length, $spacer); // remove trailing spacer and // add start and end delimiters $spacer = preg_quote($spacer); $out_str = preg_replace("/" . $spacer . "$/", "", $out_str); $out_str = $start . $out_str . $end; } return $out_str; } // for details on Message Header Extensions // for Non-ASCII Text see ... // http://www.faqs.org/rfcs/rfc2047.html ?> --was-- If your server doesn't have mb_send_mail() enabled but you want to use non-ascii (multi-byte) chars in an email's subject or name headers, you can use something like the following: <?php $charset = "iso-2202-jp"; // japanese $to = encode("japanese name 01", $charset) . " <to@email.com>"; $from = encode("japanese name 02", $charset) . " <from@email.com>"; $subject = encode("japanese text"); $message = "does not need to be encoded"; mail($to, $subject, $message, $from); function encode($in_str, $charset) { $out_str = $in_str; if ($out_str && $charset) { // define start delimimter, end delimiter and spacer $end = "?="; $start = "=?" . $charset . "?B?"; $spacer = $end . "\r\n " . $start; // determine length of encoded text within chunks // and ensure length is even $length = 75 - strlen($start) - strlen($end); /* [EDIT BY danbrown AT php DOT net: The following is a bugfix provided by (gardan AT gmx DOT de) on 31-MAR-2005 with the following note: "This means: $length should not be even, but divisible by 4. The reason is that in base64-encoding 3 8-bit-chars are represented by 4 6-bit-chars. These 4 chars must not be split between two encoded words, according to RFC-2047. */ $length = $length - ($length % 4); // encode the string and split it into chunks // with spacers after each chunk $out_str = base64_encode($out_str); $out_str = chunk_split($out_str, $length, $spacer); // remove trailing spacer and // add start and end delimiters $spacer = preg_quote($spacer); $out_str = preg_replace("/" . $spacer . "$/", "", $out_str); $out_str = $start . $out_str . $end; } return $out_str; } // for details on Message Header Extensions // for Non-ASCII Text see ... // http://www.faqs.org/rfcs/rfc2047.html ?> http://php.net/manual/en/function.mail.php

« previous php.notes (#180710) next »