Attachments via SOAP

English! place to talk about development, programming and coding
Post Reply
shihan
Znuny newbie
Posts: 4
Joined: 22 Oct 2010, 17:20
Znuny Version: 2.4

Attachments via SOAP

Post by shihan »

Ich suche mittlerweile Ewigkeiten nach einer Lösung um Attachments via SOAP zu übertragen. Prinzipiell ist es möglich Attachments zu übertragen, leider aber nur als Plaintext. Sobald ich versuche Binary-Data zu übertragen meckert SOAP bzgl. des Encodings.
Übertrage ich die Attachments als Base64-codierte Daten, funktioniert zwar die Übertragung, aber OTRS decodiert diese nicht wieder - die Datei ist also nicht lesbar.

Hat jemanden vllt. einen Code Schnipsel mit dem man das ändern könnte? Oder gibt es einen anderen Weg Attachments zu Tickets hinzuzufügen?
Leider beherrsche ich selbst kaum PERL so dass ich mir nicht selbst schnell einen Hack schreiben könnte.

Vielen Dank für Anregungen und Hilfe.
francescod
Znuny newbie
Posts: 7
Joined: 13 Nov 2010, 14:41
Znuny Version: 2

Re: Attachments via SOAP

Post by francescod »

Hi,
have you find the solution? I have the sam problem.

Thank you
Francesco
Andre Bauer
Znuny guru
Posts: 2189
Joined: 08 Dec 2005, 17:01
Znuny Version: 5.0.x
Real Name: André Bauer
Company: Magix Software GmbH
Location: Dresden
Contact:

Re: Attachments via SOAP

Post by Andre Bauer »

Maybe it would help if you post your code...
Prod: Ubuntu Server 16.04 / Zammad 1.2

DO NOT PM ME WITH OTRS RELATED QUESTIONS! ASK IN THE FORUMS!

OtterHub.org
francescod
Znuny newbie
Posts: 7
Joined: 13 Nov 2010, 14:41
Znuny Version: 2

Re: Attachments via SOAP

Post by francescod »

I don't have a code because I don't know how to do it. Have you got any idea?

Thank you
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP

Post by horst »

I also have the same issue with Attachments like pdf/jpg/xls etc.
My php code works ony for html/txt files.

Code: Select all

Code:
$myFile = "c:\\contact.htm";

$fh = fopen($myFile,'rb') or die($php_errormsg);
$theData = fread($fh,filesize($myFile));

fclose($fh) or die($php_errormsg);

$articleattachment = $soapclient->__soapCall("Dispatch", array($user, $pass, "TicketObject", "ArticleWriteAttachment",
        "Content", $theData,
        "ContentType", "text/html",
        "Filename", $myFile,
     #   "ContentID          => 'cid-1234', # optional
     #   ContentAlternative => 0,          # optional, alternative content to shown as body
        "ArticleID", $articleid,
        "UserID",$userid
    ));
Error code:

Fatal error: SOAP-ERROR: Encoding: string '%PDF-1.5 %\xb5...' is not a valid utf-8 string in

Have you got any idea?

Thank you
francescod
Znuny newbie
Posts: 7
Joined: 13 Nov 2010, 14:41
Znuny Version: 2

Re: Attachments via SOAP

Post by francescod »

Hello,
I had the same problem and I had just arrived at a possible solution. But I had not more time, so I had to find an alternative.
Through the web services I create the ticket and the article. Then I transfer the files via ftp in the folder reference. OTRS, if set to save the attachment to disk, when you create a ticket and the article in automatic it create on the disk a path like this: <year> / <month> / <day> / <articleid returned by ws> where when you put the file via ftp it automatically makes you see it in the web interface. In addition you must put a file called <articleid>. <file extension>. content_type which contains a line describing the content type. Sample Content: application/pdf. I eventually solved the case. You can do the same thing even if you save the file in the db. Instead of loading the file via ftp load it in the db.
I hope I was clear.
Bye
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP

Post by horst »

Hi,
thank you! That was a good help and it creates the Ticket and the Attachment.

My problem now is that I also want to send the attachments together with the the article.

The code in the SOAP API ArticleSend includes the Attachment:

Code: Select all

