自定义表单,通过Spring使用FreeMarker(FTL模板)发送到Outlook邮件。

huangapple go评论104阅读模式
英文:

Custom form to send on outlook email through spring using freemarker (ftl template)

问题

我想要通过邮件向最终用户发送带有表单元素的电子邮件。因此,最终用户可以在电子邮件中填写表单详细信息并提交。

我已经创建了一个ftl模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <table>
        <tr>
            <td>
                This e-mail is for project approval :<br/>
                <a href="">${projectName}</a><br/><br/>
                <form action="" method="post" id="projectApprovalFrm">
                    Status Reason:<br>
                    <textarea name="statusReason"></textarea><br><br>
                </form>
                <input type="button" value="Reject" id="reject" />
                <input type="button" value="Approve" id="approve" />
            </td>
        </tr>
    </table>

    <script>
        $("#approve").click(function() {
            $("#projectApproveFrm").attr("action", "http://localhost:8080/projectApproval");
            $("#projectApproveFrm").submit();
        });
        $("#reject").click(function() {
            $("#projectApproveFrm").attr("action", "http://localhost:8080/projectReject");
            $("#projectApproveFrm").submit();
        });
    </script>
</body>
</html>

Spring邮件发送代码:

Mail mail = new Mail(); // 创建POJO类以保存电子邮件属性
mail.setFrom("mail@outlook.id"); // 发件人邮件地址
mail.setTo("user@domain.com"); // 收件人邮件地址
mail.setSubject("Test Email");

MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,
        MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());

Template t = freemarkerConfig.getTemplate("email-project-approval-template.ftl");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, mail.getModel());
helper.setTo(mail.getTo());
helper.setText(html, true);
helper.setSubject(mail.getSubject());
helper.setFrom(mail.getFrom());

javaMailSender.send(message);

电子邮件已发送给最终用户。然而,在Outlook中,表单元素是可见的。

我还通过浏览器上的Webmail进行了验证,在那里元素是可见的,但是表单没有被提交。

如何解决这个问题?是否需要更新任何Java代码或ftl文件中是否需要进行任何更改?是否有人有任何想法?

或者是否有其他方法可以使用JAVA在电子邮件中发送HTML表单?

英文:

I want to send email with form elements to end users. So, End users can fill up the form details within email and submit it.

I have created a ftl template:

