Código PHP:
function CreateBody() {
$result = '';
if ($this->sign_key_file) {
$result .= $this->GetMailMIME();
}
$this->SetWordWrap();
switch($this->message_type) {
case 'alt':
$result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[1]);
break;
case 'plain':
$result .= $this->EncodeString($this->Body, $this->Encoding);
break;
case 'attachments':
$result .= $this->GetBoundary($this->boundary[1], '', '', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE;
$result .= $this->AttachAll();
break;
case 'alt_attachments':
$result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
$result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
$result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[2]);
$result .= $this->AttachAll();
break;
}
if($this->IsError()) {
$result = '';
} else if ($this->sign_key_file) {
$file = tempnam("", "mail");
$fp = fopen($file, "w");
fwrite($fp, $result);
fclose($fp);
$signed = tempnam("", "signed");
if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_key_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
$fp = fopen($signed, "r");
$result = fread($fp, filesize($this->sign_key_file));
fclose($fp);
} else {
$this->SetError($this->Lang("signing").openssl_error_string());
$result = '';
}
unlink($file);
unlink($signed);
}
return $result;
}
function GetBoundary($boundary, $charSet, $contentType, $encoding) {
$result = '';
if($charSet == '') {
$charSet = $this->CharSet;
}
if($contentType == '') {
$contentType = $this->ContentType;
}
if($encoding == '') {
$encoding = $this->Encoding;
}
$result .= $this->TextLine('--' . $boundary);
$result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
$result .= $this->LE;
$result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
$result .= $this->LE;
return $result;
}
function EndBoundary($boundary) {
return $this->LE . '--' . $boundary . '--' . $this->LE;
}
function SetMessageType() {
if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
$this->message_type = 'plain';
} else {
if(count($this->attachment) > 0) {
$this->message_type = 'attachments';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
$this->message_type = 'alt';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
$this->message_type = 'alt_attachments';
}
}
}
function HeaderLine($name, $value) {
return $name . ': ' . $value . $this->LE;
}
function TextLine($value) {
return $value . $this->LE;
}
function AddAttachment($path, $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;
}
$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; // isStringAttachment
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
return true;
}
function AttachAll() {
/* Return text of body */
$mime = array();
/* Add all attachments */
for($i = 0; $i < count($this->attachment); $i++) {
/* Check for string attachment */
$bString = $this->attachment[$i][5];
if ($bString) {
$string = $this->attachment[$i][0];
} else {
$path = $this->attachment[$i][0];
}
$filename = $this->attachment[$i][1];
$name = $this->attachment[$i][2];
$encoding = $this->attachment[$i][3];
$type = $this->attachment[$i][4];
$disposition = $this->attachment[$i][6];
$cid = $this->attachment[$i][7];
$mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
$mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
$mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
if($disposition == 'inline') {
$mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
}
$mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
/* Encode as string attachment */
if($bString) {
$mime[] = $this->EncodeString($string, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
} else {
$mime[] = $this->EncodeFile($path, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
}
}
$mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
return join('', $mime);
}
function EncodeFile ($path, $encoding = 'base64') {
if(!@$fd = fopen($path, 'rb')) {
$this->SetError($this->Lang('file_open') . $path);
return '';
}
$magic_quotes = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
$file_buffer = fread($fd, filesize($path));
$file_buffer = $this->EncodeString($file_buffer, $encoding);
fclose($fd);
set_magic_quotes_runtime($magic_quotes);
return $file_buffer;
}
function EncodeString ($str, $encoding = 'base64') {
$encoded = '';
switch(strtolower($encoding)) {
case 'base64':
/* chunk_split is found in PHP >= 3.0.6 */
$encoded = chunk_split(base64_encode($str), 76, $this->LE);
break;
case '7bit':
case '8bit':
$encoded = $this->FixEOL($str);
if (substr($encoded, -(strlen($this->LE))) != $this->LE)
$encoded .= $this->LE;
break;
case 'binary':
$encoded = $str;
break;
case 'quoted-printable':
$encoded = $this->EncodeQP($str);
break;
default:
$this->SetError($this->Lang('encoding') . $encoding);
break;
}
return $encoded;
}
function EncodeHeader ($str, $position = 'text') {
$x = 0;
switch (strtolower($position)) {
case 'phrase':
if (!preg_match('/[\200-\377]/', $str)) {
/* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
$encoded = addcslashes($str, "\0..\37\177\\\"");
if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
return ($encoded);
} else {
return ("\"$encoded\"");
}
}
$x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
break;
case 'comment':
$x = preg_match_all('/[()"]/', $str, $matches);
/* Fall-through */
case 'text':
default:
$x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
break;
}
if ($x == 0) {
return ($str);
}
$maxlen = 75 - 7 - strlen($this->CharSet);
/* Try to select the encoding which should produce the shortest output */
if (strlen($str)/3 < $x) {
$encoding = 'B';
if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
// Use a custom function which correctly encodes and wraps long
// multibyte strings without breaking lines within a character
$encoded = $this->Base64EncodeWrapMB($str);
} else {
$encoded = base64_encode($str);
$maxlen -= $maxlen % 4;
$encoded = trim(chunk_split($encoded, $maxlen, "\n"));
}
} else {
$encoding = 'Q';
$encoded = $this->EncodeQ($str, $position);
$encoded = $this->WrapText($encoded, $maxlen, true);
$encoded = str_replace('='.$this->LE, "\n", trim($encoded));
}
$encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
$encoded = trim(str_replace("\n", $this->LE, $encoded));
return $encoded;
}
function HasMultiBytes($str) {
if (function_exists('mb_strlen')) {
return (strlen($str) > mb_strlen($str, $this->CharSet));
} else { // Assume no multibytes (we can't handle without mbstring functions anyway)
return False;
}
}