my $ArticleID = $TicketObject->ArticleSend(
        TicketID    => 123,
        ArticleType => 'note-internal',                                        # email-external|email-internal|phone|fax|...
        SenderType  => 'agent',                                                # agent|system|customer
        From        => 'Some Agent <email@example.com>',                       # not required but useful
        To          => 'Some Customer A <customer-a@example.com>',             # not required but useful
        Cc          => 'Some Customer B <customer-b@example.com>',             # not required but useful
        ReplyTo     => 'Some Customer B <customer-b@example.com>',             # not required
        Subject     => 'some short description',                               # required
        Body        => 'the message text',                                     # required
        InReplyTo   => '<asdasdasd.12@example.com>',                           # not required but useful
        References  => '<asdasdasd.1@example.com> <asdasdasd.12@example.com>', # not required but useful
        Charset     => 'iso-8859-15'
        MimeType    => 'text/plain',
        Loop        => 0, # 1|0 used for bulk emails
        Attachment => [
            {
                Content     => $Content,
                ContentType => $ContentType,
                Filename    => 'lala.txt',
            },
            {
                Content     => $Content,
                ContentType => $ContentType,
                Filename    => 'lala1.txt',
            },
        ],
        Sign => {
            Type    => 'PGP',
            SubType => 'Inline|Detached',
            Key     => '81877F5E',
            Type    => 'SMIME',
            Key     => '3b630c80',
        },
        Crypt => {
            Type    => 'PGP',
            SubType => 'Inline|Detached',
            Key     => '81877F5E',
            Type    => 'SMIME',
            Key     => '3b630c80',
        },
        HistoryType    => 'OwnerUpdate',  # Move|AddNote|PriorityUpdate|WebRequestCustomer|...
        HistoryComment => 'Some free text!',
        NoAgentNotify  => 0,            # if you don't want to send agent notifications
        UserID         => 123,
    );
How can you send the Article together with the Attachments when they are stored in the FS system? How did you solve this?
shihan
Znuny newbie
Posts: 4
Joined: 22 Oct 2010, 17:20
Znuny Version: 2.4

Re: Attachments via SOAP

Post by shihan »

Some time ago I found a solution for that problem. I'm not a PERL programmer, but it worked for me and solved my problem since then.
It only works, if you specify the new parameter "IsBase64Encoded" as I did not find a solution for recognizing base64 encoded data.

Code: Select all

root@www:~/otrs-2.4.7/Kernel# diff -r ./System/Ticket/Article.pm /usr/share/otrs/Kernel/System/Ticket/Article.pm
260a261,272
>         if ( $Param{Attachment} ) {
> 
>                       use MIME::Base64;
> 
>             for my $Attachment ( @{ $Param{Attachment} } ) {
>                 if (defined $Attachment->{IsBase64Encoded} ) {
>                               $Self->{LogObject}->Log( Priority => 'error', Message  => "base64 decoding $Attachment->{Filename}");
>                     $Attachment->{Content} = decode_base64($Attachment->{Content});
>                 }
>             }
>         }
> 
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP

Post by horst »

