function mailhandler_retrieve in Mailhandler 5
Same name and namespace in other branches
- 6 mailhandler.retrieve.inc \mailhandler_retrieve()
- 7 mailhandler.retrieve.inc \mailhandler_retrieve()
Retrieve all msgs from a given mailbox and process them.
2 calls to mailhandler_retrieve()
- mailhandler_admin_retrieve in ./
mailhandler.module - Menu callback; Retrieve and process pending e-mails for a mailbox.
- mailhandler_cron in ./
mailhandler.module - Implementation of hook_cron(). Process msgs from all enabled mailboxes.
File
- ./
mailhandler.module, line 6
Code
function mailhandler_retrieve($mailbox) {
if ($mailbox['domain']) {
if ($mailbox['imap'] == 1) {
$box = '{' . $mailbox['domain'] . ':' . $mailbox['port'] . $mailbox['extraimap'] . '}' . $mailbox['folder'];
}
else {
$box = '{' . $mailbox['domain'] . ':' . $mailbox['port'] . '/pop3' . $mailbox['extraimap'] . '}' . $mailbox['folder'];
}
$result = imap_open($box, $mailbox['name'], $mailbox['pass']);
$err = 'domain';
}
else {
$box = $mailbox['folder'];
$result = imap_open($box, '', '');
}
if ($result) {
$n = imap_num_msg($result);
$num_processed = 0;
for ($i = 1; $i <= $n; $i++) {
$header = imap_header($result, $i);
// only process new messages
if (!$mailbox['delete_after_read'] && $header->Unseen != 'U' && $header->Recent != 'N') {
continue;
}
$mime = explode(',', $mailbox['mime']);
// Get the first text part - this will be the node body
$origbody = mailhandler_get_part($result, $i, $mime[0]);
// If we didn't get a body from our first attempt, try the alternate format (HTML or PLAIN)
if (!$origbody) {
$origbody = mailhandler_get_part($result, $i, $mime[1]);
}
// Parse MIME parts, so all mailhandler modules have access to
// the full array of mime parts without having to process the email.
$mimeparts = mailhandler_get_parts($result, $i);
// Is this an empty message with no body and no mimeparts?
if (!$origbody && !$mimeparts) {
// @TODO: Log that we got an empty email?
continue;
}
$num_processed++;
// we must process before authenticating because the password may be in Commands
$node = mailhandler_process_message($header, $origbody, $mailbox);
// check if mail originates from an authenticated user
$node = mailhandler_authenticate($node, $header, $origbody, $mailbox);
// Put $mimeparts on the node
$node->mimeparts = $mimeparts;
// we need to change the current user
// this has to be done here to allow modules
// to create users
mailhandler_switch_user($node->uid);
// modules may override node elements before submitting. they do so by returning the node.
foreach (module_list() as $name) {
if (module_hook($name, 'mailhandler')) {
$function = $name . '_mailhandler';
if (!($node = $function($node, $result, $i, $header, $mailbox))) {
// Exit if a module has handled the submitted data.
break;
}
}
}
if ($node) {
if ($node->type == 'comment') {
mailhandler_comment_submit($node, $header, $mailbox, $origbody);
}
else {
mailhandler_node_submit($node, $header, $mailbox, $origbody);
}
}
// don't delete while we're only getting new messages
if ($mailbox['delete_after_read']) {
imap_delete($result, $i);
}
// switch back to original user
mailhandler_switch_user();
}
imap_close($result, CL_EXPUNGE);
return t('Mailhandler retrieve successful: %num_processed messages for %m', array(
'%num_processed' => $num_processed,
'%m' => $mailbox['mail'],
));
}
else {
if ($err) {
watchdog('mailhandler', t('Mailhandler %c connection failed: %m', array(
'%c' => $mailbox['imap'] ? 'imap' : 'POP3',
'%m' => $mailbox['mail'],
)), WATCHDOG_ERROR);
return t('Mailhandler %c connection failed: %m', array(
'%c' => $mailbox['imap'] ? 'imap' : 'POP3',
'%m' => $mailbox['mail'],
));
}
else {
watchdog('mailhandler', t('Mailhandler: Could not access local folder: %m', array(
'%m' => $mailbox['mail'],
)), WATCHDOG_ERROR);
return t('Mailhandler could not access local folder: %m', array(
'%m' => $mailbox['mail'],
));
}
}
}