Re: [PATCH] Use Content-ID when handling multipart message over REST.

Arkadiusz Miśkiewicz <[email protected]> Fri, 20 Feb 2015 11:58:06 +0100
Newsgroups gmane.comp.bug-tracking.request-tracker.devel
Message-ID <[email protected]>
On Friday 20 of February 2015, Arkadiusz Miśkiewicz wrote:
> From: Przemyslaw Plewa <[email protected]>
> 
> Record Content-ID identifier that came over REST API.
> 
> This allows us to have multipart messages with one (or more) part
> refering to other parts of message. Content-ID identifier is used for
> reference.
> 
> Such message also needs multipart/related type (instead of default (in
> rt) multipart/mixed. So if we detect Content-ID in any part of multipart
> message we switch Content-Type to multipart/related.

There is also pull request with it if that way is better

https://github.com/bestpractical/rt/pull/136

Also I'm attaching a test script to show how created message looks like (one 
mime part refers to second mime part (attached image) via content-id).

Please review and merge.

Thanks,
-- 
Arkadiusz Miśkiewicz, arekm / ( maven.pl | pld-linux.org )
test.php (application/x-php, 1.8 KB)
<?php 

$attachment_filename = 'logo.gif';
$rt_host = 'rt.example.com'; //RT hostname (eg. rt.example.com)
$rt_username = 'RT_USER';
$rt_password = 'RT_PASS';


$binarydata = file_get_contents($attachment_filename);
$file_info = new finfo(FILEINFO_MIME);
$_finfodata = $file_info->buffer(file_get_contents($attachment_filename));
$mime  = explode(';', $_finfodata);
$mimetype = $mime[0];

$cid = mktime().rand(0,10000)."@email";
$CRLF = "\r\n";
$boundary = '-------------'.md5(mktime());
$body = '';

$body .= "--$boundary".$CRLF;
$body .= "Content-Disposition: form-data; name=\"user\"".$CRLF.$CRLF;
$body .= $rt_username.$CRLF;;

$body .= "--$boundary".$CRLF;
$body .= "Content-Disposition: form-data; name=\"pass\"".$CRLF.$CRLF;
$body .= $rt_password.$CRLF;

$body .= "--$boundary".$CRLF;
$body .= "Content-Disposition: form-data; name=\"content\"".$CRLF.$CRLF;
$body .= "Subject: Test;
Text: <html><body><h3>This is HTML message</h3><img src=\"cid:$cid\"/><h3>End of HTML message</h3></body></html>
Queue: IT.ADMIN
Content-Type: text/html
Attachment: $attachment_filename".$CRLF;
$body .= "--$boundary".$CRLF;
$body .= "Content-Disposition: inline; name=\"attachment_1\"; filename=\"$attachment_filename\"".$CRLF;
$body .= "Content-ID: <$cid>".$CRLF;
$body .= "Content-Type: $mimetype".$CRLF.$CRLF;

$body .= "$binarydata".$CRLF;
$body .= "--$boundary--".$CRLF;

$requestlen = strlen($body);

$headers = "POST /REST/1.0/ticket/new HTTP/1.1".$CRLF;
$headers .= "Host: $rt_host".$CRLF;
$headers .= "Accept: */*".$CRLF;
$headers .= "Content-Length: $requestlen".$CRLF;
$headers .= "Content-type: multipart/form-data; boundary=$boundary".$CRLF.$CRLF;


$fp = fsockopen("$rt_host", 80, $errno, $errstr);
if ($fp)
{
 fwrite($fp, $headers . $body);
 $response = '';
 while (!feof($fp))
  $response .= fgets($fp, 1024);
 fclose($fp);
}

var_dump($response);
?>