Page 1 of 1

Attachment in admin_ticket

Posted: Fri May 08, 2009 7:16 am
by ud2008
Script URL: local
Version of script: 2.0
Hosting company:
URL of phpinfo.php:
URL of session_test.php:
What terms did you try when SEARCHING for a solution:

Write your message below:

I have a question and I know its out of the scope of support for klemen, hopefully someone can help me with it so here it is.

I've altered admin_ticket, by adding an other link to send an email with the info from the ticket to a certain person, everything works fine, but the attachment (if there is any added to the ticket) is not shown as it should, I get the name of the attachment but with some other characters with it so it is not as it should: (example)

The original attachment has the name: Q298169.pdf but when I had send it, it shows as 7#Q298169.pdf, and the size is 249B instead of 17 KB.

I know it as something to do with the att_id (and what comes more with it).

Here is the code I use to send it (part of it):

Code: Select all

//define the receiver of the email 
$to = 'blabla@domain.nl';
//define the subject of the email 
$subject = "Bestelling 2009-".$ticket['id']; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: blabla@domain.nl\r\nReply-To: blabla@domain.nl\r\nCC: ".$ticket[email].",".$ticket[bhemail].",blabla@domain.nl"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1"

--PHP-alt-<?php echo $random_hash; ?>--
 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="<?php echo $ticket[attachments]?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  
 
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>--
 
<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers );
Hope someone can help me or direct me into the right direction.

Thanks already.

Posted: Fri May 08, 2009 7:50 am
by ud2008
I've added this code:

Code: Select all

<?php
if ($hesk_settings['attachments']['use'] && !empty($ticket['attachments'])) {
echo '<p><b>'.$hesklang['attachments'].':</b><br />';
$att=explode(',',substr($ticket['attachments'], 0, -1));
foreach ($att as $myatt) {
list($att_id, $att_name) = explode('#', $myatt);
    }
}
?>
before:

Code: Select all

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="<?php echo $ticket[attachments]?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 
And changed:

Code: Select all

Content-Type: application/pdf; name="<?php echo $ticket[attachments]?>"
into:

Code: Select all

Content-Type: application/pdf; name="<?php echo $myatt?>"
Now I get the pdf icon (which didnt before because of the , after the pdf.
But still the 7# are before the name in the attachment, so when trying to open I get the message that the file is damaged or wrongly coded.

Any idea??

Posted: Fri May 08, 2009 7:50 am
by Klemen
You're not reading the attachment file here (the 249B is an empty file):

Code: Select all

$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments')));
It would require some more work, you have to find the real (saved) name of the file to read.

You can try something like this instead (didn't test it thought). It will only work if you have 1 attachment, for multiple ones you would need to build in a foreach loop.

Code: Select all

list($tmp_id,$tmp_name) = explode('#',$ticket['attachments']);
/* Get attachment info */
$sql = "SELECT * FROM `".$hesk_settings['db_pfix']."attachments` WHERE `att_id`=$tmp_id LIMIT 1";
$result = hesk_dbQuery($sql);
if (hesk_dbNumRows($result) != 1)
{
	hesk_error($hesklang['id_not_valid'].' (att_id)');
}
$file = hesk_dbFetchAssoc($result);

$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments'.$file['saved_name'])));
$ticket['attachments'] = $file['real_name'];

Posted: Fri May 08, 2009 8:07 am
by ud2008
Thanks for the reply, the real name is now displayed, but the size is still to small.
I believe its still wrongly decoded, the error says:

Q298169.pdf can not be opened because the filetype is not supported or the file is damaged (because its send as an attachment or it is wrongly decoded).

Thanks

Posted: Fri May 08, 2009 11:08 am
by Klemen
I don't think it's a decoding problem but rather that the file_get_contetns isn't getting the contents. Try using the code I gave you but change

Code: Select all

$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments'.$file['saved_name']))); 
to

Code: Select all

$attachment = chunk_split(base64_encode(file_get_contents($hesk_settings['server_path'].'/attachments/'.$file['saved_name']))); 

Posted: Fri May 08, 2009 11:14 am
by ud2008
thanks, but it still is a small size.

btw when I removed:

chunk_split(base64_encode

and

Content-Transfer-Encoding: base64

The size of the file is displayed correct but the error remains.

Posted: Fri May 08, 2009 2:17 pm
by ud2008
Klemen wrote:You're not reading the attachment file here (the 249B is an empty file):

Code: Select all

$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments')));
It would require some more work, you have to find the real (saved) name of the file to read.

You can try something like this instead (didn't test it thought). It will only work if you have 1 attachment, for multiple ones you would need to build in a foreach loop.

Code: Select all

list($tmp_id,$tmp_name) = explode('#',$ticket['attachments']);
/* Get attachment info */
$sql = "SELECT * FROM `".$hesk_settings['db_pfix']."attachments` WHERE `att_id`=$tmp_id LIMIT 1";
$result = hesk_dbQuery($sql);
if (hesk_dbNumRows($result) != 1)
{
	hesk_error($hesklang['id_not_valid'].' (att_id)');
}
$file = hesk_dbFetchAssoc($result);

$attachment = chunk_split(base64_encode(file_get_contents('http://intranet/bestellingendatabase2/attachments'.$file['saved_name'])));
$ticket['attachments'] = $file['real_name'];
i solved it but used a different code then I had, only I used the code you posted up (quoted).
The only question I have is, when there is no attachment with a ticket I get an error of not possible to run SQL.

How can I add a small code that when there is no attachment your code (quoted) is not run. Only when there is an attachment?

Posted: Fri May 08, 2009 5:07 pm
by Klemen
You can add this before the code I gave you:

Code: Select all

if (strlen(trim($ticket['attachments']))) 
{
and close it after my code with a

Code: Select all

}

Posted: Sat May 09, 2009 7:59 pm
by ud2008
Thanks for your reply.

I dont get an error anymore, but when a ticket doesnt have an attachment it shows an attachment (dat file) anyways.

When a ticket has an attachment it shows the attachment fine.

Posted: Sat May 09, 2009 8:15 pm
by Klemen
Well you need to remove the attachment code from your e-mail when there are no attachments.

Posted: Tue May 12, 2009 10:47 am
by ud2008
Any idea how to do this?