Thank you Francesco and Shihan, I had made some progress, but I still have the problem to use ArticleSend() with Attachments like .jpg.
- I changed the Attachment from the DB to FS and I am able to create Attachments on the fly with the ftp, php code, but I was not able to send this attachments with ArticleSend so I was trying to do it with the changes in the Article.pm
- I did the change in the Article.pm and I added before ( # add converted attachments) the addtional lines, but when I use binary attachments it still comes the same SOAP Error Message:

Fatal error: SOAP-ERROR: Encoding: string '\xff...' is not a valid utf-8 string in

Here the code I using:

Code: Select all

#$myFile = "contact.htm";          # HTML FILE WORKS 
#$contenttype = "text/plain";     # HTML FILE WORKS 

$myFile = "CristmasCard.jpg";         #JPG FILE DOESN'T WORKS 
$contenttype = "IsBase64Encoded"; #JPG FILE DOESN'T WORKS 


$mydirFile = $mydir . $myFile;

$fh = fopen($mydirFile,'rb') or die($php_errormsg);
$theData = fread($fh,filesize($mydirFile));

fclose($fh) or die($php_errormsg);

$articleid = $soapclient->__soapCall("Dispatch", array($user, $pass, "TicketObject", "ArticleSend",
				    "TicketID"         , $ticketid,
			        "ArticleType"      , "email-external",
			        "SenderType"      , "agent",
			        "From"             , $from,
			        "To"               , $to,
			        "ReplyTo"          , "",
			        "Subject"          , "[ Ticket#: ".$ticketnr." ] ".$subject,
			        "Body"             , $message,
			        "ContentType"      , "text/plain; charset=utf-8",
			        "Charset"          , "utf-8",
			        "HistoryType"      , "EmailCustomer",
			        "HistoryComment"   , "generated by OTRSInterface-Class",
			        "NoAgentNotify"    , 0,
			        "MimeType"             , "text/html",
			        "Loop"             , 0 ,
			        "Attachment"       , array(
                    array('Content' => $theData,'ContentType' => $contenttype,'Filename' => $myFile),
#                    array('Content' => $theData,'ContentType' => $contenttype,'Filename' => $myFile)
                     ),
                     "UserID"           , $userid
			    	));

There is no additional log in the OTRS log file..

Again the only thing I want to do is sending from OTRS a Ticket with attachments like jpeg, pdf, etc.

Changes on Ticket.pm:
Should I do also something on the php code to encode the Attachments? Did you also pass the File with fread() to the variable or you did it in a different way?

Do you know another workaround to send ticket with binary attachments?
shihan
Znuny newbie
Posts: 4
Joined: 22 Oct 2010, 17:20
Znuny Version: 2.4

Re: Attachments via SOAP

Post by shihan »

Sending binary attachments works. You have to specifiy the paramter IsBase64Encoded for
every attachment and you have to encode the attachments. Yo did not do any of these things.
francescod
Znuny newbie
Posts: 7
Joined: 13 Nov 2010, 14:41
Znuny Version: 2

Re: Attachments via SOAP

Post by francescod »

In my case, when I called call ArticleSend I not send the information about attachment. As I said, when you create a ticket object and article OTRS automatically creates the folder on disk where you can put the files. The only thing I forgot to tell you is that you must pass the following parameters as follows
"ContentType", "text / html; charset = utf-8"
"Body", "<html> <head> <meta http-equiv = \" Content-Type \ "content = \"text / html; charset = utf-8 \ "</ head> <body> </ body> </ html> "

I am attaching my sample php. There is no part of the FTP connection
I hope I was clear,
Bye
Francesco
You do not have the required permissions to view the files attached to this post.
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP

Post by horst »

Hi Shihan,
- I said I have modified the Ticket.pm but this is not correct, I modified the Article.pm and I added the addtional line to this.
- Also I added the $theData = base64_encode($theData); in the php code to use the base64 encoding.
shihan wrote:Sending binary attachments works. You have to specifiy the paramter IsBase64Encoded for
every attachment and you have to encode the attachments. Yo did not do any of these things.
After the change in the php code the sending of the attachments works. (This is what you said)
So but the decoding doesn't works. I am using OTRS 3.0.4, perhaps there are changes in the Article.pm

Could you please send me your complete Article.pm

Thank you
Horst
shihan
Znuny newbie
Posts: 4
Joined: 22 Oct 2010, 17:20
Znuny Version: 2.4

Re: Attachments via SOAP

Post by shihan »

Here's the complete file. Search for "64". It is Version 2.4.7 of OTRS.

http://nopaste.php-quake.net/387428
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP [SOLVED]

Post by horst »

Hi Shihan,
finaly now it works, I had to modify a little bit your perl code, because it did not worked with OTRS 3.0.4
Attached the code, perhaps somebody runs in the same issues:

Code: Select all

    # add attachments
    if ( $Param{Attachment} ) {
        if ( $Param{Attachment} ) {
 
                        use MIME::Base64;
		    for my $Attachment ( @{ $Param{Attachment} } ) {
				my $tmpcontenttype = $Attachment->{ContentType};

                if ($tmpcontenttype eq "IsBase64Encoded" ) {
                                $Self->{LogObject}->Log( Priority => 'error', Message  => "base64 decoding $Attachment->{Filename}");
                    $Attachment->{Content} = decode_base64($Attachment->{Content});
                }
            }
        }

        for my $Attachment ( @{ $Param{Attachment} } ) {
            $Self->ArticleWriteAttachment(
                %{$Attachment},
                ArticleID => $ArticleID,
                UserID    => $Param{UserID},
            );
        }
    }
Thank you again Shihan and francescod for the really fast response!
Brassmann
Znuny newbie
Posts: 3
Joined: 08 Feb 2012, 12:34
Znuny Version: 3.0.2

Re: Attachments via SOAP

Post by Brassmann »

Hello,
if have nearly the same file like francescods sample php. And i have added
horst wrote:Hi Shihan,
finaly now it works, I had to modify a little bit your perl code, because it did not worked with OTRS 3.0.4
Attached the code, perhaps somebody runs in the same issues:

Code: Select all

 # add attachments
if ( $Param{Attachment} ) {
if ( $Param{Attachment} ) {

use MIME::Base64;
for my $Attachment ( @{ $Param{Attachment} } ) {
my $tmpcontenttype = $Attachment->{ContentType};

if ($tmpcontenttype eq "IsBase64Encoded" ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "base64 decoding $Attachment->{Filename}");
$Attachment->{Content} = decode_base64($Attachment->{Content});
}
}
}

for my $Attachment ( @{ $Param{Attachment} } ) {
$Self->ArticleWriteAttachment(
%{$Attachment},
ArticleID => $ArticleID,
UserID => $Param{UserID},
);
}
}
Thank you again Shihan and francescod for the really fast response!
this to my Article.pm. But really don't know what to add to the php/perl file? Can you attache a sample with the attachment function? Or jut tell what i have to add, to send attachments via soap with the ticket?
horst
Znuny newbie
Posts: 6
Joined: 12 Jan 2011, 21:12
Znuny Version: 3.0.4

