<cfif session.UserID NEQ 0>

<cfif form.mode eq "start">

	<h3>Send an email!</h3>

	<cfparam name="users_to_email" default="">

	<cfquery name="select_users_to_email" datasource="#application.datasource#">
		SELECT	    U.EmailAddress
		FROM	    Users U
		WHERE       UserID in (0,1,2)
	</cfquery>
	
	<cfoutput query="select_users_to_email">
	
		#EmailAddress#, 
	
		<cfset users_to_email=listappend(users_to_email, #EmailAddress#)>
	
	</cfoutput><br><br>

	<script language="JavaScript">
		function submitforminputemail()
		{
		document.myforminputemail.submit();
		}
	</script>
	
	<cfoutput><form action="#request.cgipath#" method="post" name="myforminputemail" id="myforminputemail" style="display:inline;">
	
		Email subject:<br><input type="text" name="email_subject" size="30" maxlength="50"><br><br>
		
		Email Body:<br>
		
		<textarea name="email_body" rows="4" cols="30" placeholder="type email body here"></textarea><br><br>
		
		<a href="javascript: submitforminputemail()">Submit</a><br>
		
		<input type="hidden" name="mode" value="confirm_email_contents">
		<input type="hidden" name="users_to_email" value="#users_to_email#">
	
	</form></cfoutput>

<cfelseif form.mode eq "confirm_email_contents">

	does this look correct?<br><br>
	
	Subject:<br><cfoutput>#form.email_subject#</cfoutput><br><br>
	
	Body:<br><cfoutput>#HTMLCodeFormat(form.email_body)#</cfoutput>

	<script language="JavaScript">
		function submitformsendtest()
		{
		document.myformsendtest.submit();
		}
	</script>
	
	<cfoutput><form action="#request.cgipath#" method="post" name="myformsendtest" id="myformsendtest" style="display:inline;">
		
		<a href="javascript: submitformsendtest()">Yes, send the test email!</a><br>
		
		<input type="hidden" name="mode" value="send_test_email">
		<input type="hidden" name="email_subject" value="#form.email_subject#">
		<input type="hidden" name="email_body" value="#form.email_body#">
		<input type="hidden" name="users_to_email" value="#users_to_email#">
	
	</form></cfoutput>	

<cfelseif form.mode eq "send_test_email">

	<cfmail to="admin@payfrit.com" from="admin@payfrit.com" subject="#form.email_subject#" type="HTML">#HTMLCodeFormat(form.email_body)#</cfmail>

	<script language="JavaScript">
		function submitformsendtest()
		{
		document.myformsendtest.submit();
		}
	</script>
	
	<cfoutput><form action="#request.cgipath#" method="post" name="myformsendtest" id="myformsendtest" style="display:inline;">
		
		<a href="javascript: submitformsendtest()">Looks good, send the real email!</a><br>
		
		<input type="hidden" name="mode" value="send_email">
		<input type="hidden" name="email_subject" value="#form.email_subject#">
		<input type="hidden" name="email_body" value="#form.email_body#">
		<input type="hidden" name="users_to_email" value="#users_to_email#">
	
	</form></cfoutput>

<cfelseif form.mode eq "send_email">

	<cfloop index="the_email_address" list="#users_to_email#">

		<cfquery name="get_user_email" datasource="#application.datasource#">
			SELECT			UUID
			FROM			Users
			WHERE			EmailAddress = '#the_email_address#'
								AND
							UserIsEmailverified = 1
								AND
							IsContactVerified > 0
		</cfquery>
		
		<cfset form.this_email_body = form.email_body & "

instant unsubscribe link: https://www.payfrit.com/remove_me.cfm?UUID="&#get_user_email.UUID#>

		<cfmail to="#the_email_address#" from="admin@payfrit.com" subject="#form.email_subject#" type="HTML">
	#HTMLCodeFormat(form.this_email_body)#</cfmail>
	
	</cfloop>

	<cfquery name="insert_email" datasource="#application.datasource#">
		INSERT INTO		MarketingEmails
						(
						 MarketingEmailSubject,
						 MarketingEmailBody,
						 MarketingEmailUsersEmailed
						)
		VALUES			(
						'#form.email_subject#',
						'#form.email_body#',
						'#users_to_email#'					
						)
	</cfquery>
	
	Email sent!<br>

</cfif>

<cfelse>

	Please Login!
	
</cfif>