Browse Source

Better email handling.

Nathaniel van Diepen 10 years ago
parent
commit
d34d54cbac
8 changed files with 88 additions and 9 deletions
  1. 18 3
      api.php
  2. 14 0
      data/emails/newissue.template
  3. 14 0
      data/emails/newproject.template
  4. 3 3
      data/emails/welcome.template
  5. 24 2
      php/emails.php
  6. 13 0
      php/functions.php
  7. 1 0
      php/include.php
  8. 1 1
      php/project.php

+ 18 - 3
api.php

@@ -302,7 +302,10 @@
 											if(addUser($_GET['username'],$_GET['password'],$_GET['email'])){
 												$key = login($_GET['username'],$_GET['password']);
 												$_SESSION['username'] = $_GET['username'];
-												sendMail('welcome','Welcome!',$_GET['email'],get('email'),array($_GET['username'],$_GET['password'],get('email')));
+												sendMail('welcome','Welcome!',$_GET['email'],get('email'),array(
+													'username'=>$_GET['username'],
+													'password'=>$_GET['password'],
+													'email'=>get('email')));
 											}else{
 												$ret['error'] = "Could not add user. ".get_sql()->error;
 											}
@@ -328,7 +331,13 @@
 								if(isset($_GET['pid'])){
 									$ret['error'] = 'Invalid Action';
 								}elseif(is_valid('title')&&is_valid('description')){
-									if(!newProject($_GET['title'],$_GET['description'])){
+									if($pid = newProject($_GET['title'],$_GET['description'])){
+										sendMailAll('newproject','New Project - '.$_GET['title'],array(
+											'title'=>$_GET['title'],
+											'url'=>'http://'.$_SERVER['HTTP_HOST'],
+											'id'=>$pid
+										));
+									}else{
 										$ret['error'] = 'Unable to create project. '.get_sql()->error;
 									}
 								}else{
@@ -347,7 +356,13 @@
 								if(isset($_GET['pid'])){
 									$ret['error'] = 'Invalid Action';
 								}elseif(is_valid('title')&&is_valid('description')){
-									if(!newIssue($_GET['title'],$_GET['description'])){
+									if($id = newIssue($_GET['title'],$_GET['description'])){
+										sendMailAll('newissue','New Issue - '.$_GET['title'],array(
+											'title'=>$_GET['title'],
+											'url'=>'http://'.$_SERVER['HTTP_HOST'],
+											'id'=>$id
+										));
+									}else{
 										$ret['error'] = 'Unable to create issue. '.get_sql()->error;
 									}
 								}else{

+ 14 - 0
data/emails/newissue.template

@@ -0,0 +1,14 @@
+<html>
+	<head>
+		<title>
+			New Issue
+		</title>
+	</head>
+	<body>
+		<p>
+			The project %title$s was created.
+			<br/>
+			<a href="%url$s/!%id$d">Open</a>
+		</p>
+	</body>
+</html>

+ 14 - 0
data/emails/newproject.template

@@ -0,0 +1,14 @@
+<html>
+	<head>
+		<title>
+			New Project
+		</title>
+	</head>
+	<body>
+		<p>
+			The project %title$s was created.
+			<br/>
+			<a href="%url$s/project-%id$d">Open</a>
+		</p>
+	</body>
+</html>

+ 3 - 3
data/emails/welcome.template

@@ -7,9 +7,9 @@
 	<body>
 		<p>
 			Welcome to bugs! Information for your account is as follows:<br/>
-			Username: %s<br/>
-			Password: %s<br/>
-			If this is not your information please let the webmaster know at: %s
+			Username: %username$s<br/>
+			Password: %password$s<br/>
+			If this is not your information please let the webmaster know at: %email$s
 		</p>
 	</body>
 </html>

+ 24 - 2
php/emails.php

@@ -3,7 +3,7 @@
 	require_once(PATH_PHP.'database.php');
 	require_once(PATH_PHP.'security.php');
 	require_once(PATH_PHP.'user.php');
-	function sendMail($template,$subject,$to=null,$from=null,$context=Array()){
+	function sendMail($template,$subject,$to=null,$from=null,$context=array()){
 		if(is_null($to)){
 			$to = get('email');
 		}
@@ -11,10 +11,32 @@
 			$from = get('email');
 		}
 		if(file_exists(PATH_DATA."/emails/{$template}.template")){
-			$message = vsprintf(file_get_contents(PATH_DATA."/emails/{$template}.template"),$context);
+			$message = vnsprintf(file_get_contents(PATH_DATA."/emails/{$template}.template"),$context);
 			return @mail($to,$subject,$message,"From: {$from}\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
 		}else{
 			return false;
 		}
 	}
+	function sendMailAll($template,$subject,$context=array()){
+		if($res = query("SELECT id FROM `users`")){
+			while($user = $res->fetch_assoc()){
+				$userobj = userObj((int)$user['id']);
+				foreach($userobj as $k => $val){
+					$context['user'.$k] = $val;
+				}
+				query("INSERT INTO `emails` (to_id,subject,template,context) VALUES (%d,'%s','%s','%s')",array($user['id'],$subject,$template,json_encode($context)));
+			}
+		}
+	}
+	function sendFromQueue($limit = 5){
+		if($res = query("SELECT e.id,u.email,e.subject,e.template,e.context FROM `emails` e JOIN `users` u ON u.id = e.to_id LIMIT %d",array($limit))){
+			while($email = $res->fetch_assoc()){
+				if(sendMail($email['template'],$email['subject'],$email['email'],get('email'),json_decode($email['context'],true))){
+					query("DELETE FROM `emails` WHERE id = %d",array($email['id']));
+				}else{
+					query("UPDATE `emails` SET failed=1 WHERE id = %d",array($email['id']));
+				}
+			}
+		}
+	}
 ?>

+ 13 - 0
php/functions.php

@@ -143,4 +143,17 @@
 		$json['title'] = $title;
 		return $json;
 	}
+	function vnsprintf($format,array $data){
+		preg_match_all('/ (?<!%) % ( (?: [[:alpha:]_-][[:alnum:]_-]* | ([-+])? [0-9]+ (?(2) (?:\.[0-9]+)? | \.[0-9]+ ) ) ) \$ [-+]? \'? .? -? [0-9]* (\.[0-9]+)? \w/x', $format, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+		$offset = 0;
+		$keys = array_keys($data);
+		foreach( $match as &$value ){
+			if(($key = array_search( $value[1][0], $keys, TRUE) ) !== FALSE || ( is_numeric( $value[1][0] ) && ( $key = array_search( (int)$value[1][0], $keys, TRUE) ) !== FALSE) ){
+			$len = strlen( $value[1][0]);
+			$format = substr_replace( $format, 1 + $key, $offset + $value[1][1], $len);
+			$offset -= $len - strlen( 1 + $key);
+			}
+		}
+		return vsprintf( $format, $data);
+	}
 ?>

+ 1 - 0
php/include.php

@@ -19,4 +19,5 @@
 	require_once(PATH_PHP.'issue.php');
 	require_once(PATH_PHP.'emails.php');
 	authenticate();
+	sendFromQueue();
 ?>

+ 1 - 1
php/project.php

@@ -32,7 +32,7 @@
 					$id = mysqli_insert_id(get_sql());
 					project_comment($id,'Project created');
 					alog('p',$id,"Project created");
-					return true;
+					return $id;
 				}
 			}
 		}