mirror of
https://port.numenaute.org/aleajactaest/khanat-opennel-code.git
synced 2024-11-06 23:39:06 +00:00
updates to the mail_handler
This commit is contained in:
parent
6b4deea068
commit
784fd5ecea
2 changed files with 293 additions and 239 deletions
|
@ -1,33 +1,73 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
class Mail_Handler{
|
||||||
|
|
||||||
|
private $db;
|
||||||
|
|
||||||
function oms_mail_send($recipient, $subject, $body, $from = NULL) {
|
function mail_fork() {
|
||||||
|
/*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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
function oms_mail_send($recipient, $subject, $body, $from = NULL) {
|
||||||
|
|
||||||
if(is_numeric($recipient)) {
|
if(is_numeric($recipient)) {
|
||||||
$id_user = $recipient;
|
$id_user = $recipient;
|
||||||
$recipient = NULL;
|
$recipient = NULL;
|
||||||
}
|
}
|
||||||
db_insert('email', array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'id_user' => $id_user, 'sender' => $from));
|
db_insert('email', array('recipient' => $recipient, 'subject' => $subject, 'body' => $body, 'status' => 'NEW', 'id_user' => $id_user, 'sender' => $from));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_mail_cron() {
|
function mail_cron() {
|
||||||
|
global $cfg;
|
||||||
$oms_inbox_username = '123';
|
$inbox_username = $cfg['mail']['username'];
|
||||||
$oms_inbox_password = '456';
|
$inbox_password = $cfg['mail']['password'];
|
||||||
$oms_host = 'test.com';
|
$inbox_host = $cfg['mail']['host'];
|
||||||
$oms_reply_to = "OMS <oms@".$oms_host.">";
|
$oms_reply_to = "OMS <oms@".$inbox_host.">";
|
||||||
global $basedir;
|
global $basedir;
|
||||||
|
|
||||||
// Deliver new mail
|
// Deliver new mail
|
||||||
echo("mail cron\n");
|
echo("mail cron\n");
|
||||||
|
|
||||||
//TO ASK:
|
//TO ASK:
|
||||||
$pid = oms_fork();
|
$pid = mail_fork();
|
||||||
$pidfile = '/tmp/oms_cron_email_pid';
|
$pidfile = '/tmp/oms_cron_email_pid';
|
||||||
|
|
||||||
|
//if process is running, then we are already cronning
|
||||||
if($pid) {
|
if($pid) {
|
||||||
|
|
||||||
// We're the main process.
|
// We're the main process.
|
||||||
|
@ -40,6 +80,8 @@ function oms_mail_cron() {
|
||||||
$file = fopen($pidfile, 'w');
|
$file = fopen($pidfile, 'w');
|
||||||
fwrite($file, $pid);
|
fwrite($file, $pid);
|
||||||
fclose($file);
|
fclose($file);
|
||||||
|
|
||||||
|
//select all new & failed emails & try to send them
|
||||||
$emails = db_query("select * from email where status = 'NEW' or status = 'FAILED'");
|
$emails = db_query("select * from email where status = 'NEW' or status = 'FAILED'");
|
||||||
|
|
||||||
foreach($emails as $email) {
|
foreach($emails as $email) {
|
||||||
|
@ -51,7 +93,7 @@ function oms_mail_cron() {
|
||||||
|
|
||||||
if($email['sender']) {
|
if($email['sender']) {
|
||||||
$username = oms_get_username_from_id($email['sender']);
|
$username = oms_get_username_from_id($email['sender']);
|
||||||
$from = "$username <$username@$oms_host>";
|
$from = "$username <$username@$inbox_host>";
|
||||||
} else {
|
} else {
|
||||||
$from = $oms_reply_to;
|
$from = $oms_reply_to;
|
||||||
}
|
}
|
||||||
|
@ -64,14 +106,15 @@ function oms_mail_cron() {
|
||||||
$status = "FAILED";
|
$status = "FAILED";
|
||||||
echo("Email to {$email['recipient']} failed\n");
|
echo("Email to {$email['recipient']} failed\n");
|
||||||
}
|
}
|
||||||
|
//change the status of the emails.
|
||||||
db_exec('update email set status = ?, message_id = ?, attempts = attempts + 1 where id_email = ?', array($status, $message_id, $email['id_email']));
|
db_exec('update email set status = ?, message_id = ?, attempts = attempts + 1 where id_email = ?', array($status, $message_id, $email['id_email']));
|
||||||
}
|
}
|
||||||
unlink($pidfile);
|
unlink($pidfile);
|
||||||
}
|
}
|
||||||
// Check mail
|
// Check mail
|
||||||
|
|
||||||
//$mailbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $oms_inbox_username, $oms_inbox_password);
|
//$mailbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $inbox_username, $inbox_password);
|
||||||
$mbox = imap_open("{localhost:110/pop3/novalidate-cert}INBOX", $oms_inbox_username, $oms_inbox_password);
|
$mbox = imap_open($cfg['mail']['server'], $inbox_username, $inbox_password);
|
||||||
$message_count = imap_num_msg($mbox);
|
$message_count = imap_num_msg($mbox);
|
||||||
|
|
||||||
for ($i = 1; $i <= $message_count; ++$i) {
|
for ($i = 1; $i <= $message_count; ++$i) {
|
||||||
|
@ -96,11 +139,11 @@ function oms_mail_cron() {
|
||||||
imap_close($mbox);
|
imap_close($mbox);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_new_message_id() {
|
function oms_new_message_id() {
|
||||||
|
|
||||||
$time = time();
|
$time = time();
|
||||||
$pid = getmypid();
|
$pid = getmypid();
|
||||||
|
@ -108,11 +151,11 @@ function oms_new_message_id() {
|
||||||
$oms_mail_count = ($oms_mail_count == '') ? 1 : $oms_mail_count + 1;
|
$oms_mail_count = ($oms_mail_count == '') ? 1 : $oms_mail_count + 1;
|
||||||
return "<oms.message.$pid.$oms_mail_count.$time@dev1.dev.subrigo.net>";
|
return "<oms.message.$pid.$oms_mail_count.$time@dev1.dev.subrigo.net>";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
|
function oms_create_email($from, $subject, $body, $html, $recipient = 0, $sender = NULL) {
|
||||||
|
|
||||||
if($recipient == 0 && !is_string($recipient)) {
|
if($recipient == 0 && !is_string($recipient)) {
|
||||||
global $user;
|
global $user;
|
||||||
|
@ -146,11 +189,11 @@ function oms_create_email($from, $subject, $body, $html, $recipient = 0, $sender
|
||||||
oms_task_index($message, array('subject', 'body', 'sender', 'recipient'));
|
oms_task_index($message, array('subject', 'body', 'sender', 'recipient'));
|
||||||
//---------------------------
|
//---------------------------
|
||||||
return $message['id_task'];
|
return $message['id_task'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_get_email($id) {
|
function oms_get_email($id) {
|
||||||
|
|
||||||
$message = oms_task_load($id);
|
$message = oms_task_load($id);
|
||||||
if($message) {
|
if($message) {
|
||||||
|
@ -160,30 +203,30 @@ function oms_get_email($id) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_prepare_email(&$message) {
|
function oms_prepare_email(&$message) {
|
||||||
|
|
||||||
$data = $message['data'];
|
$data = $message['data'];
|
||||||
$data['id_message'] = $message['id_task'];
|
$data['id_message'] = $message['id_task'];
|
||||||
$data['read'] = ($message['status'] != 'NEW' && $message['status'] != 'UNREAD');
|
$data['read'] = ($message['status'] != 'NEW' && $message['status'] != 'UNREAD');
|
||||||
$message = $data;
|
$message = $data;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oms_email_mark_read($mid) {
|
function oms_email_mark_read($mid) {
|
||||||
|
|
||||||
db_exec("update task set status = 'READ' where id_task = ? and type = 'email' and module = 'email'", array($mid));
|
db_exec("update task set status = 'READ' where id_task = ? and type = 'email' and module = 'email'", array($mid));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function decode_utf8($str) {
|
function decode_utf8($str) {
|
||||||
|
|
||||||
preg_match_all("/=\?UTF-8\?B\?([^\?]+)\?=/i",$str, $arr);
|
preg_match_all("/=\?UTF-8\?B\?([^\?]+)\?=/i",$str, $arr);
|
||||||
for ($i=0;$i<count($arr[1]);$i++){
|
for ($i=0;$i<count($arr[1]);$i++){
|
||||||
|
@ -192,13 +235,13 @@ function decode_utf8($str) {
|
||||||
}
|
}
|
||||||
return $str;
|
return $str;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function get_mime_type(&$structure) {
|
function get_mime_type(&$structure) {
|
||||||
|
|
||||||
$primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
|
$primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
|
||||||
if($structure->subtype) {
|
if($structure->subtype) {
|
||||||
|
@ -206,11 +249,11 @@ function get_mime_type(&$structure) {
|
||||||
}
|
}
|
||||||
return "TEXT/PLAIN";
|
return "TEXT/PLAIN";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
|
function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
|
||||||
|
|
||||||
if(!$structure) {
|
if(!$structure) {
|
||||||
$structure = imap_fetchstructure($stream, $msg_number);
|
$structure = imap_fetchstructure($stream, $msg_number);
|
||||||
|
@ -247,6 +290,6 @@ function get_part($stream, $msg_number, $mime_type, $structure = false, $part_nu
|
||||||
} // END OF STRUTURE
|
} // END OF STRUTURE
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
} // END OF FUNCTION
|
} // END OF FUNCTION
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue