It depends on the mail. The name of the part is in the Content-Type
header, the filename of the attachment in Content-Disposition. To
extract parts of a header like Content-Type you can use the method
getHeaderField($header_name, $part);. Without $part you get the first
field, which doesn't have a name, so that can be used instead of
strtok(). To get the (file-)name see this example:
<?php
$mail =
'From: [email protected]
To: [email protected]
Content-Type: multipart/mixed; boundary=my_bound
Mime-Version: 1.0
Subject: subject
Date: Fri, 01 Oct 2008 00:00:00 +0200
--my_bound
Content-Type: text/plain
plain text part
--my_bound
Content-Disposition: inline; filename=file.pdf
Content-Type: application/pdf; name="file.pdf"
..PDF..content..
--my_bound--';
require_once 'Zend/Mail/Message.php';
$message = new Zend_Mail_Message(array('raw' => $mail));
$part = $message->getPart(2);
echo 'The content type of this part is ', $part-
>getHeaderField('Content-Type');
echo ' and the filename is ', $part->getHeaderField('Content-Type',
'name');
echo ' or ', $part->getHeaderField('Content-Disposition', 'filename');
?>
Grooters Erwan wrote:
Hi,
I'm currently working on a script to take mail from a pop3 server.
Everything works correctly excepted to get back an attached file.
In fact I can't find the name of attached file.
this is the code I have (directly inspired from doc):
echo $mail->countMessages() . " messages found\n";
$i = 1;
foreach ($mail as $message) {
echo "{$i} : Mail from '{$message->from}': {$message-
>subject}\n";
$i++;
// 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";
}
}
$part = $message;
print_r($part);
while ($part->isMultipart()) {
$part = $message->getPart(2);
}
echo 'Type of this part is ' . strtok($part->contentType,
';') . "\n";
echo "Content:\n";
print_r($part->getContent());
}
I can take the content of the attached part, but I can't find the
NAME of this part...
Thanks,
Grooters Erwan