&lt;!DOCTYPE html&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;/head&gt;
	&lt;body&gt;
        &lt;table&gt;
            &lt;tr&gt;
                &lt;td&gt; 
					This e-mail is for project approval :&lt;br/&gt;
					&lt;a href=&quot;&quot;&gt;${projectName}&lt;/a&gt;&lt;br/&gt;
					&lt;br/&gt;
					&lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;projectApprovalFrm&quot;&gt;
					 Status Reason:&lt;br&gt;
					 &lt;textarea name=&quot;statusReason&quot;&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
					&lt;/form&gt;
					&lt;input type=&quot;button&quot; value=&quot;Reject&quot; id=&quot;reject&quot; /&gt;
					&lt;input type=&quot;button&quot; value=&quot;Approve&quot; id=&quot;approve&quot; /&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        
        &lt;script&gt;
        $( &quot;#approve&quot; ).click(function() {
          $(&quot;#projectApproveFrm&quot;).action( &quot;http://localhost:8080/projectApproval&quot; );
          $(&quot;#projectApproveFrm&quot;).submit();
        });
         $( &quot;#reject&quot; ).click(function() {
          $(&quot;#projectApproveFrm&quot;).action( &quot;http://localhost:8080/projectReject&quot; );
          $(&quot;#projectApproveFrm&quot;).submit();
        });
      &lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;

Spring code to sent email:

Mail mail = new Mail();	// Created POJO class to hold email properties
mail.setFrom(&quot;mail@outlook.id&quot;); //Outlook mail id from mail sent	
mail.setTo(&quot;user@domain.com&quot;);	//receiver mail id
mail.setSubject(&quot;Test Email&quot;);		
	        	
MimeMessage message = javaMailSender.createMimeMessage();	
MimeMessageHelper helper = new MimeMessageHelper(message,	
	       MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,StandardCharsets.UTF_8.name());	
	        	
Template t = freemarkerConfig.getTemplate(&quot;email-project-approval-template.ftl&quot;);	
String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, mail.getModel());	
helper.setTo(mail.getTo());	
helper.setText(html, true);
helper.setSubject(mail.getSubject());	
helper.setFrom(mail.getFrom());	

javaMailSender.send(message);

Mails are been sent to end user. However, Form elements are visible in outlook

自定义表单,通过Spring使用FreeMarker(FTL模板)发送到Outlook邮件。

I also verified by webmail on browser where elements are visible but form is not getting submitted.

What should be done to overcome this issue? Is there any Java code needs to update or any changes needs to in ftl file. Does anyone have any ideas?

Or Is there any other way to send HTML form in email using JAVA?

答案1

得分: 1

这可能与Java无关,但与表单仅适用于特定电子邮件环境有关,而Outlook不在其中之列。请参阅https://www.litmus.com/blog/the-ultimate-guide-to-interactive-forms-in-email/以获取2020年的测试结果。

您仍然可以通过电子邮件发送表单,但对于Outlook,您需要提供备用方案(例如,链接到带有表单的网页)。

您可以使用Outlook特定的代码来实现这一点(由Litmus提供):

CSS

.interactive-form {
    display: block !important;
    max-height: inherit !important;
    overflow: visible !important;
}
.fallback-form {
    display: none;
}
 
body[data-outlook-cycle] .outlookshow{ display:block !important; width: auto !important; overflow: visible !important; float: none !important; max-height:inherit !important; max-width:inherit !important; line-height: auto !important; margin-top:0px !important; visibility:inherit !important;}
body[data-outlook-cycle] .outlookhide{ display:none !important; display:none; overflow:hidden; float:left; width:0px; max-height:0px; max-width:0px; line-height:0px; visibility:hidden; }
 
[class~=&quot;x_outlookshow&quot;] { display:block !important; width: auto !important; overflow: visible !important; float: none !important; max-height:inherit !important; max-width:inherit !important; line-height: auto !important; margin-top:0px !important; visibility:inherit !important;}
[class~=&quot;x_outlookhide&quot;] { display:none !important; display:none; overflow:hidden; float:left; width:0px; max-height:0px; max-width:0px; line-height:0px; visibility:hidden; }
 
@media only screen and (max-width: 640px) {
  .fallback-form {
    display:block !important;
    width: auto !important;
    overflow: visible !important;
    float: none !important;
    max-height:inherit !important;
    max-width:inherit !important;
    line-height: auto !important;
    margin-top: 0px !important;
    visibility:inherit !important;
  }
  .interactive-form, 
  .interactive-form p, 
  .interactive-form label, 
  .interactive-form input  {
    display:none !important;
    display:none !important;
    overflow:hidden !important;
    max-height: 0px !important;
    max-width: 0px !important;
    line-height: 0px !important;
    visibility:hidden !important;
  }
}

HTML

&lt;!--[if mso | ie]&gt;
&lt;style&gt;
.fallback-form {
  display: block !important;
  max-height: inherit !important;
  overflow: visible !important;
}
&lt;/style&gt;
&lt;![endif]--&gt;
 
 
&lt;!-- 开始 FORM_INTERACTIVE --&gt;
&lt;!--[if (!mso)&amp;amp;(gte IE 10)]&gt; &lt;! -- --&gt;
&lt;div class=&quot;interactive-form outlookhide&quot; style=&quot;display:none; max-height:0; line-height:0; font-size:0; mso-hide:all;&quot;&gt;
    [在此插入交互式表单代码]
&lt;/div&gt;
&lt;!--&lt;![endif]--&gt;
&lt;!-- 结束 FORM_INTERACTIVE --&gt;
 
&lt;!-- 开始 FORM_FALLBACK --&gt;
&lt;div class=&quot;fallback-form outlookshow&quot;&gt;
    [在此插入表单部分的备用方案]
&lt;/div&gt;
&lt;!-- 结束 FORM_FALLBACK --&gt;
英文:

It's probably nothing to do with Java, but everything to do with the fact that forms work on only certain email environments - and Outlook is not one of them. See https://www.litmus.com/blog/the-ultimate-guide-to-interactive-forms-in-email/ for 2020 tests.

You can still send forms through email - but will need a fallback for Outlook (i.e. a link to a webpage with the form).

You can do that with Outlook-specific code like so (courtesy of Litmus):

CSS

.interactive-form {
    display: block !important;
    max-height: inherit !important;
    overflow: visible !important;
}
.fallback-form {
    display: none;
}
 
body[data-outlook-cycle] .outlookshow{ display:block !important; width: auto !important; overflow: visible !important; float: none !important; max-height:inherit !important; max-width:inherit !important; line-height: auto !important; margin-top:0px !important; visibility:inherit !important;}
body[data-outlook-cycle] .outlookhide{ display:none !important; display:none; overflow:hidden; float:left; width:0px; max-height:0px; max-width:0px; line-height:0px; visibility:hidden; }
 
[class~=&quot;x_outlookshow&quot;] { display:block !important; width: auto !important; overflow: visible !important; float: none !important; max-height:inherit !important; max-width:inherit !important; line-height: auto !important; margin-top:0px !important; visibility:inherit !important;}
[class~=&quot;x_outlookhide&quot;] { display:none !important; display:none; overflow:hidden; float:left; width:0px; max-height:0px; max-width:0px; line-height:0px; visibility:hidden; }
 
@media only screen and (max-width: 640px) {
  .fallback-form {
    display:block !important;
    width: auto !important;
    overflow: visible !important;
    float: none !important;
    max-height:inherit !important;
    max-width:inherit !important;
    line-height: auto !important;
    margin-top: 0px !important;
    visibility:inherit !important;
  }
  .interactive-form, 
  .interactive-form p, 
  .interactive-form label, 
  .interactive-form input  {
    display:none !important;
    display:none !important;
    overflow:hidden !important;
    max-height: 0px !important;
    max-width: 0px !important;
    line-height: 0px !important;
    visibility:hidden !important;
  }
}

HTML

&lt;!--[if mso | ie]&gt;
&lt;style&gt;
.fallback-form {
  display: block !important;
  max-height: inherit !important;
  overflow: visible !important;
}
&lt;/style&gt;
&lt;![endif]--&gt;
 
 
&lt;!-- start FORM_INTERACTIVE --&gt;
&lt;!--[if (!mso)&amp;amp;(gte IE 10)]&gt; &lt;! -- --&gt;
&lt;div class=&quot;interactive-form outlookhide&quot; style=&quot;display:none; max-height:0; line-height:0; font-size:0; mso-hide:all;&quot;&gt;
    [Insert Interactive Form code here]
&lt;/div&gt;
&lt;!--&lt;![endif]--&gt;
&lt;!-- end FORM_INTERACTIVE --&gt;
 
&lt;!-- start FORM_FALLBACK --&gt;
&lt;div class=&quot;fallback-form outlookshow&quot;&gt;
    [Insert fallback for form section here]
&lt;/div&gt;
&lt;!-- end FORM_FALLBACK --&gt;

huangapple
  • 本文由 发表于 2020年10月7日 19:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定