I’m using PHP-imap to read emails and there attachments from a IMAP Account. The emails are send from an iPhone or iPad. It works fine when they write an email and paste in an image they took.
Here’s a snippet how I get the attachments:
require_once("plugins/MailLib/MailLib.php");
$mailbox = new MailLib();
$mailbox->connect($username, $password, $server, $port, $path, false);
$mails = array();
$mailIds = $mailbox->getMails("UNSEEN");
if($mailIds != null && count($mailIds) > 0) {
foreach($mailIds as $mailId) {
$mail = $mailbox->getMail($mailId);
if($mail['attachments']) {
foreach($mail['attachments'] as $attachment) {
$file = $mailbox->getAttachment($attachment, $mailId);
$destFile = "path/to/folder/".time().".jpg";
$fh = fopen($destFile, 'a') or die("can't open file");
fwrite($fh, $file);
fclose($fh);
}
}
}
It all crashes as soon as they start formatting the content with the default options like bold, italic or underline. Because then I get the following email content:
<div>
<em>Test</em> with <strong>formatting</strong>
<br>
<br>
<img id="7CB777EB-3124-4AEA-8894-6F743EA78F79" src="cid:7CB777EB-3124-4AEA-8894-6F743EA78F79" alt="image.jpeg">
<br>
<br>
<div>Send from my iPhone</div>
</div>
Is there any kind of php library out there to get those attachments as well?
I found a solution for the problem. Someone wrote a small class to extract inline attachments.
With this class it is possible to write the attachment content direct to a file, like the following: