updates to the mail_handler

This commit is contained in:
Quitta 2013-08-10 13:59:36 +02:00
parent 6b4deea068
commit 784fd5ecea
2 changed files with 293 additions and 239 deletions

View file

@ -1,252 +1,295 @@
<?php <?php
class Mail_Handler{
function oms_mail_send($recipient, $subject, $body, $from = NULL) {
private $db;
if(is_numeric($recipient)) {
$id_user = $recipient; function mail_fork() {
$recipient = NULL; /*global $db;
$db = NULL;
$pid = pcntl_fork();
oms_db_connect();
return $pid;*/
$this->db = new DBLayer("lib");
//Start a new thread and return the thread id!
$pid = pcntl_fork();
return $pid;
} }
db_insert('email', array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'id_user' => $id_user, 'sender' => $from));
}
function oms_mail_cron() {
$oms_inbox_username = '123';
$oms_inbox_password = '456';
$oms_host = 'test.com';
$oms_reply_to = "OMS <oms@".$oms_host.">";
global $basedir;
// Deliver new mail /*
echo("mail cron\n"); function oms_db_connect() {
global $db;
global $db_host, $db_name, $db_user, $db_pass;
if(!isset($db)) {
try {
$db = new PDO(
"mysql:host=$db_host;dbname=$db_name",
$db_user,
$db_pass,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
oms_error_log("oms_db_connect: database connection established", 3);
} catch(PDOException $e) {
print "Database error: " . $e->getMessage() . "<br/>";
die();
}
} else {
oms_error_log("oms_db_connect: already connected");
}
}*/
//TO ASK: function oms_mail_send($recipient, $subject, $body, $from = NULL) {
$pid = oms_fork();
$pidfile = '/tmp/oms_cron_email_pid';
if($pid) {
// We're the main process.
} else {
if(!file_exists($pidfile)) {
$pid = getmypid();
$file = fopen($pidfile, 'w');
fwrite($file, $pid);
fclose($file);
$emails = db_query("select * from email where status = 'NEW' or status = 'FAILED'");
foreach($emails as $email) { if(is_numeric($recipient)) {
$message_id = oms_new_message_id(); $id_user = $recipient;
echo("Emailing {$email['recipient']}\n"); $recipient = NULL;
if(!$email['recipient']) { }
$email['recipient'] = oms_get_email_by_user_id($email['id_user']); db_insert('email', array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'id_user' => $id_user, 'sender' => $from));
} }
function mail_cron() {
global $cfg;
$inbox_username = $cfg['mail']['username'];
$inbox_password = $cfg['mail']['password'];
$inbox_host = $cfg['mail']['host'];
$oms_reply_to = "OMS <oms@".$inbox_host.">";
global $basedir;
// Deliver new mail
echo("mail cron\n");
//TO ASK:
$pid = mail_fork();
$pidfile = '/tmp/oms_cron_email_pid';
//if process is running, then we are already cronning
if($pid) {
// We're the main process.
} else {
if(!file_exists($pidfile)) {
if($email['sender']) { $pid = getmypid();
$username = oms_get_username_from_id($email['sender']); $file = fopen($pidfile, 'w');
$from = "$username <$username@$oms_host>"; fwrite($file, $pid);
} else { fclose($file);
$from = $oms_reply_to;
} //select all new & failed emails & try to send them
$headers = "From: $from\r\n" . "Message-ID: " . $message_id; $emails = db_query("select * from email where status = 'NEW' or status = 'FAILED'");
if(mail($email['recipient'], $email['subject'], $email['body'], $headers)) { foreach($emails as $email) {
$status = "DELIVERED"; $message_id = oms_new_message_id();
echo("Emailed {$email['recipient']}\n"); echo("Emailing {$email['recipient']}\n");
} else { if(!$email['recipient']) {
$status = "FAILED"; $email['recipient'] = oms_get_email_by_user_id($email['id_user']);
echo("Email to {$email['recipient']} failed\n"); }
}
db_exec('update email set status = ?, message_id = ?, attempts = attempts + 1 where id_email = ?', array($status, $message_id, $email['id_email'])); if($email['sender']) {
} $username = oms_get_username_from_id($email['sender']);
unlink($pidfile); $from = "$username <$username@$inbox_host>";
} } else {
// Check mail $from = $oms_reply_to;
}
$headers = "From: $from\r\n" . "Message-ID: " . $message_id;
//$mailbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $oms_inbox_username, $oms_inbox_password); if(mail($email['recipient'], $email['subject'], $email['body'], $headers)) {
$mbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $oms_inbox_username, $oms_inbox_password); $status = "DELIVERED";
$message_count = imap_num_msg($mbox); echo("Emailed {$email['recipient']}\n");
} else {
for ($i = 1; $i <= $message_count; ++$i) { $status = "FAILED";
$header = imap_header($mbox, $i); echo("Email to {$email['recipient']} failed\n");
$entire_email = imap_fetchheader($mbox, $i) . imap_body($mbox, $i); }
$subject = decode_utf8($header->subject); //change the status of the emails.
$to = $header->to[0]->mailbox; db_exec('update email set status = ?, message_id = ?, attempts = attempts + 1 where id_email = ?', array($status, $message_id, $email['id_email']));
$from = $header->from[0]->mailbox . '@' . $header->from[0]->host; }
$txt = get_part($mbox, $i, "TEXT/PLAIN"); unlink($pidfile);
$html = get_part($mbox, $i, "TEXT/HTML"); }
//return task ID // Check mail
$tid = oms_create_email($from, $subject, $txt, $html, $to, $from);
//$mailbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $inbox_username, $inbox_password);
if($tid) { $mbox = imap_open($cfg['mail']['server'], $inbox_username, $inbox_password);
$file = fopen("$basedir/mail/$tid", 'w'); $message_count = imap_num_msg($mbox);
fwrite($file, $entire_email);
fclose($file);
}
imap_delete($mbox, $i);
}
imap_expunge($mbox);
imap_close($mbox);
}
}
function oms_new_message_id() {
$time = time();
$pid = getmypid();
global $oms_mail_count;
$oms_mail_count = ($oms_mail_count == '') ? 1 : $oms_mail_count + 1;
return "<oms.message.$pid.$oms_mail_count.$time@dev1.dev.subrigo.net>";
}
function oms_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
if($recipient == 0 && !is_string($recipient)) {
global $user;
$recipient = $user->uid;
}
if($sender !== NULL && !is_numeric($sender)) $sender = oms_get_id_from_username($sender);
if(!is_numeric($recipient)) $recipient = oms_get_id_from_username($recipient);
oms_error_log("Sending message: '$subject' from $sender to $recipient", 3);
$message = array(
'creator' => $sender,
'owner' => $recipient,
'id_module' => 'email',
'type' => 'email',
'summary' => $subject,
'data' => array (
'subject' => $subject,
'body' => $body,
'html' => $html,
'sender' => oms_get_username_from_id($sender),
'from' => $from,
'recipient' => oms_get_username_from_id($recipient),
'time' => time(),
),
);
//TO ASK: for ($i = 1; $i <= $message_count; ++$i) {
oms_task_create($message); $header = imap_header($mbox, $i);
oms_task_index($message, array('subject', 'body', 'sender', 'recipient')); $entire_email = imap_fetchheader($mbox, $i) . imap_body($mbox, $i);
//--------------------------- $subject = decode_utf8($header->subject);
return $message['id_task']; $to = $header->to[0]->mailbox;
} $from = $header->from[0]->mailbox . '@' . $header->from[0]->host;
$txt = get_part($mbox, $i, "TEXT/PLAIN");
$html = get_part($mbox, $i, "TEXT/HTML");
//return task ID
function oms_get_email($id) { $tid = oms_create_email($from, $subject, $txt, $html, $to, $from);
$message = oms_task_load($id); if($tid) {
if($message) { $file = fopen("$basedir/mail/$tid", 'w');
oms_prepare_email($message); fwrite($file, $entire_email);
return $message; fclose($file);
} else { }
return FALSE; imap_delete($mbox, $i);
}
}
function oms_prepare_email(&$message) {
$data = $message['data'];
$data['id_message'] = $message['id_task'];
$data['read'] = ($message['status'] != 'NEW' && $message['status'] != 'UNREAD');
$message = $data;
}
function oms_email_mark_read($mid) {
db_exec("update task set status = 'READ' where id_task = ? and type = 'email' and module = 'email'", array($mid));
}
function decode_utf8($str) {
preg_match_all("/=\?UTF-8\?B\?([^\?]+)\?=/i",$str, $arr);
for ($i=0;$i<count($arr[1]);$i++){
$str=ereg_replace(ereg_replace("\?","\?",
$arr[0][$i]),base64_decode($arr[1][$i]),$str);
}
return $str;
}
function get_mime_type(&$structure) {
$primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
if($structure->subtype) {
return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
}
return "TEXT/PLAIN";
}
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
if(!$structure) {
$structure = imap_fetchstructure($stream, $msg_number);
}
if($structure) {
if($mime_type == get_mime_type($structure)) {
if(!$part_number) {
$part_number = "1";
}
$text = imap_fetchbody($stream, $msg_number, $part_number);
if($structure->encoding == 3) {
return imap_base64($text);
} else if($structure->encoding == 4) {
return imap_qprint($text);
} else {
return $text;
} }
imap_expunge($mbox);
imap_close($mbox);
} }
if($structure->type == 1) /* multipart */ { }
while(list($index, $sub_structure) = each($structure->parts)) {
if($part_number) {
$prefix = $part_number . '.';
function oms_new_message_id() {
$time = time();
$pid = getmypid();
global $oms_mail_count;
$oms_mail_count = ($oms_mail_count == '') ? 1 : $oms_mail_count + 1;
return "<oms.message.$pid.$oms_mail_count.$time@dev1.dev.subrigo.net>";
}
function oms_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
if($recipient == 0 && !is_string($recipient)) {
global $user;
$recipient = $user->uid;
}
if($sender !== NULL && !is_numeric($sender)) $sender = oms_get_id_from_username($sender);
if(!is_numeric($recipient)) $recipient = oms_get_id_from_username($recipient);
oms_error_log("Sending message: '$subject' from $sender to $recipient", 3);
$message = array(
'creator' => $sender,
'owner' => $recipient,
'id_module' => 'email',
'type' => 'email',
'summary' => $subject,
'data' => array (
'subject' => $subject,
'body' => $body,
'html' => $html,
'sender' => oms_get_username_from_id($sender),
'from' => $from,
'recipient' => oms_get_username_from_id($recipient),
'time' => time(),
),
);
//TO ASK:
oms_task_create($message);
oms_task_index($message, array('subject', 'body', 'sender', 'recipient'));
//---------------------------
return $message['id_task'];
}
function oms_get_email($id) {
$message = oms_task_load($id);
if($message) {
oms_prepare_email($message);
return $message;
} else {
return FALSE;
}
}
function oms_prepare_email(&$message) {
$data = $message['data'];
$data['id_message'] = $message['id_task'];
$data['read'] = ($message['status'] != 'NEW' && $message['status'] != 'UNREAD');
$message = $data;
}
function oms_email_mark_read($mid) {
db_exec("update task set status = 'READ' where id_task = ? and type = 'email' and module = 'email'", array($mid));
}
function decode_utf8($str) {
preg_match_all("/=\?UTF-8\?B\?([^\?]+)\?=/i",$str, $arr);
for ($i=0;$i<count($arr[1]);$i++){
$str=ereg_replace(ereg_replace("\?","\?",
$arr[0][$i]),base64_decode($arr[1][$i]),$str);
}
return $str;
}
function get_mime_type(&$structure) {
$primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
if($structure->subtype) {
return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
}
return "TEXT/PLAIN";
}
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
if(!$structure) {
$structure = imap_fetchstructure($stream, $msg_number);
}
if($structure) {
if($mime_type == get_mime_type($structure)) {
if(!$part_number) {
$part_number = "1";
}
$text = imap_fetchbody($stream, $msg_number, $part_number);
if($structure->encoding == 3) {
return imap_base64($text);
} else if($structure->encoding == 4) {
return imap_qprint($text);
} else { } else {
$prefix = ''; return $text;
} }
$data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix . ($index + 1)); }
if($data) {
return $data; if($structure->type == 1) /* multipart */ {
} while(list($index, $sub_structure) = each($structure->parts)) {
} // END OF WHILE if($part_number) {
} // END OF MULTIPART $prefix = $part_number . '.';
} // END OF STRUTURE } else {
return false; $prefix = '';
}
} // END OF FUNCTION $data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix . ($index + 1));
if($data) {
return $data;
}
} // END OF WHILE
} // END OF MULTIPART
} // END OF STRUTURE
return false;
} // END OF FUNCTION
}

