← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10687: Made email configuration more flexible. You can now specify smpt port number and whether to use TLS.

 

------------------------------------------------------------
revno: 10687
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-04-25 11:07:24 +0200
message:
  Made email configuration more flexible. You can now specify smpt port number and whether to use TLS.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/GetSMTPSettingsAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetSMTPSettingsAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/resources/org/hisp/dhis/settings/i18n_module.properties
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemEmailSettings.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java	2013-03-09 15:25:42 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java	2013-04-25 09:07:24 +0000
@@ -60,8 +60,10 @@
     final String KEY_DATAMART_TASK = "keyDataMartTask";
     final String KEY_DATASETCOMPLETENESS_TASK = "keyDataSetCompletenessTask";
     final String KEY_EMAIL_HOST_NAME = "keyEmailHostName";
+    final String KEY_EMAIL_PORT = "keyEmailPort";
     final String KEY_EMAIL_USERNAME = "keyEmailUsername";
     final String KEY_EMAIL_PASSWORD = "keyEmailPassword";
+    final String KEY_EMAIL_TLS = "keyEmailTls";
     final String KEY_SCHEDULED_PERIOD_TYPES = "keyScheduledPeriodTypes";
     final String KEY_SCHEDULED_TASKS = "keySchedTasks";
     final String KEY_ORGUNITGROUPSET_AGG_LEVEL = "orgUnitGroupSetAggregationLevel";
@@ -86,6 +88,7 @@
     final String DEFAULT_GOOGLE_MAPS_API_KEY = "ABQIAAAAut6AhySExnYIXm5s2OFIkxRKNzJ-_9njnryRTbvC6CtrS4sRvRREWnxwlZUa630pLuPf3nD9i4fq9w";
     final String DEFAULT_START_MODULE = "dhis-web-dashboard-integration";
     final String DEFAULT_APPLICATION_TITLE = "District Health Information Software 2";
+    final int DEFAULT_EMAIL_PORT = 587;
     
     final int DEFAULT_COMPLETENESS_OFFSET = 15;
     final String DEFAULT_TIME_FOR_SENDING_MESSAGE = "08:00";    
@@ -114,10 +117,14 @@
     
     String getEmailHostName();
     
+    int getEmailPort();
+    
     String getEmailUsername();
     
     String getEmailPassword();
     
+    boolean getEmailTls();
+    
     boolean accountRecoveryEnabled();
     
     boolean emailEnabled();

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2012-12-11 21:20:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2013-04-25 09:07:24 +0000
@@ -27,10 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_HOST_NAME;
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_PASSWORD;
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_USERNAME;
-import static org.hisp.dhis.user.UserSettingService.*;
+import static org.hisp.dhis.user.UserSettingService.KEY_MESSAGE_EMAIL_NOTIFICATION;
 
 import java.io.Serializable;
 import java.util.Map;
@@ -55,12 +52,9 @@
     implements MessageSender
 {
     private static final Log log = LogFactory.getLog( EmailMessageSender.class );
-    private static final int SMTP_PORT = 587;
-    private static final int LOCAL_SMTP_PORT = 25;
     private static final String FROM_ADDRESS = "noreply@xxxxxxxxx";
     private static final String FROM_NAME = "DHIS2 Message [No reply]";
     private static final String SUBJECT_PREFIX = "[DHIS2] ";
-    private static final String LOCALHOST = "localhost";
     private static final String LB = System.getProperty( "line.separator" );
 
     // -------------------------------------------------------------------------
@@ -92,11 +86,13 @@
     @Override
     public void sendMessage( String subject, String text, User sender, Set<User> users, boolean forceSend )
     {        
-        String hostName = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_HOST_NAME ) );
-        String username = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_USERNAME ) );
-        String password = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_PASSWORD ) );
+        String hostName = systemSettingManager.getEmailHostName();
+        int port = systemSettingManager.getEmailPort();
+        String username = systemSettingManager.getEmailUsername();
+        String password = systemSettingManager.getEmailPassword();
+        boolean tls = systemSettingManager.getEmailTls();
 
-        if ( hostName == null || username == null || password == null )
+        if ( hostName == null )
         {
             return;
         }
