Attachment in admin_ticket

Helpdesk for my helpdesk software

Moderator: mkoch227

Post Reply
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Attachment in admin_ticket

Post 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.
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post 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??
Klemen
Site Admin
Posts: 10142
Joined: Fri Feb 11, 2005 4:04 pm

Post 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'];
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post 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
Klemen
Site Admin
Posts: 10142
Joined: Fri Feb 11, 2005 4:04 pm

Post 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']))); 
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post 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.
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post 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?
Klemen
Site Admin
Posts: 10142
Joined: Fri Feb 11, 2005 4:04 pm

Post 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

}
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post 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.
Klemen
Site Admin
Posts: 10142
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

Well you need to remove the attachment code from your e-mail when there are no attachments.
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
ud2008
Posts: 24
Joined: Fri Mar 27, 2009 9:41 am

Post by ud2008 »

Any idea how to do this?
Post Reply