@@ -207,43 +207,54 @@ final public function getReferences(): array
207207 */
208208 final public function getBodyHtml (): ?string
209209 {
210- $ htmlParts = $ this ->getBodyHtmlParts ( );
210+ $ htmlParts = $ this ->getAllContentsBySubtype ( self :: SUBTYPE_HTML );
211211
212212 return $ htmlParts [0 ] ?? null ;
213213 }
214214
215215 /**
216- * Get body HTML parts.
216+ * Get all contents parts of specific subtype (self::SUBTYPE_HTML or self::SUBTYPE_PLAIN) .
217217 *
218218 * @return string[]
219219 */
220- final public function getBodyHtmlParts ( ): array
220+ final public function getAllContentsBySubtype ( string $ subtype ): array
221221 {
222222 $ iterator = new \RecursiveIteratorIterator ($ this , \RecursiveIteratorIterator::SELF_FIRST );
223- $ htmlParts = [];
223+ $ parts = [];
224+ /** @var PartInterface $part */
224225 foreach ($ iterator as $ part ) {
225- if (self :: SUBTYPE_HTML === $ part ->getSubtype ()) {
226- $ htmlParts [] = $ part ->getDecodedContent ();
226+ if ($ subtype === $ part ->getSubtype ()) {
227+ $ parts [] = $ part ->getDecodedContent ();
227228 }
228229 }
229- if (\count ($ htmlParts ) > 0 ) {
230- return $ htmlParts ;
230+ if (\count ($ parts ) > 0 ) {
231+ return $ parts ;
231232 }
232233
233- // If message has no parts and is HTML , return content of message itself .
234- if (self :: SUBTYPE_HTML === $ this ->getSubtype ()) {
234+ // If message has no parts and is of right type , return content of message.
235+ if ($ subtype === $ this ->getSubtype ()) {
235236 return [$ this ->getDecodedContent ()];
236237 }
237238
238239 return [];
239240 }
240241
242+ /**
243+ * Get body HTML parts.
244+ *
245+ * @return string[]
246+ */
247+ final public function getBodyHtmlParts (): array
248+ {
249+ return $ this ->getAllContentsBySubtype (self ::SUBTYPE_HTML );
250+ }
251+
241252 /**
242253 * Get all body HTML parts merged into 1 html.
243254 */
244255 final public function getCompleteBodyHtml (): ?string
245256 {
246- $ htmlParts = $ this ->getBodyHtmlParts ( );
257+ $ htmlParts = $ this ->getAllContentsBySubtype ( self :: SUBTYPE_HTML );
247258
248259 if (1 === \count ($ htmlParts )) {
249260 return $ htmlParts [0 ];
@@ -279,19 +290,28 @@ final public function getCompleteBodyHtml(): ?string
279290 */
280291 final public function getBodyText (): ?string
281292 {
282- $ iterator = new \RecursiveIteratorIterator ($ this , \RecursiveIteratorIterator::SELF_FIRST );
283- foreach ($ iterator as $ part ) {
284- if (self ::SUBTYPE_PLAIN === $ part ->getSubtype ()) {
285- return $ part ->getDecodedContent ();
286- }
287- }
293+ $ plainParts = $ this ->getAllContentsBySubtype (self ::SUBTYPE_PLAIN );
294+
295+ return $ plainParts [0 ] ?? null ;
296+ }
288297
289- // If message has no parts, return content of message itself.
290- if (self ::SUBTYPE_PLAIN === $ this ->getSubtype ()) {
291- return $ this ->getDecodedContent ();
298+ /**
299+ * Get all body PLAIN parts merged into 1 string.
300+ *
301+ * @return null|string Null if message has no PLAIN message parts
302+ */
303+ final public function getCompleteBodyText (): ?string
304+ {
305+ $ plainParts = $ this ->getAllContentsBySubtype (self ::SUBTYPE_PLAIN );
306+
307+ if (1 === \count ($ plainParts )) {
308+ return $ plainParts [0 ];
309+ }
310+ if (0 === \count ($ plainParts )) {
311+ return null ;
292312 }
293313
294- return null ;
314+ return \implode ( "\n" , $ plainParts ) ;
295315 }
296316
297317 /**
0 commit comments