summaryrefslogtreecommitdiff
path: root/import/modify.php
blob: a05d01ce2eee2dcb04cb2e5726f4be286b5be6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
//modify an mbox file
require_once 'Mail/Mbox.php';

//This function just lists all subjects
function listSubjects($mbox) {
	echo 'Mbox has ' . $mbox->size() . ' messages.' . "\n";

	for ($n = 0; $n < $mbox->size(); $n++) {
		$message = $mbox->get($n);
		preg_match('/Subject: (.*)$/m', $message, $matches);
		$subject = $matches[1];
		echo 'Mail #' . $n . ': ' . $subject . "\n";
	}
	echo "\n";
}

//make a copy of the demo file
$original = dirname(__FILE__) . '/demobox';
$file = tempnam('/tmp', 'mbox-copy-');
copy($original, $file);
echo 'Using file ' . $file . "\n";

$mbox = new Mail_Mbox($file);
$mbox->open();
listSubjects($mbox);

echo 'append a message to the end of the box' . "\n";
$message = $mbox->get(0) . "\n" . 'This is a copy of the mail';
$mbox->insert($message);
listSubjects($mbox);

echo 'insert a message before the second message' . "\n";
$message = $mbox->get(0) . "\n" . 'This is another copy of the mail';
$mbox->insert($message, 1);
listSubjects($mbox);

echo 'remove the last message' . "\n";
$mbox->remove(
	$mbox->size() - 1,
);
listSubjects($mbox);

echo 'remove the first two messages' . "\n";
$mbox->remove([0, 1]);
listSubjects($mbox);

$mbox->close();

//remove the tmp file
unlink($file);
?>