diff --git a/modules/export_seda/ArchiveTransfer.php b/modules/export_seda/ArchiveTransfer.php
index a50950a80a5facc63dd617a449b2318d97fe5784..f8b52d8c8be5f55ca1ff0192e4168be044a95e6b 100644
--- a/modules/export_seda/ArchiveTransfer.php
+++ b/modules/export_seda/ArchiveTransfer.php
@@ -31,16 +31,18 @@ class ArchiveTransfer {
 		$this->db = new Database();
 	}
 
-	public function receive($listResId, $transferringAgency = "", $archivalAgency = "") {
-		
+	public function receive($listResId) {
 		if (!$listResId) {
 			return false;
 		}
 
 		$messageObject = new stdClass();
-		$messageObject = $this->initMessage($messageObject, $transferringAgency, $archivalAgency);
+		$messageObject = $this->initMessage($messageObject);
 
+		$result = [];
 		foreach ($listResId as $resId) {
+			$result .= $resId.'#';
+
 			$letterbox = $this->getCourrier($resId);
 
 			if ($letterbox->filename) {
@@ -51,11 +53,15 @@ class ArchiveTransfer {
 			}
 		}
 
-		//TODO
-		//$messageObject->dataObjectPackage->managementMetadata
+		$res = $this->insertMessage($messageObject);
+
+		if ($res) {
+			$this->sendXml($messageObject);
+		} else {
+			return $res;
+		}
 		
-		$this->insertMessage($messageObject);
-		return $messageObject;
+		return $result;
 	}
 
 	public function sendXml($messageObject)
@@ -67,12 +73,35 @@ class ArchiveTransfer {
 		$DOMTemplateProcessor->merge();
 		$DOMTemplateProcessor->removeEmptyNodes();
 
-        file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'seda2'.DIRECTORY_SEPARATOR.$messageObject->messageIdentifier->value.'.xml', $DOMTemplate->saveXML());
+		if (!is_dir(__DIR__.DIRECTORY_SEPARATOR.'seda2')) {
+			mkdir(__DIR__.DIRECTORY_SEPARATOR.'seda2', 0777, true);
+		}
+
+		$messageId = $messageObject->messageIdentifier->value;
+		if (!is_dir(__DIR__.DIRECTORY_SEPARATOR.'seda2'.DIRECTORY_SEPARATOR.$messageId)) {
+			mkdir(__DIR__.DIRECTORY_SEPARATOR.'seda2'.DIRECTORY_SEPARATOR.$messageId, 0777, true);
+		}
+
+        file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'seda2'.DIRECTORY_SEPARATOR.$messageId.DIRECTORY_SEPARATOR.$messageId.'.xml', $DOMTemplate->saveXML());
+
+        $this->sendAttachment($messageObject);
 
 		return $xml;
 	}
 
-	private function initMessage($messageObject, $transferringAgency =null, $archivalAgency = null)
+	private function sendAttachment($messageObject)
+	{
+		$messageId = $messageObject->messageIdentifier->value;
+
+		foreach ($messageObject->dataObjectPackage->binaryDataObject as $binaryDataObject) {
+			$basename = basename($binaryDataObject->uri);
+			$dest = __DIR__.DIRECTORY_SEPARATOR.'seda2'.DIRECTORY_SEPARATOR.$messageId.DIRECTORY_SEPARATOR.$basename;
+
+			copy($binaryDataObject->uri, $dest);
+		}
+	}
+
+	private function initMessage($messageObject)
 	{
 		$messageObject->date = date('Y-m-d h:i:s');
 		$messageObject->messageIdentifier = new stdClass();
@@ -81,31 +110,20 @@ class ArchiveTransfer {
 		$messageObject->transferringAgency = new stdClass();
 		$messageObject->transferringAgency->identifier = new stdClass();
 
-		if ($transferringAgency) {
-			$messageObject->transferringAgency->identifier->value = $transferringAgency;
-		}else {
-			foreach ($_SESSION['user']['entities'] as $entitie) {
-				if ($entitie['ENTITY_TYPE'] == "Service") {
-					$entitie = $this->getEntitie($entitie['ENTITY_ID']);
-					if ($entitie) {
-						$messageObject->transferringAgency->identifier->value = $entitie->business_id;
-					} else {
-						// TODO return error;
-					}
-				}
-			}
-		}
-
-		$messageObject->archivalAgreement = new stdClass();
-		$messageObject->archivalAgreement->value = "A COMPLETER";
-
 		$messageObject->archivalAgency = new stdClass();
 		$messageObject->archivalAgency->identifier = new stdClass();
 
-		if ($archivalAgency) {
-			$messageObject->archivalAgency->identifier->value = $archivalAgency;
-		} else {
-			$messageObject->archivalAgency->identifier->value = "A COMPLETER";
+		$messageObject->archivalAgreement = new stdClass();
+
+		foreach ($_SESSION['user']['entities'] as $entitie) {
+			$entitie = $this->getEntitie($entitie['ENTITY_ID']);
+			if ($entitie) {
+				$messageObject->transferringAgency->identifier->value = $entitie->business_id;
+				$messageObject->archivalAgency->identifier->value = $entitie->archival_agency;
+				$messageObject->archivalAgreement->value = $entitie->archival_agreement;
+			} else {
+				// TODO return error;
+			}
 		}
 		
 		$messageObject->dataObjectPackage = new stdClass();
@@ -117,70 +135,96 @@ class ArchiveTransfer {
 		return $messageObject;
 	}
 
+	
+
 	private function getArchiveUnit($letterbox)
 	{
 		$messageArchiveUnit = new stdClass();
 
-		$messageArchiveUnit->content = new stdClass();
-		$messageArchiveUnit->content->receivedDate = $letterbox->admission_date;
-		$messageArchiveUnit->content->sentDate = $letterbox->doc_date;
-		$messageArchiveUnit->content->receivedDate = $letterbox->admission_date;
-		$messageArchiveUnit->content->receivedDate = $letterbox->admission_date;
+		$messageArchiveUnit->content = $this->getContent($letterbox);
+		
+		$messageArchiveUnit->management = $this->getManagement($letterbox);
+
+		if ($dataObjectReferenceId) {
+			$messageArchiveUnit->dataObjectReference = new stdClass();
+			$messageArchiveUnit->dataObjectReference->dataObjectReferenceId = $letterbox->res_id;
+		}
+
+		return $messageArchiveUnit;
+	}
+
+	private function getContent($letterbox)
+	{
+		$content = new stdClass();
+		$content->receivedDate = $letterbox->admission_date;
+		$content->sentDate = $letterbox->doc_date;
+		$content->receivedDate = $letterbox->admission_date;
+		$content->receivedDate = $letterbox->admission_date;
 
-		$messageArchiveUnit->content->addressee = [];
-		$messageArchiveUnit->content->keyword = [];
+		$content->addressee = [];
+		$content->keyword = [];
 
 		if ($letterbox->exp_contact_id) {
 			
 			$contact = $this->getContact($letterbox->exp_contact_id);
 			$entitie = $this->getEntitie($letterbox->destination);
 
-			$messageArchiveUnit->content->keyword[] = $this->getKeyword($contact);
-			$messageArchiveUnit->content->addressee[] = $this->getAddresse($entitie,"entitie");
+			$content->keyword[] = $this->getKeyword($contact);
+			$content->addressee[] = $this->getAddresse($entitie,"entitie");
 		} else if ($letterbox->dest_contact_id) {
 			$contact = $this->getContact($letterbox->dest_contact_id);
 			$entitie = $this->getEntitie($letterbox->destination);
 
-			$messageArchiveUnit->content->addressee[] = $this->getAddresse($contact);
-			$messageArchiveUnit->content->keyword[] = $this->getKeyword($entitie,"entitie");
+			$content->addressee[] = $this->getAddresse($contact);
+			$content->keyword[] = $this->getKeyword($entitie,"entitie");
 		} else if ($letterbox->exp_user_id) {
 			$user = $this->getUserInformation($letterbox->exp_user_id);
 			$entitie = $this->getEntitie($letterbox->initiator);
 			//$entitie = $this->getEntitie($letterbox->destination);
 
-			$messageArchiveUnit->content->keyword[] = $this->getKeyword($user);
-			$messageArchiveUnit->content->addressee[] = $this->getAddresse($entitie,"entitie");
+			$content->keyword[] = $this->getKeyword($user);
+			$content->addressee[] = $this->getAddresse($entitie,"entitie");
 		}
 		
-		$messageArchiveUnit->content->source = $_SESSION['mail_nature'][$letterbox->nature_id];
+		$content->source = $_SESSION['mail_nature'][$letterbox->nature_id];
 
-		$messageArchiveUnit->content->documentType = $letterbox->type_label;
-		$messageArchiveUnit->content->originatingAgencyArchiveIdentifier = $letterbox->alt_identifier;
-		$messageArchiveUnit->content->originatingSystemId = $letterbox->res_id;
-		$messageArchiveUnit->content->title = [];
-		$messageArchiveUnit->content->title[] = $letterbox->subject;
-		$messageArchiveUnit->content->description = [];
-		$messageArchiveUnit->content->description[] = " ";
-		$messageArchiveUnit->content->endDate = $letterbox->process_limit_date;
+		$content->documentType = $letterbox->type_label;
+		$content->originatingAgencyArchiveIdentifier = $letterbox->alt_identifier;
+		$content->originatingSystemId = $letterbox->res_id;
+		$content->title = [];
+		$content->title[] = $letterbox->subject;
+		$content->description = [];
+		$content->description[] = " ";
+		$content->endDate = $letterbox->process_limit_date;
 
 		$notes = $this->getNotes($letterbox->res_id);
-		$messageArchiveUnit->content->custodialHistory = new stdClass();
-		$messageArchiveUnit->content->custodialHistory->custodialHistoryItem = [];
+		$content->custodialHistory = new stdClass();
+		$content->custodialHistory->custodialHistoryItem = [];
 
 		foreach ($notes as $note) {
-			$messageArchiveUnit->content->custodialHistory->custodialHistoryItem[] = $this->getCustodialHistoryItem($note);
+			$content->custodialHistory->custodialHistoryItem[] = $this->getCustodialHistoryItem($note);
 		}
 
-		if ($dataObjectReferenceId) {
-			$messageArchiveUnit->dataObjectReference = new stdClass();
-			$messageArchiveUnit->dataObjectReference->dataObjectReferenceId = $letterbox->res_id;
-		}
+		return $content;
+	}
 
-		return $messageArchiveUnit;
+	private function getManagement($letterbox) {
+		$management = new stdClass();
+
+		$docTypes = $this->getDocTypes($letterbox->type_id);
+
+		$management->appraisalRule = new stdClass();
+		$management->appraisalRule->rule = new stdClass();
+		$management->appraisalRule->rule->value = $docTypes->retention_rule;
+		$management->appraisalRule->finalAction = $docTypes->retention_final_disposition;
+		
+		return $management;
 	}
 
 	private function getBinaryDataObject($letterbox)
 	{
+		$docServers = $this->getDocServer($letterbox->docserver_id);
+
 		$binaryDataObject = new stdClass();
 		$binaryDataObject->id = $letterbox->res_id;
 		$binaryDataObject->messageDigest = new stdClass();
@@ -192,7 +236,7 @@ class ArchiveTransfer {
 		$uri = str_replace("##", DIRECTORY_SEPARATOR, $letterbox->path);
 		$uri =  str_replace("#", DIRECTORY_SEPARATOR, $uri);
 		$uri .= $letterbox->filename;
-		$binaryDataObject->uri = $uri;
+		$binaryDataObject->uri = $docServers->path_template.$uri;
 
 		return $binaryDataObject;
 	}
@@ -259,6 +303,21 @@ class ArchiveTransfer {
 		return $letterbox;
 	}
 
+	private function getDocTypes($typeId)
+	{
+		$queryParams = [];
+
+		$queryParams[] = $typeId;
+
+		$query = "SELECT * FROM doctypes WHERE type_id = ?";
+
+		$smtp = $this->db->query($query,$queryParams);
+		
+		$docTypes = $smtp->fetchObject();
+
+		return $docTypes;
+	}
+
 	private function getUserInformation($userId) 
 	{
 		$queryParams = [];
@@ -322,6 +381,21 @@ class ArchiveTransfer {
 		return $contact;
 	}
 
+	private function getDocServer($docServerId)
+	{
+		$queryParams = [];
+
+		$queryParams[] = $docServerId;
+
+		$query = "SELECT * FROM docservers WHERE docserver_id = ?";
+
+		$smtp = $this->db->query($query,$queryParams);
+		
+		$docServers = $smtp->fetchObject();
+
+		return $docServers;
+	}
+
 	private function insertMessage($messageObject) 
 	{
 		$queryParams = [];
@@ -336,9 +410,9 @@ class ArchiveTransfer {
 				date,
 				reference,
 	            account_id ,
-				sender_org_identifier_id,
+				sender_org_identifier,
 				sender_org_name,
-				recipient_org_identifier_id,
+				recipient_org_identifier,
 				recipient_org_name,
 				archival_agreement_reference,
 				reply_code,
@@ -373,20 +447,28 @@ class ArchiveTransfer {
 				$this->insertUnitIdentifier($messageId, "res_letterbox", $binaryDataObject->id);
 			}
 		} catch (Exception $e) {
-			// return error
+			var_dump($e);
+			return false;
 		}
-	}
 
-	private function insertUnitIdentifier($messageId, $tableName, $resId) {
-		$query = ("INSERT INTO unit_identifier VALUES (?,?,?)");
-		$queryParams = [];
+		return true;
+	}
 
-		$queryParams[] = $messageId;
-		$queryParams[] = $tableName;
-		$queryParams[] = $resId;
+	private function insertUnitIdentifier($messageId, $tableName, $resId) 
+	{
+		try {
+			$query = ("INSERT INTO unit_identifier VALUES (?,?,?)");
+			$queryParams = [];
 
-		$res = $this->db->query($query,$queryParams);
+			$queryParams[] = $messageId;
+			$queryParams[] = $tableName;
+			$queryParams[] = $resId;
 
-		return $res;
+			$res = $this->db->query($query,$queryParams);
+		} catch (Exception $e) {
+			return false;
+		}
+		
+		return true;
 	}
 }
\ No newline at end of file
diff --git a/modules/export_seda/export_seda.php b/modules/export_seda/export_seda.php
index 18a53aed3a784ba84fdffe0c971cf37721539c6f..00d08cceac90634fb39967c26f3e10ce1213e8ad 100644
--- a/modules/export_seda/export_seda.php
+++ b/modules/export_seda/export_seda.php
@@ -28,12 +28,19 @@ $etapes = array('export');
 
 function manage_export($arr_id, $history, $id_action, $label_action, $status)
 {
-    // récupérer l'entité racine du courrier
-    // récupérer transferring_agency et archival_agreement
+    require_once('modules/export_seda/ArchiveTransfer.php');
 
-    // récupérer la duration et retention_rule du type de doc du courrier
+    $archiveTransfer = new ArchiveTransfer();
 
-    // appel fonction de transfert et génération bdx
+    
+    // récupérer l'entité racine du courrier *
+    // récupérer archival_agency et archival_agreement *
+
+    // récupérer la retention_final_disposition et retention_rule du type de doc du courrier *
+
+    // appel fonction de transfert et génération bdx *
+
+    $result = $archiveTransfer->receive($arr_id);
 
     // historisation du transfert
 
@@ -41,13 +48,6 @@ function manage_export($arr_id, $history, $id_action, $label_action, $status)
 
     // ensuite il y a aura une suppression logique des documents et des contacts (si plus de courriers associés)
 
-    for($i=0; $i<count($arr_id);$i++)
-    {
-        $result .= $arr_id[$i].'#';
-        //$db->query("UPDATE ".$ext_table. " SET closing_date = CURRENT_TIMESTAMP WHERE res_id = ?", array($arr_id[$i]));
-
-    }
-
     return array('result' => $result, 'history_msg' => '');
 }
 
diff --git a/modules/export_seda/resources/AbstractRule.xml b/modules/export_seda/resources/AbstractRule.xml
index a19e1967955259432ac7fac60ac1de1983558913..bea6adcdfddbdd4776f349204dcbe1623a660b70 100644
--- a/modules/export_seda/resources/AbstractRule.xml
+++ b/modules/export_seda/resources/AbstractRule.xml
@@ -1,4 +1,4 @@
-<?merge $abstractRule.rule ?><Rule><?merge .value ?></Rule>
-<?merge $abstractRule.startDate ?><StartDate><?merge . ?></StartDate>
+<?merge $abstractRule.rule.bool() ?><Rule><?merge $abstractRule.rule.value ?></Rule>
+<?merge $abstractRule.startDate.bool() ?><StartDate><?merge $abstractRule.startDate.value ?></StartDate>
 <PreventInheritance><?merge $abstractRule.preventInheritance ?></PreventInheritance>
 <RefNonRuleId><?merge $abstractRule.refNonRuleId ?></RefNonRuleId>
\ No newline at end of file
diff --git a/modules/export_seda/resources/ArchiveUnit.xml b/modules/export_seda/resources/ArchiveUnit.xml
index d5869fdc4301a17df2429eb2baec247741adab3f..8d2ea8d3d5ad7eee8240fdf59bfa6860c385fa0d 100644
--- a/modules/export_seda/resources/ArchiveUnit.xml
+++ b/modules/export_seda/resources/ArchiveUnit.xml
@@ -4,28 +4,34 @@
     <ArchiveUnitProfile schemeID="[?merge .archiveUnitProfile.schemeID ?]" schemeName="[?merge .archiveUnitProfile.schemeName ?]" schemeAgencyID="[?merge .archiveUnitProfile.schemeAgencyID ?]" schemeAgencyName="[?merge .archiveUnitProfile.schemeAgencyName ?]" schemeVersionID="[?merge .archiveUnitProfile.schemeVersionID ?]" schemeDataURI="[?merge .archiveUnitProfile.schemeDataURI ?]" schemeURI="[?merge .archiveUnitProfile.schemeURI ?]"><?merge .archiveUnitProfile ?></ArchiveUnitProfile>
     <?merge .management.bool() ?>
     <Management>
+        <?merge .management.storageRule.bool() ?>
         <StorageRule>
             <?merge .management.storageRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
             <FinalAction><?merge .management.storageRule.finalAction ?></FinalAction>
         </StorageRule>
+        <?merge .management.appraisalRule.bool() ?>
         <AppraisalRule>
             <?merge .management.appraisalRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
             <FinalAction><?merge .management.appraisalRule.finalAction ?></FinalAction>
         </AppraisalRule>
+        <?merge .management.accessRule.bool() ?>
         <AccessRule>
             <?merge .management.accessRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
         </AccessRule>
+        <?merge .management.disseminationRule.bool() ?>
         <DisseminationRule>
             <?merge .management.disseminationRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
         </DisseminationRule>
+        <?merge .management.reuseRule.bool() ?>
         <ReuseRule>
             <?merge .management.reuseRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
         </ReuseRule>
+        <?merge .management.classificationRule.bool() ?>
         <ClassificationRule>
             <?merge .management.classificationRule $abstractRule ?>
             <?xinclude /resources/AbstractRule.xml ?>
@@ -34,9 +40,11 @@
             <ClassificationReassessingDate><?merge .management.classificationRule.classificationReassessingDate ?></ClassificationReassessingDate>
             <NeedReassessingAuthorization><?merge .management.classificationRule.needReassessingAuthorization ?></NeedReassessingAuthorization>
         </ClassificationRule>
+        <?merge .management.needAuthorization.bool() ?>
         <NeedAuthorization>
             <?merge .management.needAuthorization ?>
         </NeedAuthorization>
+        <?merge .management.otherManagementAbstract.bool() ?>
         <OtherManagementAbstract>
             <?merge .management.otherManagementAbstract ?>
         </OtherManagementAbstract>
@@ -48,7 +56,8 @@
         <?merge .content.filePlanPosition ?><FilePlanPosition schemeID="[?merge .schemeID @schemeID ?]" schemeName="[?merge .schemeName @schemeName ?]" schemeAgencyID="[?merge .schemeAgencyID @schemeAgencyID ?]" schemeAgencyName="[?merge .schemeAgencyName @schemeAgencyName ?]" schemeVersionID="[?merge .schemeVersionID @schemeVersionID ?]" schemeDataURI="[?merge .schemeDataURI @schemeDataURI ?]" schemeURI="[?merge .schemeURI @schemeURI ?]"><?merge .value ?></FilePlanPosition>
         <OriginatingAgencyArchiveIdentifier><?merge .content.originatingAgencyArchiveIdentifier ?></OriginatingAgencyArchiveIdentifier>
         <?merge .content.description.bool() ?><Description><?merge .content.description ?></Description>
-        <?merge .content.custodialHistory.bool() ?><CustodialHistory>
+        <?merge .content.custodialHistory.bool() ?>
+        <CustodialHistory>
             <?merge .content.custodialHistory.custodialHistoryItem ?>
             <CustodialHistoryItem when="[?merge .when ?]"><?merge .value ?></CustodialHistoryItem>
         </CustodialHistory>
diff --git a/modules/export_seda/resources/DataObjectPackage.xml b/modules/export_seda/resources/DataObjectPackage.xml
index 1647fdf1914452ce3df5c7ad120cb854c9c94866..e1982c643e2a53b208bd1a1b7f358ebda0864832 100644
--- a/modules/export_seda/resources/DataObjectPackage.xml
+++ b/modules/export_seda/resources/DataObjectPackage.xml
@@ -14,7 +14,7 @@
             <Filename><?merge .fileInfo.filename ?></Filename>
         </FileInfo>
     </BinaryDataObject>
-    <?merge $dataObjectPackage.physicalDataObject ?>
+    <?merge $dataObjectPackage.physicalDataObject.bool() ?>
     <PhysicalDataObject>
         <?merge .bool() ?>
         <PhysicalId schemeID="[?merge .physicalId.schemeID ?]" schemeName="[?merge .physicalId.schemeName ?]" schemeAgencyID="[?merge .physicalId.schemeAgencyID ?]" schemeAgencyName="[?merge .physicalId.schemeAgencyName ?]" schemeVersionID="[?merge .physicalId.schemeVersionID ?]" schemeDataURI="[?merge .physicalId.schemeDataURI ?]" schemeURI="[?merge .physicalId.schemeURI ?]"><?merge .physicalId.value ?></PhysicalId>