0

From examples I seen online It seems using cfmail is only to send emails automatically. Would it be possible to see the email HTML text before sending it the the recipient? In other words I would be able to change the 'TO:' email part. Is this possible or cfmail only works sending emails automatically?

Example code of cfm file:

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

<cfmail query="test"
to=""
from="mail@mail.com"
.....
type="html">
here is test
<cfoutput>
<p>#data</p>
</cfouput>
</cfmail>
user3591637
  • 499
  • 5
  • 20
  • 1
    You will need to make your own interface to collect the 'correct' recipient information. `cfmail` simply sends the email, it has no interface components. – Antony May 01 '14 at 06:30
  • possible duplicate of [How to send table html with email using mailto?](http://stackoverflow.com/questions/23401893/how-to-send-table-html-with-email-using-mailto) – duncan May 01 '14 at 08:10
  • Not a duplicate question. What was asked in the other question has lead to a different question. – Will May 01 '14 at 10:13
  • If you use eclipse I would recomend tool like MailSnag. https://marketplace.eclipse.org/content/mailsnag "With MailSnag, you can debug application generated emails within Eclipse. MailSnag creates a simple SMTP server to capture and inspect emails sent out by an application during development. You can view the email rendered as HTML, text or both. You can also view the raw data, embedded content and attachments." – nkostic May 01 '14 at 16:07

2 Answers2

3

I would do something like this.

 <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>

I have not tested this. It will take some tweaking but should point you in the right direction.

Will
  • 3,004
  • 29
  • 43
  • wow , js and ajax, I thought i would be able to stay away from that. I see if i can make it work – user3591637 May 01 '14 at 14:19
  • You can stay away from js and ajax. It simply depends on what you want your user to see and what you want him to be able to change. – Dan Bracuk May 01 '14 at 16:11
  • I would like the user just to see the message before sending it, no need to change it – user3591637 May 01 '14 at 16:18
  • I like to use ajax so the email function is run in the back ground. You can do the whole thing in CF if you like. – Will May 02 '14 at 09:13
2

This answer is very similar to Will's. It does the job with a bit less code.

Assuming the user submits a form, start with this:

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

Then do this:

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

Present this to the user:

<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 looks like this:

<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>

The code which I copied was written a long time ago in an envrionment with locked down computers. If I were to do it again, I would probably use hidden form fields instead of session variables. They are more likely to change unexpectedly these days.

If you are coding for the general public, bear in mind that the user might change his browser preferences to prevent the new window from opening.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • I was thinking about suggestion adding it to the session scope but like you said it might not be the best idea. – Will May 02 '14 at 09:18