I am pretty new when it comes to handling emails in php.
Basically I need to read a pop3 mailbox and save the messages to a database and the attachments to a folder. Then mail that same mail (slightly altered) back to another email recipient.
I thought I got this all working, then I realized when the attachment is another mail message I only receive the body as text (after base64_decode).
Is it possible to save an attached email to a file and then send it as attachment in another message?
Any help will be appreciated.
Here is some code to put it into perspective :
// This is the IMAP connection: imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass,OP_SILENT);
$mailConnection = $this->Maildata->mail_login($host,$port,$user,$pass,$folder,$ssl);
// This retrieves the list of mails in the current folder
$messageList = $this->Maildata->mail_list($mailConnection,$message);
foreach ($messageList as $messageId => $messageListData) {
// This reads the mail object into an array
$mailMessage = $this->Maildata->mail_mime_to_array($mailConnection,$messageId,true);
// This loops through each mail
foreach ($mailMessage as $tmpId => $tmpData) {
// Excluded save to database since it is unrelated
// This checks if item is an attachment and saves it.
if (isset($tmpData['is_attachment']) && $tmpData['is_attachment'] === true) {
$tmpName = date('Y-m-d H:i:s', strtotime('now'));
$this->Maildata->mail_attachment_save($tmpData['data'], $tmpName .$tmpData['name']);
}
}
}
P.S. This all works, but imap_fetchbody($mailConnection , $messageId, $prefix); doesn’t seem to send all the data I need.
Yes.
You can save attached mail messages as a file, but it will not be in a format that Microsoft Office Outlook will understand. Interestingly enough if you save the file as a .eml then windows live mail will handle the message perfectly.
No webmail service handles attached mail items as outlook and windlows live mail does and gmail ignores them completely.