Improved: EmailServices.sendMailFromScreen improved to take multiple
attachments with appropriate type along with several email fixes. (OFBIZ-9395) This patch fixes a number of issues combined in a patch because of the code dependencies. 1. it enables to add BCC adress(es) to service OrderServices.sendOrderNotificationScreens to oversteer ProductStoreEmailSetting of BCC the same was as for CC. 2. a method UtilValidate.isEmailList(String) is added to check a comma separated list of email addresses, used for example to check the String passed to the new BCC field for an Order-Notification. 3. there are improvements in EmailServices.sendMailFromScreen. The attachment type of MailAttachments is now not only .pdf but depends on the specific file. This has not been the case before - the mime type was always hard coded as .pdf. The same goes for the bodyPart content-type which is now set to the passed content type or the default text/html type. Before this was also always set to text/html. Additionally, an attachment that has the mime-type text/plain is not rendered with the fop-renderer anymore but with a simple text-renderer. Therefore it is possible to send an CSV file as attachment now. The patch also refactors some catch-Blocks in the EmailServices.sendMailFromScreen by using multi-catch since the e xception handling is always the same. Thanks Martin Becker for reporting and providing the patch. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1805459 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -51,6 +51,7 @@ import javax.mail.internet.MimeMultipart;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.apache.fop.apps.FOPException;
|
||||
import org.apache.fop.apps.Fop;
|
||||
import org.apache.fop.apps.MimeConstants;
|
||||
import org.apache.ofbiz.base.util.Debug;
|
||||
@@ -438,6 +439,12 @@ public class EmailServices {
|
||||
if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationListParam)) xslfoAttachScreenLocationList.addAll(xslfoAttachScreenLocationListParam);
|
||||
if (UtilValidate.isNotEmpty(attachmentNameListParam)) attachmentNameList.addAll(attachmentNameListParam);
|
||||
|
||||
List<String> attachmentTypeList = new LinkedList<String>();
|
||||
String attachmentTypeParam = (String) serviceContext.remove("attachmentType");
|
||||
List<String> attachmentTypeListParam = UtilGenerics.checkList(serviceContext.remove("attachmentTypeList"));
|
||||
if (UtilValidate.isNotEmpty(attachmentTypeParam)) attachmentTypeList.add(attachmentTypeParam);
|
||||
if (UtilValidate.isNotEmpty(attachmentTypeListParam)) attachmentTypeList.addAll(attachmentTypeListParam);
|
||||
|
||||
Locale locale = (Locale) serviceContext.get("locale");
|
||||
Map<String, Object> bodyParameters = UtilGenerics.checkMap(serviceContext.remove("bodyParameters"));
|
||||
if (bodyParameters == null) {
|
||||
@@ -504,9 +511,9 @@ public class EmailServices {
|
||||
List<Map<String, ? extends Object>> bodyParts = new LinkedList<Map<String, ? extends Object>>();
|
||||
if (bodyText != null) {
|
||||
bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale);
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyText, "type", "text/html"));
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyText, "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html"));
|
||||
} else {
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyWriter.toString(), "type", "text/html"));
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyWriter.toString(), "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html"));
|
||||
}
|
||||
|
||||
for (int i = 0; i < xslfoAttachScreenLocationList.size(); i++) {
|
||||
@@ -515,39 +522,51 @@ public class EmailServices {
|
||||
if (UtilValidate.isNotEmpty(attachmentNameList) && attachmentNameList.size() >= i) {
|
||||
attachmentName = attachmentNameList.get(i);
|
||||
}
|
||||
|
||||
String attachmentType = MimeConstants.MIME_PDF;
|
||||
if (UtilValidate.isNotEmpty(attachmentTypeList) && attachmentTypeList.size() >= i) {
|
||||
attachmentType = attachmentTypeList.get(i);
|
||||
}
|
||||
|
||||
isMultiPart = true;
|
||||
// start processing fo pdf attachment
|
||||
try {
|
||||
Writer writer = new StringWriter();
|
||||
MapStack<String> screenContextAtt = MapStack.create();
|
||||
// substitute the freemarker variables...
|
||||
ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", dctx.getDelegator()),
|
||||
EntityUtilProperties.getPropertyValue("widget", "screenfop.screenrenderer", dctx.getDelegator()));
|
||||
ScreenStringRenderer foScreenStringRenderer = null;
|
||||
if(MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)){
|
||||
foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screentext.name", dctx.getDelegator()),
|
||||
EntityUtilProperties.getPropertyValue("widget", "screentext.screenrenderer", dctx.getDelegator()));
|
||||
}else{
|
||||
foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", dctx.getDelegator()),
|
||||
EntityUtilProperties.getPropertyValue("widget", "screenfop.screenrenderer", dctx.getDelegator()));
|
||||
}
|
||||
ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContext, foScreenStringRenderer);
|
||||
screensAtt.populateContextForService(dctx, bodyParameters);
|
||||
screenContextAtt.putAll(bodyParameters);
|
||||
screensAtt.render(xslfoAttachScreenLocation);
|
||||
|
||||
// create the input stream for the generation
|
||||
StreamSource src = new StreamSource(new StringReader(writer.toString()));
|
||||
|
||||
// create the output stream for the generation
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF);
|
||||
ApacheFopWorker.transform(src, null, fop);
|
||||
if (MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)) {
|
||||
baos.write(writer.toString().getBytes());
|
||||
} else {
|
||||
// create the input stream for the generation
|
||||
StreamSource src = new StreamSource(new StringReader(writer.toString()));
|
||||
Fop fop = ApacheFopWorker.createFopInstance(baos, attachmentType);
|
||||
ApacheFopWorker.transform(src, null, fop);
|
||||
}
|
||||
|
||||
// and generate the PDF
|
||||
// and generate the attachment
|
||||
baos.flush();
|
||||
baos.close();
|
||||
|
||||
// store in the list of maps for sendmail....
|
||||
bodyParts.add(UtilMisc.<String, Object> toMap("content", baos.toByteArray(), "type", "application/pdf", "filename",
|
||||
attachmentName));
|
||||
} catch (Exception e) {
|
||||
Debug.logError(e, "Error rendering PDF attachment for email: " + e.toString(), module);
|
||||
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenPdfError",
|
||||
UtilMisc.toMap("errorString", e.toString()), locale));
|
||||
bodyParts.add(UtilMisc.<String, Object>toMap("content", baos.toByteArray(), "type", attachmentType, "filename", attachmentName));
|
||||
|
||||
} catch (GeneralException|IOException|SAXException|ParserConfigurationException |TemplateException ge) {
|
||||
Debug.logError(ge, "Error rendering PDF attachment for email: " + ge.toString(), module);
|
||||
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", ge.toString()), locale));
|
||||
}
|
||||
|
||||
serviceContext.put("bodyParts", bodyParts);
|
||||
|
||||
Reference in New Issue
Block a user