<?
/** Mail Functions ********************************************
Send mails in UTF-8 enchoding with readable headers
@NOTE http://php.net/imap_8bit (2nd comment) - replacese PHP`s built-in 'imap_8bit'
@AUTHOR Tomix
@NOTE http://php.net/manual/en/function.mail.php (7th comment)
@AUTHOR umu
Comments stripped to reduce the code size. Refer to the origin links to see the raw sources.
Most of the script is copied one to one except few changes.
***************************************************************/
function quoted_printable_encode($sText,$bEmulate_imap_8bit=true) {
$aLines=explode(chr(13).chr(10),$sText);
for ($i=0;$i<count($aLines);$i++) {
$sLine =& $aLines[$i];
if (strlen($sLine)===0) continue; // do nothing, if empty
$sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
if ($bEmulate_imap_8bit)
$sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
$sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
$sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
{
$iLength = strlen($sLine);
$iLastChar = ord($sLine{$iLength-1});
if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
if (($iLastChar==0x09)||($iLastChar==0x20)) {
$sLine{$iLength-1}='=';
$sLine .= ($iLastChar==0x09)?'09':'20';
}
}
if ($bEmulate_imap_8bit) {
$sLine=str_replace(' =0D','=20=0D',$sLine);
}
preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
$sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); // add soft crlf's
}
return implode(chr(13).chr(10),$aLines);
}
function header_quoted_printable_encode($string, $encoding='UTF-8') {
$string = str_replace(" ", "_", trim($string)) ;
$string = str_replace("?", "=3F", str_replace("=\r\n", "", quoted_printable_encode($string))) ;
$string = chunk_split($string, 73);
$string = substr($string, 0, strlen($string)-2);
$string = str_replace("\r\n", "?=".HEAD_CRLF." =?".$encoding."?Q?", $string) ;
return '=?'.$encoding.'?Q?'.$string.'?=';
}
function imap8bit(&$item, $key) {
$item = header_quoted_printable_encode($item);
}
/**
@input params: email (string $to, string $subject, string $message [, string $additional_headers ] )
@returns boolean: result of <mail()> (true on success, false on fail)
**/
function email($e_mail, $subject, $message, $headers="") {
if ( strlen($headers) )
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$subject = wordwrap($subject, 25, "\n", FALSE);
$subject = explode("\n", $subject);
array_walk($subject, imap8bit);
$subject = implode("\r\n ", $subject);
$subject = $subject;
$message = quoted_printable_encode($message);
return(mail("$e_mail", "$subject", "$message", "$headers"));
}
/*
Test how it works
*/
/*
$result = email (
"user@mail.example",
"Кирилица i latinica",
"Тест\nTest\nTest тест."//,
//$more_headers
);
*/
?>