Coldfusion 使用Cfmail时,是否可以在发送前查看电子邮件?
从我在网上看到的例子来看,使用cfmail似乎只是自动发送电子邮件。 在发送给收件人之前,是否可以查看电子邮件HTML文本? 换言之,我将能够更改“收件人:”电子邮件部分。 这是可能的还是cfmail只能自动发送电子邮件 cfm文件的示例代码:Coldfusion 使用Cfmail时,是否可以在发送前查看电子邮件?,coldfusion,coldfusion-9,Coldfusion,Coldfusion 9,从我在网上看到的例子来看,使用cfmail似乎只是自动发送电子邮件。 在发送给收件人之前,是否可以查看电子邮件HTML文本? 换言之,我将能够更改“收件人:”电子邮件部分。 这是可能的还是cfmail只能自动发送电子邮件 cfm文件的示例代码: <cfquery....> select ... </cfquery> <cfmail query="test" to="" from="mail@mail.com" ..... type="html"> here
<cfquery....>
select ...
</cfquery>
<cfmail query="test"
to=""
from="mail@mail.com"
.....
type="html">
here is test
<cfoutput>
<p>#data</p>
</cfouput>
</cfmail>
我会这样做
<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>
我还没有测试过这个。这将需要一些调整,但应该指向正确的方向 我会这样做
<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>
我还没有测试过这个。这将需要一些调整,但应该指向正确的方向 这个答案与威尔的非常相似。它用更少的代码完成这项工作 假设用户提交表单,请从以下内容开始:
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>
我复制的代码是很久以前在一个电脑被锁定的环境中编写的。如果我再次这样做,我可能会使用隐藏的表单字段而不是会话变量。这些天来,他们更有可能发生意想不到的变化
如果您是为公众编写代码,请记住,用户可能会更改其浏览器首选项以阻止新窗口打开。此答案与Will的答案非常相似。它用更少的代码完成这项工作 假设用户提交表单,请从以下内容开始:
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>
我复制的代码是很久以前在一个电脑被锁定的环境中编写的。如果我再次这样做,我可能会使用隐藏的表单字段而不是会话变量。这些天来,他们更有可能发生意想不到的变化
如果您是为公众编写代码,请记住,用户可能会更改其浏览器首选项以阻止新窗口打开。您需要创建自己的界面来收集“正确”的收件人信息。cfmail只发送电子邮件,没有接口组件。可能重复的问题不会重复。在另一个问题中提出的问题引出了另一个问题。如果您使用eclipse,我将推荐类似MailSnag的工具。通过MailSnag,您可以在Eclipse中调试应用程序生成的电子邮件。MailSnag创建了一个简单的SMTP服务器,用于在开发过程中捕获和检查应用程序发送的电子邮件。您可以查看呈现为HTML、文本或两者的电子邮件。您还可以查看原始数据、嵌入内容和附件。您需要创建自己的界面来收集“正确”的收件人信息。cfmail只发送电子邮件,没有接口组件。可能重复的问题不会重复。在另一个问题中提出的问题引出了另一个问题。如果您使用eclipse,我将推荐类似MailSnag的工具。通过MailSnag,您可以在Eclipse中调试应用程序生成的电子邮件。MailSnag创建了一个简单的SMTP服务器,用于在开发过程中捕获和检查应用程序发送的电子邮件。您可以查看呈现为HTML、文本或两者的电子邮件。您还可以查看原始数据、嵌入内容和附件。哇,js和ajax,我想我可以远离这些。我看看我是否能让它工作,你可以远离js和ajax。这完全取决于你希望你的用户看到什么以及你希望他能够更改什么。我希望用户在发送消息之前只看到消息,无需更改。我希望使用ajax,因此电子邮件功能在后台运行。如果你喜欢的话,你可以用CF来做所有的事情。哇,js和ajax,我想我可以远离它。我看看我是否能让它工作,你可以远离js和ajax。这完全取决于你希望你的用户看到什么以及你希望他能够更改什么。我希望用户在发送消息之前只看到消息,无需更改。我希望使用ajax,因此电子邮件功能在后台运行。如果您愿意,您可以在CF中完成整个操作。我正在考虑建议将其添加到会话范围,但正如您所说,这可能不是最好的主意。我正在考虑建议将其添加到会话范围,但正如您所说,这可能不是最好的主意。