Mail: Display-example
[ ]
[ Display example ] [ Mail listing example ] [ Rfcs ]
[ ]
[ ]
[ ]
[ ]
Mail Parsing and Displaying Example
This script demonstrates how to display parsed email messages. It takes one
argument on the command line. This argument is a file containing one mail file
which is then read and parsed in line 9 to 11. Line 12 starts the display
process.
1. <?php
2. /**
3. * You can run this file for example with:
4. * php display-example.php ../../tests/parser/data/gmail/mail_with_attachment.mail
5. */
6.
7. require_once "../tutorial/tutorial_autoload.php";
8.
9. $set = new ezcMailFileSet( array( $argv[1] ) );
10. $parser = new ezcMailParser();
11. $mail = $parser->parseMail( $set );
12. echo formatMail( $mail[0] );
13.
14. function formatMail( $mail )
15. {
16. $t = '';
17. $t .= "From: ". formatAddress( $mail->from ). "\n";
18. $t .= "To: ". formatAddresses( $mail->to ). "\n";
19. $t .= "Cc: ". formatAddresses( $mail->cc ). "\n";
20. $t .= "Bcc: ". formatAddresses( $mail->bcc ). "\n";
21. $t .= 'Date: '. date( DATE_RFC822, $mail->timestamp ). "\n";
22. $t .= 'Subject: '. $mail->subject . "\n";
23. $t .= "MessageId: ". $mail->messageId . "\n";
24. $t .= "\n";
25. $t .= formatMailPart( $mail->body );
26. return $t;
27. }
28.
29. function formatMailPart( $part )
30. {
31. if ( $part instanceof ezcMail )
32. return formatMail( $part );
33.
34. if ( $part instanceof ezcMailText )
35. return formatMailText( $part );
36.
37. if ( $part instanceof ezcMailFile )
38. return formatMailFile( $part );
39.
40. if ( $part instanceof ezcMailRfc822Digest )
41. return formatMailRfc822Digest( $part );
42.
43. if ( $part instanceof ezcMailMultiPart )
44. return formatMailMultipart( $part );
45.
46. die( "No clue about the ". get_class( $part ) . "\n" );
47. }
48.
49. function formatMailMultipart( $part )
50. {
51. if ( $part instanceof ezcMailMultiPartAlternative )
52. return formatMailMultipartAlternative( $part );
53.
54. if ( $part instanceof ezcMailMultiPartDigest )
55. return formatMailMultipartDigest( $part );
56.
57. if ( $part instanceof ezcMailMultiPartRelated )
58. return formatMailMultipartRelated( $part );
59.
60. if ( $part instanceof ezcMailMultiPartMixed )
61. return formatMailMultipartMixed( $part );
62.
63. die( "No clue about the ". get_class( $part ) . "\n" );
64. }
65.
66. function formatMailMultipartMixed( $part )
67. {
68. $t = '';
69. foreach ( $part->getParts() as $key => $alternativePart )
70. {
71. $t .= "-MIXED-$key------------------------------------------------------------------\n";
72. $t .= formatMailPart( $alternativePart );
73. }
74. $t .= "-MIXED END----------------------------------------------------------\n";
75. return $t;
76. }
77.
78. function formatMailMultipartRelated( $part )
79. {
80. $t = '';
81. $t .= "-RELATED MAIN PART-----------------------------------------------------------\n";
82. $t .= formatMailPart( $part->getMainPart() );
83. foreach ( $part->getRelatedParts() as $key => $alternativePart )
84. {
85. $t .= "-RELATED PART $key-----------------------------------------------------\n";
86. $t .= formatMailPart( $alternativePart );
87. }
88. $t .= "-RELATED END--------------------------------------------------------\n";
89. return $t;
90. }
91.
92. function formatMailMultipartDigest( $part )
93. {
94. $t = '';
95. foreach ( $part->getParts() as $key => $alternativePart )
96. {
97. $t .= "-DIGEST-$key-----------------------------------------------------------------\n";
98. $t .= formatMailPart( $alternativePart );
99. }
100. $t .= "-DIGEST END---------------------------------------------------------\n";
101. return $t;
102. }
103.
104. function formatMailRfc822Digest( $part )
105. {
106. $t = '';
107. $t .= "-DIGEST-ITEM-$key------------------------------------------------------------\n";
108. $t .= "Item:\n\n";
109. $t .= formatMailpart( $part->mail );
110. $t .= "-DIGEST ITEM END----------------------------------------------------\n";
111. return $t;
112. }
113.
114. function formatMailMultipartAlternative( $part )
115. {
116. $t = '';
117. foreach ( $part->getParts() as $key => $alternativePart )
118. {
119. $t .= "-ALTERNATIVE ITEM $key-------------------------------------------------------\n";
120. $t .= formatMailPart( $alternativePart );
121. }
122. $t .= "-ALTERNATIVE END----------------------------------------------------\n";
123. return $t;
124. }
125.
126. function formatMailText( $part )
127. {
128. $t = '';
129. $t .= "Original Charset: {$part->originalCharset}\n";
130. $t .= "Charset: {$part->charset}\n";
131. $t .= "Encoding: {$part->encoding}\n";
132. $t .= "Type: {$part->subType}\n";
133. $t .= "\n{$part->text}\n";
134. return $t;
135. }
136.
137. function formatMailFile( $part )
138. {
139. $t = '';
140. $t .= "Disposition Type: {$part->dispositionType}\n";
141. $t .= "Content Type: {$part->contentType}\n";
142. $t .= "Mime Type: {$part->mimeType}\n";
143. $t .= "Content ID: {$part->contentId}\n";
144. $t .= "Filename: {$part->fileName}\n";
145. $t .= "\n";
146. return $t;
147. }
148.
149. function formatAddresses( $addresses )
150. {
151. $fa = array();
152. foreach ( $addresses as $address )
153. {
154. $fa[] = formatAddress( $address );
155. }
156. return implode( ', ', $fa );
157. }
158.
159. function formatAddress( $address )
160. {
161. $name = '';
162. if ( !empty( $address->name ) )
163. {
164. $name = "{$address->name} ";
165. }
166. return $name . "<{$address->email}>";
167. }
168. ?>
Last updated: Wed, 18 Jun 2008