PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Working with messages

After you fetch the messages with getMessage() you want to fetch headers, the content or single parts of a multipart message. All headers can be accessed via properties or the method getHeader() if you want more control or have unusual header names. The header names are lower-cased internally, thus the case of the header name in the mail message doesn't matter. Also headers with a dash can be written in camel-case. If no header is found for both notations an exception is thrown. To encounter this the method headerExists() can be used to check the existence of a header.

<?php
// get the message object
$message $mail->getMessage(1);

// output subject of message
echo $message->subject "\n";

// get content-type header
$type $message->contentType;

// check if CC isset:
if( isset($message->cc) ) { // or $message->headerExists('cc');
    
$cc $message->cc;
}

If you have multiple headers with the same name- i.e. the Received headers- you might want an array instead of a string. In this case, use the getHeader() method.

<?php
// get header as property - the result is always a string,
// with new lines between the single occurrences in the message
$received $message->received;

// the same via getHeader() method
$received $message->getHeader('received''string');

// better an array with a single entry for every occurrences
$received $message->getHeader('received''array');
foreach (
$received as $line) {
    
// do stuff
}

// if you don't define a format you'll get the internal representation
// (string for single headers, array for multiple)
$received $message->getHeader('received');
if (
is_string($received)) {
    
// only one received header found in message
}

The method getHeaders() returns all headers as array with the lower-cased name as key and the value as and array for multiple headers or as string for single headers.

<?php
// dump all headers
foreach ($message->getHeaders() as $name => $value) {
    if (
is_string($value)) {
        echo 
"$name$value\n";
        continue;
    }
    foreach (
$value as $entry) {
        echo 
"$name$entry\n";
    }
}

If you don't have a multipart message, fetching the content is easily done via getContent(). Unlike the headers, the content is only fetched when needed (aka late-fetch).

<?php
// output message content for HTML
echo '<pre>';
echo 
$message->getContent();
echo 
'</pre>';

Checking for multipart messages is done with the method isMultipart(). If you have multipart message you can get an instance of Zend_Mail_Part with the method getPart(). Zend_Mail_Part is the base class of Zend_Mail_Message, so you have the same methods: getHeader(), getHeaders(), getContent(), getPart(), isMultipart() and the properties for headers.

<?php
// get the first none multipart part
$part $message;
while (
$part->isMultipart()) {
    
$part $message->getPart(1);
}
echo 
'Type of this part is ' strtok($part->contentType';') . "\n";
echo 
"Content:\n";
echo 
$part->getContent();

Zend_Mail_Part also implements RecursiveIterator, which makes it easy to scan through all parts. And for easy output, it also implements the magic method __toString(), which returns the content.

<?php
// output first text/plain part
$foundPart null;
foreach (new 
RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
    try {
        if (
strtok($part->contentType';') == 'text/plain') {
            
$foundPart $part;
            break;
        }
    } catch (
Zend_Mail_Exception $e) {
        
// ignore
    
}
}
if (!
$foundPart) {
    echo 
'no plain text part found';
} else {
    echo 
"plain text part: \n" $foundPart;
}

Zend Framework