@@ -111,7 +107,7 @@
 
         try
         {
-            Email email = getEmail( hostName, username, password );
+            Email email = getEmail( hostName, port, username, password, tls );
             email.setSubject( SUBJECT_PREFIX + subject );
             email.setMsg( text );
             
@@ -127,7 +123,7 @@
                 {
                     email.addBcc( user.getEmail() );
                     
-                    log.debug( "Sending email to user: " + user + " with email address: " + user.getEmail() );
+                    log.info( "Sending email to user: " + user + " with email address: " + user.getEmail() );
                     
                     hasRecipients = true;
                 }
@@ -137,7 +133,7 @@
             {
                 email.send();
                 
-                log.debug( "Email sent" );
+                log.info( "Email sent using host: " + hostName + " with TLS: " + tls );
             }
         }
         catch ( EmailException ex )
@@ -146,24 +142,20 @@
         }
     }
 
-    private Email getEmail( String hostName, String username, String password )
+    private Email getEmail( String hostName, int port, String username, String password, boolean tls )
         throws EmailException
     {
         Email email = new SimpleEmail();
         email.setHostName( hostName );
         email.setFrom( FROM_ADDRESS, FROM_NAME );
-
-        if ( hostName.equals( LOCALHOST ) )
-        {
-            email.setSmtpPort( LOCAL_SMTP_PORT );
-        }
-        else
-        {
-            email.setSmtpPort( SMTP_PORT );
+        email.setSmtpPort( port );
+        email.setTLS( true );
+        
+        if ( username != null && password != null )
+        {
             email.setAuthenticator( new DefaultAuthenticator( username, password ) );
-            email.setTLS( true );
         }
-
+        
         return email;
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java	2012-12-07 14:32:11 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java	2013-04-25 09:07:24 +0000
@@ -137,6 +137,11 @@
     {
         return StringUtils.trimToNull( (String) getSystemSetting( KEY_EMAIL_HOST_NAME ) );
     }
+    
+    public int getEmailPort()
+    {
+        return (Integer) getSystemSetting( KEY_EMAIL_PORT, DEFAULT_EMAIL_PORT );
+    }
 
     public String getEmailPassword()
     {
@@ -148,6 +153,11 @@
         return StringUtils.trimToNull( (String) getSystemSetting( KEY_EMAIL_USERNAME ) );
     }
     
+    public boolean getEmailTls()
+    {
+        return (Boolean) getSystemSetting( KEY_EMAIL_TLS, true );
+    }
+    
     public boolean accountRecoveryEnabled()
     {
         return (Boolean) getSystemSetting( KEY_ACCOUNT_RECOVERY, false );

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css	2013-03-17 14:36:26 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css	2013-04-25 09:07:24 +0000
@@ -775,3 +775,12 @@
     background-color: #f3f3f3;
 	color: #222;
 }
+
+/*----------------------------------------------------------------------------*/
+/* Div                                                                        */
+/*----------------------------------------------------------------------------*/
+
+.tipText
+{
+	color: #777;
+}

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/GetSMTPSettingsAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/GetSMTPSettingsAction.java	2012-02-02 20:01:36 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/GetSMTPSettingsAction.java	2013-04-25 09:07:24 +0000
@@ -59,6 +59,13 @@
     {
         return smtpHostName;
     }
+    
+    private int smtpPort;
+
+    public int getSmtpPort()
+    {
+        return smtpPort;
+    }
 
     private String smtpUsername;
 
@@ -73,6 +80,13 @@
     {
         return smtpPassword;
     }
+    
+    private boolean smtpTls;
+
+    public boolean isSmtpTls()
+    {
+        return smtpTls;
+    }
 
     // -------------------------------------------------------------------------
     // Action implementation
@@ -82,10 +96,14 @@
     {
         smtpHostName = systemSettingManager.getEmailHostName();
         
+        smtpPort = systemSettingManager.getEmailPort();
+        
         smtpPassword = systemSettingManager.getEmailPassword();
         
         smtpUsername = systemSettingManager.getEmailUsername();
 
+        smtpTls = systemSettingManager.getEmailTls();
+        
         return SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetSMTPSettingsAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetSMTPSettingsAction.java	2012-02-02 20:01:36 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/java/org/hisp/dhis/settings/action/system/SetSMTPSettingsAction.java	2013-04-25 09:07:24 +0000
@@ -26,9 +26,8 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_HOST_NAME;
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_PASSWORD;
-import static org.hisp.dhis.setting.SystemSettingManager.KEY_EMAIL_USERNAME;
+
+import static org.hisp.dhis.setting.SystemSettingManager.*;
 
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.setting.SystemSettingManager;
@@ -64,6 +63,13 @@
         this.smtpHostName = hostName;
     }
 
+    private int smtpPort;
+
+    public void setSmtpPort( int smtpPort )
+    {
+        this.smtpPort = smtpPort;
+    }
+
     private String smtpUsername;
 
     public void setSmtpUsername( String username )
@@ -78,6 +84,13 @@
         this.smtpPassword = password;
     }
 
+    private boolean smtpTls;
+
+    public void setSmtpTls( boolean smtpTls )
+    {
+        this.smtpTls = smtpTls;
+    }
+
     private String message;
 
     public String getMessage()
@@ -100,10 +113,14 @@
     {
         systemSettingManager.saveSystemSetting( KEY_EMAIL_HOST_NAME, smtpHostName );
 
+        systemSettingManager.saveSystemSetting( KEY_EMAIL_PORT, smtpPort );
+        
         systemSettingManager.saveSystemSetting( KEY_EMAIL_PASSWORD, smtpPassword );
 
         systemSettingManager.saveSystemSetting( KEY_EMAIL_USERNAME, smtpUsername );
 
+        systemSettingManager.saveSystemSetting( KEY_EMAIL_TLS, smtpTls );
+        
         message = i18n.getString( "settings_updated" );
 
         return SUCCESS;

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/resources/org/hisp/dhis/settings/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/resources/org/hisp/dhis/settings/i18n_module.properties	2013-03-21 14:07:18 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/resources/org/hisp/dhis/settings/i18n_module.properties	2013-04-25 09:07:24 +0000
@@ -58,3 +58,5 @@
 select_organisation_unit=Select organisation unit
 application_notification=Application notification
 multi_organisation_unit_forms=Enable multi-organisation unit forms
+port=Port
+tls=TLS
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemEmailSettings.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemEmailSettings.vm	2012-10-22 18:48:19 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemEmailSettings.vm	2013-04-25 09:07:24 +0000
@@ -22,17 +22,32 @@
 
 <form id="emailSettingForm">
 
-<div class="settingLabel">$i18n.getString( "host_name" )</div>
-
-<div class="setting"><input type="text" id="smtpHostName" name="smtpHostName" style="width:20em" value="$!smtpHostName"/></div>
+<div class="settingLabel">$i18n.getString( "host_name" ) <span class="tipText">(e.g. smtp.gmail.com, localhost)</span></div>
+
+<div class="setting"><input type="text" id="smtpHostName" name="smtpHostName" value="$!smtpHostName"/></div>
+
+<div class="settingLabel">$i18n.getString( "port" )</div>
+
+<div class="setting">
+    <select id="smtpPort" name="smtpPort">
+    	<option value="587"#if( $smtpPort == 587 ) selected="selected"#end>587</option>
+    	<option value="465"#if( $smtpPort == 465 ) selected="selected"#end>465</option>
+    	<option value="25"#if( $smtpPort == 25 ) selected="selected"#end>25</option>
+    </select>
+</div>
 
 <div class="settingLabel">$i18n.getString( "username" )</div>
 
-<div class="setting"><input type="text" id="smtpUsername" name="smtpUsername" value="$!smtpUsername" style="width:20em" autocomplete="off"/></div>
+<div class="setting"><input type="text" id="smtpUsername" name="smtpUsername" value="$!smtpUsername" autocomplete="off"/></div>
 
 <div class="settingLabel">$i18n.getString( "password" )</div>
 
-<div class="setting"><input type="password" id="smtpPassword" name="smtpPassword" value="$!smtpPassword" style="width:20em" autocomplete="off"/></div>
+<div class="setting"><input type="password" id="smtpPassword" name="smtpPassword" value="$!smtpPassword" autocomplete="off"/></div>
+
+<div class="setting">
+	<input type="checkbox" id="smtpTls" name="smtpTls"#if( $smtpTls ) checked="checked"#end>
+	<label for="smtpTls">$i18n.getString( "tls" )</label>
+</div>
 
 <div class="setting"><input type="submit" value="$i18n.getString( 'save' )" style="width:10em"/></div>
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm	2013-03-20 08:47:25 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm	2013-04-25 09:07:24 +0000
@@ -56,7 +56,7 @@
 <div class="settingLabel">$i18n.getString( "feedback_recipients" )</div>
 
 <div class="setting">
-<select id="feedbackRecipients" name="feedbackRecipients" style="width:25em">
+<select id="feedbackRecipients" name="feedbackRecipients">
 	<option value="-1">$i18n.getString( "no_feedback_recipients" )</option>
 	#foreach ( $group in $userGroups )
 	<option value="$group.id" #if( $group.id == $!feedbackRecipients.id )selected="selected"#end>$encoder.htmlEncode( $group.name )</option>