Re: Attachments via SOAP

Post by horst »

Hello,
please you this code:

Code: Select all

#$myFile = "contact.htm";          # HTML FILE WORKS 
#$contenttype = "text/plain";     # HTML FILE WORKS 

$myFile = "CristmasCard.jpg";         #JPG FILE DOESN'T WORKS 
$contenttype = "IsBase64Encoded"; #JPG FILE DOESN'T WORKS 


$mydirFile = $mydir . $myFile;

$fh = fopen($mydirFile,'rb') or die($php_errormsg);
$theData = fread($fh,filesize($mydirFile));

fclose($fh) or die($php_errormsg);

$articleid = $soapclient->__soapCall("Dispatch", array($user, $pass, "TicketObject", "ArticleSend",
                "TicketID"         , $ticketid,
                 "ArticleType"      , "email-external",
                 "SenderType"      , "agent",
                 "From"             , $from,
                 "To"               , $to,
                 "ReplyTo"          , "",
                 "Subject"          , "[ Ticket#: ".$ticketnr." ] ".$subject,
                 "Body"             , $message,
                 "ContentType"      , "text/plain; charset=utf-8",
                 "Charset"          , "utf-8",
                 "HistoryType"      , "EmailCustomer",
                 "HistoryComment"   , "generated by OTRSInterface-Class",
                 "NoAgentNotify"    , 0,
                 "MimeType"             , "text/html",
                 "Loop"             , 0 ,
                 "Attachment"       , array(
                    array('Content' => $theData,'ContentType' => $contenttype,'Filename' => $myFile),
#                    array('Content' => $theData,'ContentType' => $contenttype,'Filename' => $myFile)
                     ),
                     "UserID"           , $userid
                ));
Important is this: $contenttype = "IsBase64Encoded" to use the decode_base64 function

Brassmann wrote:Hello,
if have nearly the same file like francescods sample php. And i have added
horst wrote:Hi Shihan,
finaly now it works, I had to modify a little bit your perl code, because it did not worked with OTRS 3.0.4
Attached the code, perhaps somebody runs in the same issues:

Code: Select all

 # add attachments
if ( $Param{Attachment} ) {
if ( $Param{Attachment} ) {

use MIME::Base64;
for my $Attachment ( @{ $Param{Attachment} } ) {
my $tmpcontenttype = $Attachment->{ContentType};

if ($tmpcontenttype eq "IsBase64Encoded" ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "base64 decoding $Attachment->{Filename}");
$Attachment->{Content} = decode_base64($Attachment->{Content});
}
}
}

for my $Attachment ( @{ $Param{Attachment} } ) {
$Self->ArticleWriteAttachment(
%{$Attachment},
ArticleID => $ArticleID,
UserID => $Param{UserID},
);
}
}
Thank you again Shihan and francescod for the really fast response!
this to my Article.pm. But really don't know what to add to the php/perl file? Can you attache a sample with the attachment function? Or jut tell what i have to add, to send attachments via soap with the ticket?
Brassmann
Znuny newbie
Posts: 3
Joined: 08 Feb 2012, 12:34
Znuny Version: 3.0.2

Re: Attachments via SOAP

