Sending Random SMS Text Messages With ColdFusion To Make Her Feel Loved

<!--- Get the current time. --->
<cfset dtNow = Now() />
 
<!---
	Check to see if this is a weekday. We only care about
	doing this during the week when we are more than likely
	NOT with our special someones. Additionally, we only want
	this to be for work hours - 9AM - 6PM.
 
	Additionally, we only want to send a message if we are
	at or after the time of the next message slot.
--->
<cfif (
	(DayOfWeek( dtNow ) GTE 2) AND
	(DayOfWeek( dtNow ) LTE 6) AND
	(Hour( dtNow ) GTE 9) AND
	(Hour( dtNow ) LTE 18) AND
	(Now() GTE APPLICATION.Settings.NextMessage)
	)>
 
 
	<!---
		Based on the current time, create a time-only
		data value. This will be used in our min/max
		query criteria.
	--->
	<cfset dtTime = CreateTime(
		Hour( dtNow ),
		Minute( dtNow ),
		Second( dtNow )
		) />
 
 
	<!---
		Query for valid messages based on the date
		and time criteria.
	--->
	<cfquery name="qMessage" dbtype="query">
		SELECT
			id,
			message
		FROM
			APPLICATION.Messages
		WHERE
			(
					min_time IS NULL
				OR
					min_time <= <cfqueryparam value="#dtTime#" cfsqltype="CF_SQL_FLOAT" />
			)
		AND
			(
					max_time IS NULL
				OR
					max_time >= <cfqueryparam value="#dtTime#" cfsqltype="CF_SQL_FLOAT" />
			)
		AND
			(
					days IS NULL
				OR
					days LIKE <cfqueryparam value="%#LCase( DateFormat( dtNow, 'ddd' ) )#%" cfsqltype="CF_SQL_VARCHAR" />
			)
 
		<!---
			Check to make sure we are not selecting a
			recently used message.
		--->
		AND
			id NOT IN (
				<cfqueryparam value="#ArrayToList( APPLICATION.Settings.PrevMessages )#,0" cfsqltype="CF_SQL_INTEGER" list="yes" />
			)
 
		ORDER BY
			id ASC
	</cfquery>
 
 
	<!---
		Check to make sure that we have at least
		one message to choose from.
	--->
	<cfif qMessage.RecordCount>
 
 
		<!--- Select a random row for the query. --->
		<cfset intMessage = RandRange(
			1,
			qMessage.RecordCount
			) />
 
 
		<!---
			Send email for confirmation. We don't want to just
			fire off these text messages without confirmation
			or we might text our ladies at highly inappropriate
			times (such as when we are currently with them).
		--->
		<cfmail
			to="xyz@xxxxxxxxxx.com"
			from="xyz@xxxxxxxxxx.com"
			subject="Romantic SMS Text Message Confirmation"
			type="HTML">
 
			<div style="font-size: 18px ; line-height: 27px ;">
 
				<p>
					Hey Romeo, do you want to send out the
					following text message:
				</p>
 
				<blockquote style="font-style: italic ;">
					#qMessage[ "message" ][ intMessage ]#
				</blockquote>
 
				<p>
					<br />
					<br />
				</p>
 
				<p>
					<a
						href="http://#CGI.server_name##GetDirectoryFromPath( CGI.script_name )#confirm_message.cfm?id=#qMessage[ "id" ][ intMessage ]#"
						>SEND TEXT MESSAGE</a>
				</p>
 
			</div>
 
		</cfmail>
 
	</cfif>
 
</cfif>

For Cut-and-Paste