I am trying to automate process of encrypting a file and sending it through email. Where I have got success for below things:
I am able to encrypt file, and I am able to send file as attachment.
But I am facing a problem with file that is being received is not exactly as the file I sent. When I compared original encrypted file and received file from email, I found out that the received file truncates all data after it encounters “0x00” in file. When I tried to encode encrypted file with base64, I am able to received data in Ascii form, but I can’t get the original formatted file.
Can you help me for this problem?
How do I avoid truncation of data after encounter of 00 in file while attaching it?
What content-type should I give when I encode file with base64? As content type Base64 is not allowed in firewall while downloading the file.
If you can help me with this problem as soon as possible, I would really be greatful to you.
I AM USING MIME.CLASS FOR CODE THAT I FOUND ON INTERNET
CODE
/* ---------------------------------------------------------
Attach a 'file' to e-mail message
Pass a file name to attach.
This function returns a success/failure code/key of current
attachment in array (+1). Read attach() below.
--------------------------------------------------------- */
function fattach($path, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
$this->errstr = "";
if (!file_exists($path)) {
$this->errstr = "File does not exist";
return 0;
}
// Read in file
$fp = fopen($path, "rb"); # (b)inary for Win compatability
if (!$fp) {
$this->errstr = "fopen() failed";
return 0; //failed
}
$contenttype .= ";\r\n\tname=".basename($path).";\r\n";
//if(filesize($path))
while(!feof($fp))
$data .= fread($fp, 2048);
//$data=readfile($path);
echo "<BR>".$data."<BR>";
return $this->attach($data,
$description,
$contenttype,
$encoding,
$disp);
}
/* ---------------------------------------------------------
Attach data provided by user (rather than a file)
Useful when you want to MIME encode user input
like HTML. NOTE: This function returns key at which the requested
data is attached. IT IS CURRENT KEY VALUE + 1!!
Construct the body with MIME parts
--------------------------------------------------------- */
function attach($data, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
$this->errstr = "";
if (empty($data)) {
$this->errstr = "No data to be attached";
return 0;
}
if (trim($contenttype) == '') $contenttype = OCTET;
if (trim($encoding) == '') $encoding = BASE64;
if ($encoding == BIT7)
{$emsg = $data;}
elseif ($encoding == QP)
{$emsg = $$this->qp_func($data);}
elseif ($encoding == BASE64) {
if (!$this->base64_func) # Check if there is user-defined function
{
//$emsg = base64_encode($data);
$emsg = chunk_split(base64_encode($data));
}
else
{$emsg = $this->base64_func($data);}
}
//Check if content-type is text/plain and if charset is not specified append default CHARSET
if (preg_match("!^".TEXT."!i", $contenttype) && !preg_match("!;charset=!i", $contenttype))
$contenttype .= ";\r\n\tcharset=".CHARSET;
$msg = sprintf("Content-Type: %s Content-Transfer-Encoding: %s%s%s%s",
$contenttype.CRLF,
$encoding.CRLF,
((($description) && (BODY != $description))?"Content-Description: $description".CRLF:""),
($disp?"Content-Disposition: $disp".CRLF:""),
CRLF.$emsg.CRLF);
BODY==$description? $this->mimeparts[0] = $msg: $this->mimeparts[] = $msg;
return sizeof($this->mimeparts);
}
[mod edit]What is is with people and their adversion to using code tags?!