Post by Brassmann »

Oh, can you start a little bit further? I've never worked with files before. What I have to do to get a file to work with like that?
djigger
Znuny newbie
Posts: 1
Joined: 10 Feb 2012, 09:50
Znuny Version: 3.0.9
Real Name: Dmitry

Re: Attachments via SOAP

Post by djigger »

Hi!
I solved this problem.
$soapclient->__soapCall("Dispatch", array(
$username, $password,
"TicketObject", "ArticleWriteAttachment",
"Content", new SoapVar(file_get_contents($attachment['tmp_name']),XSD_BASE64BINARY, 'xsd:base64Binary'),
"ContentType", $attachment['type'],
"Filename", $attachment['name'],
"ArticleID", $articleId,
"UserID", 1
);
Magic here new SoapVar(file_get_contents($attachment['tmp_name']),XSD_BASE64BINARY, 'xsd:base64Binary').

It's work for me.
Good luck)
onsitewellness
Znuny newbie
Posts: 1
Joined: 15 Feb 2012, 00:42
Znuny Version: 30000
Real Name: Donna
Company: Activation LLC
Contact:

Re: Attachments via SOAP

Post by onsitewellness »

I will try all your suggestions guys. I was actually planning to do some thing like this. Thanks.
Brassmann
Znuny newbie
Posts: 3
Joined: 08 Feb 2012, 12:34
Znuny Version: 3.0.2

Re: Attachments via SOAP

Post by Brassmann »

Thank You very much. It got it working :D

Here what i put in my form-script:

...
<form action="<?php print $_SERVER["PHP_SELF"]; ?>" method="post" accept-charset="UTF8" enctype="multipart/form-data">
...

<label for='uploaded_file'>Select a file to upload:</label>
<input type="file" name="uploaded_file">

...

$client->__soapCall("Dispatch", array(
$username,
$password,
"TicketObject", "ArticleWriteAttachment",
"Content", new SoapVar(file_get_contents($_FILES["uploaded_file"]['tmp_name']),XSD_BASE64BINARY, 'xsd:base64Binary'),
"ContentType", $_FILES["uploaded_file"]['type'],
"Filename", $_FILES["uploaded_file"]['name'],
"ArticleID", $ArticleID,
"UserID", 1
));



Maybe there are another people don't knowing how to get the file in the form.

Greetings
Brassmann
denbagusjkt
Znuny newbie
Posts: 23
Joined: 07 Jun 2012, 09:20
Znuny Version: 3.1.5
Real Name: denbagus
Company: indolabsoft

Re: Attachments via SOAP

Post by denbagusjkt »

still get error when try adding attachment via soap.
...
<form action="<?php print $_SERVER["PHP_SELF"]; ?>" method="post" accept-charset="UTF8" enctype="multipart/form-data">
...

<label for='uploaded_file'>Select a file to upload:</label>
<input type="file" name="uploaded_file">

...

$client->__soapCall("Dispatch", array(
$username,
$password,
"TicketObject", "ArticleWriteAttachment",
"Content", new SoapVar(file_get_contents($_FILES["uploaded_file"]['tmp_name']),XSD_BASE64BINARY, 'xsd:base64Binary'),
"ContentType", $_FILES["uploaded_file"]['type'],
"Filename", $_FILES["uploaded_file"]['name'],
"ArticleID", $ArticleID,
"UserID", 1
));
i want to add attachment jpg file from file system, not uploaded form processing. but i get this message

Code: Select all

Need Content!
on system log. and the attchment not appear on my otrs.
denbagusjkt
Znuny newbie
Posts: 23
Joined: 07 Jun 2012, 09:20
Znuny Version: 3.1.5
Real Name: denbagus
Company: indolabsoft

Re: Attachments via SOAP

Post by denbagusjkt »

i change this
$_FILES["uploaded_file"]['tmp_name']
to absolute location of my attachment file. and now its works perfectly.

thanks :) :) :)
chrisadams
Znuny newbie
Posts: 2
Joined: 12 Jan 2013, 11:32
Znuny Version: 3.0.10
Real Name: chris adams
Company: Abdo
Location: Muscat, Oman
Contact:

Re: Attachments via SOAP

Post by chrisadams »

Actually SOAP applications often have to deal with more than just simple messages. The payload for a SOAP message can often include a word processing or PDF document, image, or other binary file. I hope, this could help you in dealing with errors.
Post Reply