Update of
/var/cvs/contributions/CMSContainer_Portlets/portlets-dynamicforms/src/java/com/finalist/portlets/responseform
In directory
james.mmbase.org:/tmp/cvs-serv3681/portlets-dynamicforms/src/java/com/finalist/portlets/responseform
Modified Files:
Tag: b1_4
EmailSender.java ResponseFormPortlet.java
Log Message:
CMSC-1015 Dynamic forms send emails which are marked as spam based on dns
spf records
See also:
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer_Portlets/portlets-dynamicforms/src/java/com/finalist/portlets/responseform
See also: http://www.mmbase.org/jira/browse/CMSC-1015
Index: EmailSender.java
===================================================================
RCS file:
/var/cvs/contributions/CMSContainer_Portlets/portlets-dynamicforms/src/java/com/finalist/portlets/responseform/EmailSender.java,v
retrieving revision 1.2
retrieving revision 1.2.2.1
diff -u -b -r1.2 -r1.2.2.1
--- EmailSender.java 26 Nov 2007 11:42:56 -0000 1.2
+++ EmailSender.java 14 Jul 2008 11:45:10 -0000 1.2.2.1
@@ -11,11 +11,10 @@
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import net.sf.mmapps.commons.util.StringUtil;
+import javax.mail.internet.*;
+
+import org.apache.commons.lang.StringUtils;
+
import com.finalist.cmsc.mmbase.PropertiesUtil;
/**
@@ -23,11 +22,12 @@
*
* @author Cati Macarov
*/
-public class EmailSender {
+public final class EmailSender {
- private static Properties props = new Properties();
private static EmailSender instance = null;
- private static String mailHost = null;
+
+ private Properties props = new Properties();
+ private String mailHost = null;
/**
@@ -46,28 +46,49 @@
*/
public static synchronized EmailSender getInstance() {
String tempMailHost = PropertiesUtil.getProperty("mail.smtp.host");
- if ((tempMailHost != null) && (!tempMailHost.equals(mailHost))) {
+ if (instance == null) {
instance = new EmailSender(tempMailHost);
}
- if (instance == null) {
+ else {
+ if ((tempMailHost != null) &&
(!tempMailHost.equals(instance.mailHost))) {
instance = new EmailSender(tempMailHost);
}
+ }
+
return instance;
}
- /*
- * @param emailFrom The email address of the sender @param nameFrom The name
- * of the sender @param toAddresses The list of email addresses of the
- * receivers @param subject The subject of the email @param body The body of
- * the email @param fileName The name of the attachment
+ /**
+ * @param emailFrom The email address of the sender
+ * @param nameFrom The name of the sender
+ * @param toAddresses The list of email addresses of the receivers
+ * @param subject The subject of the email
+ * @param body The body of the email @param fileName The name of the
attachment
+ * @param attachment Binary part to add to the email message
*/
public void sendEmail(String emailFrom, String nameFrom, List<String>
toAddresses, String subject, String body,
- DataSource dataSource) throws UnsupportedEncodingException,
MessagingException {
- if (StringUtil.isEmptyOrWhitespace(emailFrom)) {
+ DataSource attachment) throws UnsupportedEncodingException,
MessagingException {
+ sendEmail(emailFrom, nameFrom, toAddresses, subject, body, attachment,
null);
+ }
+
+
+ /**
+ * @param emailFrom The email address of the sender
+ * @param nameFrom The name of the sender
+ * @param toAddresses The list of email addresses of the receivers
+ * @param subject The subject of the email
+ * @param body The body of the email @param fileName The name of the
attachment
+ * @param attachment Binary part to add to the message
+ * @param replyTo Address as reply-to header in the message
+ */
+ public void sendEmail(String emailFrom, String nameFrom, List<String>
toAddresses,
+ String subject, String body, DataSource attachment, String replyTo)
+ throws MessagingException, UnsupportedEncodingException,
AddressException {
+ if (StringUtils.isBlank(emailFrom)) {
emailFrom = PropertiesUtil.getProperty("mail.system.email");
}
- if (StringUtil.isEmptyOrWhitespace(nameFrom)) {
+ if (StringUtils.isBlank(nameFrom)) {
nameFrom = PropertiesUtil.getProperty("mail.system.name");
}
Session session = Session.getInstance(props, null);
@@ -79,7 +100,18 @@
addresses[i] = new InternetAddress(toAddresses.get(i));
}
message.addRecipients(Message.RecipientType.TO, addresses);
+
+ if (StringUtils.isNotBlank(replyTo)) {
+ message.setReplyTo(InternetAddress.parse(replyTo));
+ }
+
message.setSubject(subject);
+ Multipart multipart = createMultiPart(body, attachment);
+ message.setContent(multipart);
+ Transport.send(message);
+ }
+
+ private Multipart createMultiPart(String body, DataSource dataSource)
throws MessagingException {
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
@@ -94,28 +126,47 @@
messageBodyPart.setFileName(dataSource.getName());
multipart.addBodyPart(messageBodyPart);
}
- message.setContent(multipart);
- Transport.send(message);
+ return multipart;
}
- /*
- * @param emailFrom The email address of the sender @param nameFrom The name
- * of the sender @param emailTo The email address of the receiver @param
- * subject The subject of the email @param body The body of the email
+ /**
+ * @param emailFrom The email address of the sender
+ * @param nameFrom The name of the sender
+ * @param emailTo The email address of the receiver
+ * @param subject The subject of the email
+ * @param body The body of the email
*/
public void sendEmail(String emailFrom, String nameFrom, String emailTo,
String subject, String body)
throws UnsupportedEncodingException, MessagingException {
- if (StringUtil.isEmptyOrWhitespace(emailFrom)) {
+ sendEmail(emailFrom, nameFrom, emailTo, subject, body, null);
+ }
+
+
+ /**
+ * @param emailFrom The email address of the sender
+ * @param nameFrom The name of the sender
+ * @param emailTo The email address of the receiver
+ * @param subject The subject of the email
+ * @param body The body of the email
+ * @param replyTo Address as reply-to header in the message
+ */
+ public void sendEmail(String emailFrom, String nameFrom, String emailTo,
String subject,
+ String body, String replyTo) throws MessagingException,
UnsupportedEncodingException,
+ AddressException {
+ if (StringUtils.isBlank(emailFrom)) {
emailFrom = PropertiesUtil.getProperty("mail.system.email");
}
- if (StringUtil.isEmptyOrWhitespace(nameFrom)) {
+ if (StringUtils.isBlank(nameFrom)) {
nameFrom = PropertiesUtil.getProperty("mail.system.name");
}
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom, nameFrom));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(emailTo));
+ if (StringUtils.isNotBlank(replyTo)) {
+ message.setReplyTo(InternetAddress.parse(replyTo));
+ }
message.setSubject(subject);
message.setText(body);
Transport.send(message);
Index: ResponseFormPortlet.java
===================================================================
RCS file:
/var/cvs/contributions/CMSContainer_Portlets/portlets-dynamicforms/src/java/com/finalist/portlets/responseform/ResponseFormPortlet.java,v
retrieving revision 1.8.2.5
retrieving revision 1.8.2.6
diff -u -b -r1.8.2.5 -r1.8.2.6
--- ResponseFormPortlet.java 4 Jul 2008 16:23:28 -0000 1.8.2.5
+++ ResponseFormPortlet.java 14 Jul 2008 11:45:10 -0000 1.8.2.6
@@ -11,41 +11,22 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
-import java.util.Arrays;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import javax.activation.DataSource;
import javax.mail.MessagingException;
-import javax.portlet.ActionRequest;
-import javax.portlet.ActionResponse;
-import javax.portlet.PortletException;
-import javax.portlet.PortletMode;
-import javax.portlet.PortletPreferences;
-import javax.portlet.PortletSession;
-import javax.portlet.RenderRequest;
-import javax.portlet.RenderResponse;
+import javax.portlet.*;
import net.sf.mmapps.commons.bridge.RelationUtil;
-import org.apache.commons.lang.StringUtils;
import net.sf.mmapps.modules.cloudprovider.CloudProvider;
import net.sf.mmapps.modules.cloudprovider.CloudProviderFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.portlet.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-
-import org.mmbase.bridge.Cloud;
-import org.mmbase.bridge.Node;
-import org.mmbase.bridge.NodeIterator;
-import org.mmbase.bridge.NodeList;
-import org.mmbase.bridge.NodeManager;
+import org.apache.commons.fileupload.portlet.PortletFileUpload;
+import org.apache.commons.lang.StringUtils;
+import org.mmbase.bridge.*;
import org.mmbase.bridge.util.SearchUtil;
import org.mmbase.remotepublishing.PublishManager;
import org.mmbase.remotepublishing.util.PublishUtil;
@@ -53,16 +34,17 @@
import com.finalist.cmsc.mmbase.PropertiesUtil;
import com.finalist.cmsc.navigation.ServerUtil;
import com.finalist.cmsc.portlets.ContentPortlet;
-import com.finalist.pluto.portalImpl.core.CmscPortletMode;
public class ResponseFormPortlet extends ContentPortlet {
protected static final String PARAMETER_MAP = "parameterMap";
protected static final String ERRORMESSAGES = "errormessages";
- private static final int DEFAULT_MAXFILESIZE = 2; // default file size in
- // Meg
+ private static final int DEFAULT_MAXFILESIZE = 2; // default file size in
Meg
private static final long MEGABYTE = 1024 * 1024; // 1 Meg
+
+ private static final String ENCODING_UTF8 = "UTF-8";
+
private static final String FIELD_PREFIX = "field_";
private static final int TYPE_TEXTBOX = 1;
private static final int TYPE_TEXTAREA = 2;
@@ -77,32 +59,30 @@
private static final String DEFAULT_EMAILREGEX =
"^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$";
+ @Override
@SuppressWarnings("unchecked")
public void processView(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
Map<String, String> errorMessages = new Hashtable<String, String>();
Map<String, Object> parameterMap = new HashMap<String, Object>();
DataSource attachment = processUserRequest(request, errorMessages,
parameterMap);
- String action = request.getParameter(ACTION_PARAM);
- Map<String, Object> formfields = new HashMap<String, Object>();
- StringBuffer data = new StringBuffer();
- if (action == null) {
- response.setPortletMode(CmscPortletMode.EDIT_DEFAULTS);
- }
- else if (action.equals("edit")) {
PortletPreferences preferences = request.getPreferences();
String contentelement = preferences.getValue(CONTENTELEMENT, null);
if (contentelement != null) {
if (parameterMap.size() > 0) {
+
+ String userEmailAddress = null;
+ StringBuffer data = new StringBuffer();
+ Map<String, Object> formfields = new HashMap<String, Object>();
+
CloudProvider cloudProvider =
CloudProviderFactory.getCloudProvider();
Cloud cloud = cloudProvider.getCloud();
Node responseForm = cloud.getNode(contentelement);
NodeList formfieldList =
SearchUtil.findRelatedOrderedNodeList(responseForm, "formfield", "posrel",
"posrel.pos");
- NodeIterator formfieldIterator = formfieldList.nodeIterator();
- String userEmailAddress = null;
+ NodeIterator formfieldIterator = formfieldList.nodeIterator();
while (formfieldIterator.hasNext()) {
Node formfield = formfieldIterator.nextNode();
String number = formfield.getStringValue("number");
@@ -132,13 +112,14 @@
}
if ((type == TYPE_TEXTBOX) && sendEmail) { //If data is
used as email address, then it should be valid
- if (!isEmailAddress(userEmailAddress))
+ if (!isEmailAddress(userEmailAddress)) {
errorMessages.put(fieldIdentifier,
"view.formfield.invalid");
}
+ }
if (type == TYPE_CHECKBOX) {
if(value != null && value instanceof String){
- textValue = (value == null) ? CHECKBOX_NO :
value.toString();
+ textValue = value.toString();
}
else if (value != null && value instanceof ArrayList){
textValue = transferParameterValues((ArrayList)value);
@@ -187,12 +168,6 @@
else {
getLogger().error("No contentelement");
}
- // switch to View mode
- response.setPortletMode(PortletMode.VIEW);
- }
- else {
- getLogger().error("Unknown action: '" + action + "'");
- }
}
@@ -202,6 +177,7 @@
data.append(textValue);
data.append(System.getProperty("line.separator"));
}
+
@SuppressWarnings("unchecked")
private String transferParameterValues(List textValues) {
StringBuffer temp = new StringBuffer();
@@ -306,13 +282,14 @@
String emailTextBefore = responseform.getStringValue("emailbody");
String emailTextAfter = responseform.getStringValue("emailbodyafter");
- String senderEmailAddress = userEmailAddress;
- String senderName = userEmailAddress;
- if (StringUtils.isBlank(userEmailAddress)) {
- senderEmailAddress =
responseform.getStringValue("useremailsender").trim();
- senderName =
responseform.getStringValue("useremailsendername").trim();
+ String senderEmailAddress =
responseform.getStringValue("useremailsender").trim();
+ String senderName =
responseform.getStringValue("useremailsendername").trim();
+ if (StringUtils.isNotBlank(userEmailAddress)) {
+ senderName = userEmailAddress + " [CMS]";
+ }
+ if (!isEmailAddress(senderEmailAddress)) {
+ return false; //Last check email address
}
- if (!isEmailAddress(senderEmailAddress)) return false; //Last check
email address
emailTextBefore = emailTextBefore.trim();
emailText.append(emailTextBefore);
@@ -324,9 +301,11 @@
emailText.append(emailTextAfter);
}
- String emailAddressesValue =
responseform.getStringValue("emailaddresses").replaceAll(" ","");
+ String emailAddressesValue =
responseform.getStringValue("emailaddresses");
String emailSubject = responseform.getStringValue("emailsubject").trim();
- List<String> emailList = Arrays.asList(emailAddressesValue.split(";"));
+
+ List<String> emailList = splitEmailAddresses(emailAddressesValue);
+
if (!isEmailAddress(emailList)) {
getLogger().error("error sending email. Some of the following
emailaddresses are incorrect: " + emailList.toString());
return false; //Could not sent email because of false email address
@@ -334,7 +313,7 @@
try {
EmailSender.getInstance().sendEmail(senderEmailAddress, senderName,
emailList, emailSubject, emailText.toString(),
- attachment);
+ attachment, userEmailAddress);
sent = true;
}
catch (UnsupportedEncodingException e) {
@@ -347,6 +326,17 @@
}
+ private List<String> splitEmailAddresses(String emailAddressesValue) {
+ List<String> emailList = new ArrayList<String>();
+ StringTokenizer addresssTokenizer = new
StringTokenizer(emailAddressesValue, " ,;");
+ while(addresssTokenizer.hasMoreTokens()) {
+ String address = addresssTokenizer.nextToken();
+ emailList.add(address);
+ }
+ return emailList;
+ }
+
+
@SuppressWarnings("unchecked")
@Override
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
@@ -394,6 +384,7 @@
}
+ @Override
public void processEdit(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
super.processEdit(request, response);
@@ -415,12 +406,12 @@
Map<String, Object> parameterMap) {
List<FileItem> fileItems = null;
DataSource attachment = null;
- String encoding = "UTF-8";
+
List<String> parameterValues = null;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
PortletFileUpload upload = new PortletFileUpload(factory);
- upload.setHeaderEncoding(encoding);
+ upload.setHeaderEncoding(ENCODING_UTF8);
fileItems = upload.parseRequest(request);
}
catch (FileUploadException e) {
@@ -441,15 +432,15 @@
else if (parameterMap.get(fileItem.getFieldName())
instanceof ArrayList){
parameterValues = (ArrayList<String>)
parameterMap.get(fileItem.getFieldName());
}
- parameterValues.add(fileItem.getString(encoding));
+ parameterValues.add(fileItem.getString(ENCODING_UTF8));
parameterMap.put(fileItem.getFieldName(),
parameterValues);
}
else{
- parameterMap.put(fileItem.getFieldName(),
fileItem.getString(encoding));
+ parameterMap.put(fileItem.getFieldName(),
fileItem.getString(ENCODING_UTF8));
}
}
catch (UnsupportedEncodingException e) {
- getLogger().error("UnsupportedEncoding " + encoding);
+ getLogger().error("UnsupportedEncoding " + ENCODING_UTF8);
}
}
else {
@@ -470,21 +461,32 @@
}
public boolean isEmailAddress(String emailAddress) {
- if (emailAddress == null) return false;
- if (StringUtils.isBlank(emailAddress)) return false;
-
+ if (emailAddress == null) {
+ return false;
+ }
+ if (StringUtils.isBlank(emailAddress)) {
+ return false;
+ }
String emailRegex = getEmailRegex();
return emailAddress.trim().matches(emailRegex);
}
public boolean isEmailAddress(List<String> emailList) {
- if (emailList == null) return false;
- if (emailList.isEmpty()) return false;
+ if (emailList == null) {
+ return false;
+ }
+ if (emailList.isEmpty()) {
+ return false;
+ }
String emailRegex = getEmailRegex();
for (String email : emailList) {
- if (email == null || StringUtils.isBlank(email)) return false;
- if (!email.matches(emailRegex)) return false;
+ if (email == null || StringUtils.isBlank(email)) {
+ return false;
+ }
+ if (!email.matches(emailRegex)) {
+ return false;
+ }
}
return true;
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs