From e081677384b95082962b531b8ad4fe492181d8fe Mon Sep 17 00:00:00 2001
From: Ubbo Veentjer <veentjer@sub.uni-goettingen.de>
Date: Mon, 3 Dec 2012 14:29:31 +0000
Subject: [PATCH] mail with mime and smtp relay

git-svn-id: https://textgridlab.org/svn/textgrid/trunk/middleware/tgauth@13672 7c539038-3410-0410-b1ec-0f2a7bf1c452
---
 .../README.txt                                |  4 +-
 .../include/config.inc.php                    |  9 ++--
 .../include/tgImap.class.php                  | 43 ++++++++++++++++---
 .../install/tgaccount.ini.tmpl                |  5 ++-
 4 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/info.textgrid.middleware.tgauth.tgaccount/README.txt b/info.textgrid.middleware.tgauth.tgaccount/README.txt
index b047a00..7cc25c3 100644
--- a/info.textgrid.middleware.tgauth.tgaccount/README.txt
+++ b/info.textgrid.middleware.tgauth.tgaccount/README.txt
@@ -13,10 +13,10 @@ Installation:
 
   Required pear (http://pear.php.net) packages:
   
-    Auth
+    Auth, Mail, Mail_Mime
 
     $ sudo apt-get install php-pear
-    $ sudo pear install Auth
+    $ sudo pear install Auth Mail Mail_Mime
   
 2) Put source into your Webserver path (possibly /var/www)
 
diff --git a/info.textgrid.middleware.tgauth.tgaccount/include/config.inc.php b/info.textgrid.middleware.tgauth.tgaccount/include/config.inc.php
index 627bb6a..4fd0c7b 100644
--- a/info.textgrid.middleware.tgauth.tgaccount/include/config.inc.php
+++ b/info.textgrid.middleware.tgauth.tgaccount/include/config.inc.php
@@ -19,9 +19,12 @@ $conf['ldap']['managerdn'] = $ini['managerdn'];
 $conf['ldap']['managerpassword'] = $ini['managerpassword'];
 $conf['ldap']['debug'] = false;
 
-// imap
-$conf['imap']['sender'] = $ini['mailSender'];
-$conf['imap']['cc'] = $ini['mailCC'];
+// mail (imap)
+$conf['mail']['sender'] = $ini['mailSender'];
+$conf['mail']['cc'] = $ini['mailCC'];
+$conf['mail']['smtpHost'] = $ini['smtpHost'];
+$conf['mail']['smtpUser'] = $ini['smtpUser'];
+$conf['mail']['smtpEhlo'] = $ini['smtpEhlo'];
  
 // textgrid-sandbox
 $conf['sandbox'][0] = 'TGPR30';
diff --git a/info.textgrid.middleware.tgauth.tgaccount/include/tgImap.class.php b/info.textgrid.middleware.tgauth.tgaccount/include/tgImap.class.php
index f6fd920..ef12885 100644
--- a/info.textgrid.middleware.tgauth.tgaccount/include/tgImap.class.php
+++ b/info.textgrid.middleware.tgauth.tgaccount/include/tgImap.class.php
@@ -1,19 +1,29 @@
 <?php
 
+require_once('Mail.php');
+require_once('Mail/mime.php');
+
 class tgImap {
 
   var $conf;
   var $sender;
   var $cc;
 
-  function __construct( $conf) {
+  function __construct( $conf) {    
     $this->conf = $conf;
-    $this->sender = $conf['imap']['sender'];
-    $this->cc = $conf['imap']['cc'];
+    $this->sender = $conf['mail']['sender'];
+    $this->cc = $conf['mail']['cc'];
+
+    $params['host'] = $conf['mail']['smtpHost'];
+    $params['username'] = $conf['mail']['smtpUser'];
+    //$params['debug'] = true;
+    $params['localhost'] = $conf['mail']['smtpEhlo'];
+    $this->smtp =& Mail::factory('smtp', $params);
+    
   }
   
-  function mail($to, $subject, $body) {
-
+  function mail($to, $subject, $body, $cc=false) {
+/*
     // Falls eine Zeile der Nachricht mehr als 70 Zeichen enthälten könnte,
     // sollte wordwrap() benutzt werden (http://de.php.net/manual/de/function.mail.php)
     $body = wordwrap($body, 70);
@@ -29,8 +39,29 @@ class tgImap {
     // replace linebreaks on www.textgrid.de
     $body = str_replace("\r\n", "\n", $body);
     
-    mail($to, $subject, $body, $header);
+    mail($to, $subject, $body, $header);*/
+    
+    $text = $body;
+    $html = '<html><body>'.nl2br($body).'</body></html>';
+    $crlf = "\n";
+
+    $mime = new Mail_mime(array('eol' => $crlf));
+    $mime->setTXTBody($text);
+    $mime->setHTMLBody($html);
+
+    $headers['From']    = $this->sender;
+    $headers['To']      = $to;
+    $headers['Subject'] = $subject;
+    
+    if($cc) {
+      $headers['Cc:']     = $this->cc;
+    }
     
+    $body = $mime->get();
+    $headers = $mime->headers($headers);
+    
+    $this->smtp->send($to, $headers, $body);
+
   }
 
 }
diff --git a/info.textgrid.middleware.tgauth.tgaccount/install/tgaccount.ini.tmpl b/info.textgrid.middleware.tgauth.tgaccount/install/tgaccount.ini.tmpl
index 1ca7992..9d7d73f 100644
--- a/info.textgrid.middleware.tgauth.tgaccount/install/tgaccount.ini.tmpl
+++ b/info.textgrid.middleware.tgauth.tgaccount/install/tgaccount.ini.tmpl
@@ -13,8 +13,11 @@ managerdn = "cn=Manager,dc=textgrid,dc=de"
 managerpassword = 
 
 [mail]
-mailSender = register@textgrid.de
+mailSender = register@textgridlab.org
 mailCC = TEXTGRID-REGISTER@D-GRID.DE
+smtpHost = localhost
+smtpUser = register@textgridlab.org
+smtpEhlo = localhost
 
 [smarty]
 smartyLib = /usr/share/php/smarty/libs/Smarty.class.php
-- 
GitLab