Hello,
Here's the newest version.
Basically it bridges XWiki.sendMessage to MailSenderPlugin.
It uses Reflection, so no circulars, but it's not exactly pretty.
Why it's needed - registration / validation / activation e-mails are
still sent via obsolete Apache Commons SmtpClient, so they _do not_
support SMTP AUTH.
This is a _major_ problem because due to spam and other abuse less and
less hosting providers / network admins allow SMTP without
authorization, and those who do are likely to have very insecure
infrastructure.
I honestly believe this is a MUST HAVE for a secure, spam-free
configuration.
Greetings, Lilianne
Index: main/resources/XWiki/AdminGeneralSheet.xml
===================================================================
--- main/resources/XWiki/AdminGeneralSheet.xml (revision 18064)
+++ main/resources/XWiki/AdminGeneralSheet.xml (working copy)
@@ -61,6 +61,6 @@
#set($params.language = ['multilingual', 'languages' ,
'default_language', 'dateformat'])
#set($params.editor = ['editor'])
#set($params.admin = ['admin_email'])
-#set($params.server = ['smtp_server'])
+#set($params.server = ['smtp_server', 'smtp_server_username',
'smtp_server_password', 'javamail_extra_props'])
#includeForm('XWiki.AdminFieldsDisplaySheet')</content>
</xwikidoc>
Index: main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- main/java/com/xpn/xwiki/XWiki.java (revision 17953)
+++ main/java/com/xpn/xwiki/XWiki.java (working copy)
@@ -161,6 +161,8 @@
import com.xpn.xwiki.web.XWikiURLFactoryService;
import com.xpn.xwiki.web.XWikiURLFactoryServiceImpl;
import com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString;
+import java.io.BufferedReader;
+import java.io.StringReader;
public class XWiki implements XWikiDocChangeNotificationInterface
{
@@ -2838,9 +2840,13 @@
needsUpdate |= bclass.addTextAreaField("meta", "HTTP Meta
Info", 60, 8);
needsUpdate |= bclass.addTextField("dateformat", "Date
Format", 30);
+ // mail
needsUpdate |= bclass.addBooleanField("use_email_verification",
"Use eMail Verification", "yesno");
+ needsUpdate |= bclass.addTextField("admin_email", "Admin
eMail", 30);
needsUpdate |= bclass.addTextField("smtp_server", "SMTP
Server", 30);
- needsUpdate |= bclass.addTextField("admin_email", "Admin
eMail", 30);
+ needsUpdate |= bclass.addTextField("smtp_server_username", "SMTP
Server username (optional)", 30);
+ needsUpdate |= bclass.addTextField("smtp_server_password", "SMTP
Server password (optional)", 30);
+ needsUpdate |= bclass.addTextAreaField("javamail_extra_props",
"Additional JavaMail properties", 60, 6);
needsUpdate |= bclass.addTextAreaField("validation_email_content",
"Validation eMail Content", 72, 10);
needsUpdate |= bclass.addTextAreaField("confirmation_email_content",
"Confirmation eMail Content", 72, 10);
needsUpdate |= bclass.addTextAreaField("invitation_email_content",
"Invitation eMail Content", 72, 10);
@@ -3311,9 +3317,139 @@
* Plugin</a>
*/
@Deprecated
- public void sendMessage(String sender, String[] recipient, String message,
XWikiContext context)
+ public void sendMessage(String sender, String[] recipients, String message,
XWikiContext context)
throws XWikiException
{
+ LOG.info("Entering sendMessage(...)...");
+
+ Object mailSender;
+ Class mailSenderClass;
+ Method mailSenderSendText;
+
+ try
+ {
+ mailSender = getPluginApi("mailsender", context);
+ mailSenderClass =
Class.forName("com.xpn.xwiki.plugin.mailsender.MailSenderPluginApi");
+
+ // public int sendTextMessage(String from, String to, String subject, String
message)
+ mailSenderSendText = mailSenderClass.getMethod("sendTextMessage",
+ new Class[]{String.class, String.class, String.class,
String.class});
+ }
+ catch(Exception e)
+ {
+ String eMsg = "Problem getting MailSender via Reflection: " + e;
+ LOG.error(eMsg);
+ throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL,
+ XWikiException.ERROR_XWIKI_EMAIL_ERROR_SENDING_EMAIL, eMsg);
+ }
+
+ LOG.trace("Message = \"" + message + "\"");
+
+ String messageParsed[] = parseRawMessage(message);
+ String messageSubject = messageParsed[0];
+ String messageBody = messageParsed[1];
+ String messageRecipients = recipients[0];
+ for( int i = 1; i < recipients.length; i++)
+ {
+ messageRecipients = messageRecipients + "," + recipients[i];
+ }
+
+ if( messageSubject == null )
+ {
+ // TODO: provide some sensible default
+ messageSubject = "Message from XWiki";
+ }
+
+ LOG.trace("Subject = \"" + messageParsed[0] +
"\"");
+ LOG.trace("Text = \"" + messageParsed[1] + "\"");
+
+ try
+ {
+ mailSenderSendText.invoke(mailSender, sender, messageRecipients,
messageSubject, messageBody);
+ }
+ catch(InvocationTargetException ite)
+ {
+ Throwable cause = ite.getCause();
+ if( cause instanceof XWikiException )
+ {
+ throw (XWikiException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ catch(Exception e)
+ {
+ // probably either IllegalAccessException or IllegalArgumentException
+ // shouldn't happen unless there were an incompatible code change
+ throw new RuntimeException(e);
+ }
+
+ LOG.info("Exiting sendMessage(...). It seems everything went ok.");
+ }
+
+ /**
+ *
+ * @return [subject (can be null), text (never null)]
+ */
+ public String[] parseRawMessage(String rawMessage)
+ {
+ String SUBJECT = "Subject: ";
+
+ String messageSubject = null;
+ String messageText = "";
+
+ // sanity check
+ if( rawMessage == null )
+ {
+ throw new IllegalArgumentException("rawMessage can't be null");
+ }
+ else if( rawMessage.trim().equals("") )
+ {
+ throw new IllegalArgumentException("rawMessage can't be empty");
+ }
+
+ try
+ {
+ StringReader sr = new StringReader(rawMessage);
+ BufferedReader br = new BufferedReader(sr);
+ String line;
+
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+
+ line = br.readLine();
+ if( line.startsWith(SUBJECT) )
+ {
+ messageSubject = line.substring(SUBJECT.length());
+ line = br.readLine();
+ if( !line.trim().equals("") )
+ {
+ pw.println(line);
+ }
+ }
+
+ while( (line = br.readLine()) != null )
+ {
+ pw.println(line);
+ }
+
+ messageText = sw.toString();
+ }
+ catch(IOException ioe)
+ {
+ // can't happen here
+ }
+
+ return new String[]{messageSubject, messageText};
+ }
+
+
+ @Deprecated
+ public void sendMessageOld(String sender, String[] recipient, String message,
XWikiContext context)
+ throws XWikiException
+ {
SMTPClient smtpc = null;
try {
String server = getXWikiPreference("smtp_server", context);
Index: main/resources/ApplicationResources.properties
===================================================================
--- main/resources/ApplicationResources.properties (revision 17953)
+++ main/resources/ApplicationResources.properties (working copy)
@@ -181,6 +181,9 @@
use_email_verification=Use email verification
admin_email=Admin email
smtp_server=Outgoing SMTP Server
+smtp_server_username=SMTP Server Username (optional)
+smtp_server_password=SMTP Server Password (optional)
+javamail_extra_props=Additional JavaMail properties
validation_email_content=Validation e-Mail Content
confirmation_email_content=Confirmation e-Mail Content
preferences=Preferences