【问题标题】:When using Cfmail is it possible to see the email before sending?使用 Cfmail 时,是否可以在发送前查看电子邮件?
【发布时间】:2014-05-01 04:50:21
【问题描述】:

从我在网上看到的例子看来使用cfmail只是自动发送电子邮件。 在将电子邮件发送给收件人之前是否可以看到电子邮件 HTML 文本? 换句话说,我将能够更改“收件人:”电子邮件部分。 这是可能的还是 cfmail 只能自动发送电子邮件?

cfm文件示例代码:

<cfquery....>
select ...
</cfquery>

<cfmail query="test"
to=""
from="mail@mail.com"
.....
type="html">
here is test
<cfoutput>
<p>#data</p>
</cfouput>
</cfmail>

【问题讨论】:

  • 您需要制作自己的界面来收集“正确”的收件人信息。 cfmail只是发送邮件,没有界面组件。
  • 不是重复的问题。另一个问题中提出的问题导致了另一个问题。
  • 如果您使用 eclipse,我会推荐 MailSnag 之类的工具。 marketplace.eclipse.org/content/mailsnag “使用 MailSnag,您可以在 Eclipse 中调试应用程序生成的电子邮件。MailSnag 创建一个简单的 SMTP 服务器来捕获和检查应用程序在开发期间发送的电子邮件。您可以查看呈现为 HTML、文本或两者的电子邮件。您可以还可以查看原始数据、嵌入内容和附件。”

标签: coldfusion coldfusion-9


【解决方案1】:

我会做这样的事情。

 <cfquery....>
select ...
</cfquery>

<!--- Save your content to a variable --->
<cfsavecontent variable="content">
    here is test
    <cfoutput>
    <p>#data#</p>
    </cfouput>

</cfsavecontent>

<!--- Output that content into a div so the user can see it--->
<div id="email-content">
    <cfoutput>#content#</cfouput>
</div>
<button onclick="sendEmail()">send</button>

<!--- When the user clicks send call a JS function --->
<!--- Use ajax to call a send function and use your email content --->

<script type="text/javascript">
    function sendEmail()
    {
        var emailContent = $('#email-content').html();

        $.ajax({
          type: 'POST',
          url: "yourCFCfileToSendEmail?method=sendEmail&content=" + emailContent ,
          success: function(){
                console.log("email sent");
          }
        });
    }
</script>





<!--- An example of what your email function would look like in the cfc .  --->
<cffunction name="sendEmail" access="remote" returntype="string" returnformat="plain">
        <cfargument name="toEmail" type="string" required="true">
        <cfargument name="fromEmail" type="string" required="true">
        <cfargument name="subject" type="string" required="true">
        <cfargument name="content" type="string" required="true">
        <cfargument name="replyTo" type="string" required="false">

        <cfset attributes = decodeParams(params)>

        <cfparam name="arguments.replyTo" default="#arguments.fromEmail#">
        <cfif NOT Len(Trim(arguments.replyTo))>
            <cfset arguments.replyTo = arguments.fromEmail>
        </cfif>

        <cfmail to="#arguments.toEmail#"
                from="#arguments.fromEmail#"
                subject="#arguments.subject#"
                replyto="#arguments.replyTo#"
                type="html">

            <h1>#arguments.subject#</h1>
            #content#


        </cfmail> 
</cffunction>

我没有测试过这个。这需要一些调整,但应该为您指明正确的方向。

【讨论】:

  • 哇,js 和 ajax,我想我可以远离那个。我看看我能不能让它工作
  • 你可以远离js和ajax。这仅取决于您希望用户看到什么以及您希望他能够更改什么。
  • 我希望用户在发送之前只看到消息,不需要更改它
  • 我喜欢使用 ajax,所以电子邮件功能在后台运行。如果您愿意,您可以在 CF 中完成所有操作。
【解决方案2】:

这个答案与威尔的答案非常相似。它用更少的代码完成这项工作。

假设用户提交了一个表单,从这个开始:

 session.mailto = form.mailto;
 session.mailfrom = form.mailfrom;

然后这样做:

<cfsavecontent variable = "session.mailbody">
code
</cfsavecontent>

将此呈现给用户:

<a href="javascript:void(0)" 
onclick="Emailwin=window.open('SendMail.cfm','thewin', 
'width=500,height=500,left=30,top=30');">
<button type="button">Send Mail </button>

SendMail.cfm 看起来像这样:

<cfmail to="#session.mailto#" from="#session.mailfrom#" 
subject = "something" type="html">
#session.mailbody#
</cfmail> 

<h3>Your mail has been sent.  This window will close in 2 seconds.</h3>
<script language="javascript">
winClose=window.setTimeout("window.close()",2000);
</script>

我复制的代码是很久以前在计算机被锁定的环境中编写的。如果我再做一次,我可能会使用隐藏的表单字段而不是会话变量。这些天他们更有可能发生意想不到的变化。

如果您是为公众编写代码,请记住用户可能会更改其浏览器首选项以阻止新窗口打开。

【讨论】:

  • 我正在考虑将其添加到会话范围的建议,但就像您说的那样,这可能不是最好的主意。
猜你喜欢
  • 2012-10-02
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多