View file

@ -35,6 +35,17 @@ $cfg['mail']['username'] = '123';
$cfg['mail']['password'] = '456'; $cfg['mail']['password'] = '456';
$cfg['mail']['host'] = 'test.com'; $cfg['mail']['host'] = 'test.com';
// To connect to an IMAP server running on port 143 on the local machine,
// do the following: $mbox = imap_open("{localhost:143}INBOX", "user_id", "password");
// POP3 server on port 110: $mbox = imap_open ("{localhost:110/pop3}INBOX", "user_id", "password");
// SSL IMAP or POP3 server, add /ssl after the protocol: $mbox = imap_open ("{localhost:993/imap/ssl}INBOX", "user_id", "password");
// To connect to an SSL IMAP or POP3 server with a self-signed certificate,
// add /ssl/novalidate-cert after the protocol specification:
// $mbox = imap_open ("{localhost:995/pop3/ssl/novalidate-cert}", "user_id", "password");
// NNTP server on port 119 use: $nntp = imap_open ("{localhost:119/nntp}comp.test", "", "");
// To connect to a remote server replace "localhost" with the name or the IP address of the server you want to connect to.
$cfg['mail']['server'] = '{localhost:110/pop3/novalidate-cert}INBOX';
//----------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------
// If true= the server will add automatically unknown user in the database // If true= the server will add automatically unknown user in the database
// (in nel.user= nel.permission= ring.ring_user and ring.characters // (in nel.user= nel.permission= ring.ring_user and ring.characters