Código PHP:
function Base64EncodeWrapMB($str) {
$start = "=?".$this->CharSet."?B?";
$end = "?=";
$encoded = "";
$mb_length = mb_strlen($str, $this->CharSet);
// Each line must have length <= 75, including $start and $end
$length = 75 - strlen($start) - strlen($end);
// Average multi-byte ratio
$ratio = $mb_length / strlen($str);
// Base64 has a 4:3 ratio
$offset = $avgLength = floor($length * $ratio * .75);
for ($i = 0; $i < $mb_length; $i += $offset) {
$lookBack = 0;
do {
$offset = $avgLength - $lookBack;
$chunk = mb_substr($str, $i, $offset, $this->CharSet);
$chunk = base64_encode($chunk);
$lookBack++;
}
while (strlen($chunk) > $length);
$encoded .= $chunk . $this->LE;
}
// Chomp the last linefeed
$encoded = substr($encoded, 0, -strlen($this->LE));
return $encoded;
}
function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split('/(?:\r\n|\r|\n)/', $input);
$eol = "\r\n";
$escape = '=';
$output = '';
while( list(, $line) = each($lines) ) {
$linlen = strlen($line);
$newline = '';
for($i = 0; $i < $linlen; $i++) {
$c = substr( $line, $i, 1 );
$dec = ord( $c );
if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
$c = '=2E';
}
if ( $dec == 32 ) {
if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
$c = '=20';
} else if ( $space_conv ) {
$c = '=20';
}
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16);
$h1 = floor($dec%16);
$c = $escape.$hex[$h2].$hex[$h1];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = '';
// check if newline first character will be point or not
if ( $dec == 46 ) {
$c = '=2E';
}
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
} // end of while
return trim($output);
}
function EncodeQ ($str, $position = 'text') {
/* There should not be any EOL in the string */
$encoded = preg_replace("[\r\n]", '', $str);
switch (strtolower($position)) {
case 'phrase':
$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
case 'comment':
$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
case 'text':
default:
/* Replace every high ascii, control =, ? and _ characters */
$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
"'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
}
/* Replace every spaces to _ (more readable than =20) */
$encoded = str_replace(' ', '_', $encoded);
return $encoded;
}
function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $string;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $filename;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = true; // isString
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
}
function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
if(!@is_file($path)) {
$this->SetError($this->Lang('file_access') . $path);
return false;
}
$filename = basename($path);
if($name == '') {
$name = $filename;
}
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = false;
$this->attachment[$cur][6] = 'inline';
$this->attachment[$cur][7] = $cid;
return true;
}
function InlineImageExists() {
$result = false;
for($i = 0; $i < count($this->attachment); $i++) {
if($this->attachment[$i][6] == 'inline') {
$result = true;
break;
}
}
return $result;
}
function ClearAddresses() {
$this->to = array();
}
function ClearCCs() {
$this->cc = array();
}
function ClearBCCs() {
$this->bcc = array();
}
function ClearReplyTos() {
$this->ReplyTo = array();
}
function ClearAllRecipients() {
$this->to = array();
$this->cc = array();
$this->bcc = array();
}
function ClearAttachments() {
$this->attachment = array();
}
function ClearCustomHeaders() {
$this->CustomHeader = array();
}
function SetError($msg) {
$this->error_count++;
$this->ErrorInfo = $msg;
}
function RFCDate() {
$tz = date('Z');
$tzs = ($tz < 0) ? '-' : '+';
$tz = abs($tz);
$tz = (int)($tz/3600)*100 + ($tz%3600)/60;
$result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
return $result;
}
function ServerVar($varName) {
global $HTTP_SERVER_VARS;
global $HTTP_ENV_VARS;
if(!isset($_SERVER)) {
$_SERVER = $HTTP_SERVER_VARS;
if(!isset($_SERVER['REMOTE_ADDR'])) {
$_SERVER = $HTTP_ENV_VARS; // must be Apache
}
}
if(isset($_SERVER[$varName])) {
return $_SERVER[$varName];
} else {
return '';
}
}
function ServerHostname() {
if ($this->Hostname != '') {
$result = $this->Hostname;
} elseif ($this->ServerVar('SERVER_NAME') != '') {
$result = $this->ServerVar('SERVER_NAME');
} else {
$result = 'localhost.localdomain';
}
return $result;
}
function Lang($key) {
if(count($this->language) < 1) {
$this->SetLanguage('en'); // set the default language
}
if(isset($this->language[$key])) {
return $this->language[$key];
} else {
return 'Language string failed to load: ' . $key;
}
}
function IsError() {
return ($this->error_count > 0);
}
function FixEOL($str) {
$str = str_replace("\r\n", "\n", $str);
$str = str_replace("\r", "\n", $str);
$str = str_replace("\n", $this->LE, $str);
return $str;
}
function AddCustomHeader($custom_header) {
$this->CustomHeader[] = explode(':', $custom_header, 2);
}