Tuesday, February 17, 2015

Setting up e-mail server in ColdFusion

What is e-mail server?



          A mail server is also referred as e-mail server that handles (receives & delivers) e-mail over a network, basically over the internet.

Uses of e-mail server

          In this Tech Era,in our day to day life for any statement , login verification , reports , applications and many other purpose we all are using email-ids. The e-mail functionality works due to the presence of a series of e-mail servers in between the sender and the receiver.

Examples:

  • Lets say we are applying for any Examination online. Once it is applied successfully we are getting confirmation message via e-mail.
  • If we are doing any e-shopping, on success/failure we are getting confirm/regret message via e-mail.
  • Getting monthly banking statement via e-mail.
  • Sending mail to any email-id.
In all the above cases we are using various e-mail servers to perform these operations.

How e-mail server works?

          Lets say we are sending any mail to another email-id. Here when we click the send button, then the email client communicates with the SMTP* server and provide the sender's and receiver's email addresses,message body and the attachments(If available) and the work of e-mail server starts. It then processes the receiver's email address and sends the message to IMAP**/POP3*** server directly(in case of same receiver domain)/routes via series of unrelated SMTP servers(in case of different receiver domain).

Need of e-mail server set up in Application

  • Lets say we have an application which collects data from the user and create the monthly statistical report and sends the report to the users via email.
  • Application like e-commerce sites need to send updates on the status of the purchased products to its users via email.
  • Banking applications keep track of the transactions made by the users and send them the monthly statements on their transactions via mail.
In Such cases we need to have e-mail server set up for our Application.

How to set up e-mail server in ColdFusion?

          In order to perform email sending operation in ColdFusion, First we need to set up a SMTP mail server for our use. If we don't have a mail server of our own, localhost can act as a mail server and ColdFusion will act normally, but emails will not arrive at their destination due to unavailability of mail server set up in localhost.

          We can use Gmail,Yahoo SMTP server setting for setting up mail server in ColdFusion.
Although we can tell ColdFusion,which server to use by adding it to the cfmail tag, here we will use ColdFusion Administrator to set up our mail server.

Mail Server Set up via ColdFusion Administrator

Steps:

1) Once we login to CF Admin page, under server setting, we have Mail menu.
If we have no mail server setting present then we can add new mail server setting and if any existing mail server setting present, it will show under this menu for changing the existing setting.
2)  

Using GMAIL SMTP server setting**:

  • Mail server: smtp.gmail.com
  • User name: Gmail email address(eg. abcdef@gmail.com)
  • Password: Corresponding Gmail password
  • Server Port: 465
  • Enable SSL: yes

OR

Using YAHOO SMTP server setting:

  • Mail server: smtp.mail.yahoo.com
  • User name: Yahoo email address(eg. abcdef@yahoo.in)
  • Password: Corresponding Yahoo password
  • Server Port: 465
  • Enable SSL: yes


3) Once these setting are done, in order to verify the setting works or not we must check the "Verify Mail Server Connection" and submit the Setting.

4) Upon successful mail server setting we will get a message stating "Server has been updated successfully" and "Connection Verification Successful".

Sending Email once the mail server is configured

We have only to write the code with From, To addresses,Subject and Body using cfmail tag.

Code:
<!--- For sending email  --->
<cfmail from="testby4@yahoo.com" subject="Test Email" to="testme43@gmail.com">
This is a Test mail
</cfmail>
          The above code will send mail to "testme43@gmail.com" with the mentioned Subject and Body via the mail server set up in ColdFusion Administrator.

Important Observation

          When you configure the mail server in ColdFusion Administrator with GMAIL SMTP server settings, upon sending email from different "FROM" addresses in code, you can notice that the From address remains the same as mentioned in the ColdFusion Mail Server Setting,where as if the mail server setting is configured with YAHOO SMTP server setting, upon sending email from different "FROM" addresses, the From addresses are the actual FROM addresses mentioned in the code.

          So before using the GMAIL SMTP server you must be aware that whether you send emails from different "FROM" addresses in code , all the emails will be fired to the receiver with a single FROM address.

**Alternate GMAIL SMTP server setting
  • Mail server: smtp.gmail.com
  • User name: Gmail email address(eg. abcdef@gmail.com)
  • Password: Corresponding Gmail password
  • Server Port: 587
  • Enable TLS: yes


Sunday, October 19, 2014

Perform Scheduled Tasks Using ColdFusion

What is Scheduled Tasks?

      Those tasks planned earlier to be performed at a particular time in future is known as Scheduled Tasks.

Examples:

1 - Birthday notification in any social networking sites.
For the above process to take place, there is some task need to run on everyday at 12:00 AM to find out the persons having birthday on that day and notify to the connected users regarding the birthday notification.
2 - Monthly bank statement reports via SMS/Email.
Here, there must be some task which will run on a particular day in a month and gather all the transaction details and create a report and send it to the user via SMS/Email.

How Scheduled Tasks work?

      This is done by "Scheduler" that provides the ability to schedule the launch of programs or scripts at predefined times or after specified time intervals.

      In ColdFusion we have an inbuilt Scheduler which is available with the installation of ColdFusion package.ColdFusion scheduled task enable us to schedule any ColdFusion template to run at any time.

How to create Scheduled Tasks?

      ColdFusion scheduled tasks can be configured either via the ColdFusion Administrator or programmatically (using CFSCHEDULE tag).

Via ColdFusion Administrator

Steps:

 1) Once we login to CF Admin page, under server setting, we have scheduled task menu.
If we have some existing scheduler present it will show under scheduled task menu for Starting/Pausing/Editing/Deleting individual scheduled tasks.
 2) For creating a new scheduled task, click on "Schedule New Task" button.



 3) Provide Task name,Duration,Frequency and URL and click "Submit" for the scheduled task to be created in ColdFusion.
Task name refers to the name for the scheduled task. 
Duration refers to the starting and ending date(optional) in which the task will run.(If we don't specify an end date ColdFusion calculate the remaining count of the task basing on the frequency provided) 
Frequency refers to the no of times the task will run.(In 5 different category we can set the frequency).
  • One time task will run at the specified time for a single time and then will be Expired. 
  • Recurring task provide us to configure the scheduled task(daily,weekly,monthly) at a specific time till the End date.
  • Daily every task provide us to configure the scheduled task per(hour,minute,second) basis with Start,End time and no of times on each day.
  • Crontime takes an Corn-Expression* for the configuration of the scheduled task.
  • Chained task provide us to configure a child task that is chained to another parent task.(It executes after the parent task) 
Note:If we don't provide an end date the scheduler will run it for infinite period of time for Recurring and Daily every task schedules.
URL is the path of the file that is used to perform the task.(Sending notificaion/Email/SMS).
Submit completes the configuration of the scheduled task successfully.
 Now the scheduled task will run at the scheduled time.


Programmatically:

Steps:


  1.  Create a ColdFusion template.
  2.  Use the CFSCHEDULE tag using the UPDATE action.
  3.  Save the template inside the ColdFusion application.
  4.  Run the template by opening it in any browser.
  5.  Running the template will either update the ColdFusion scheduled task, or create it if it doesn't already exist.
We can now find this scheduled task listed in the ColdFusion Administrator under scheduled tasks.

 Code:
<!--- For creating or editing a scheduled task  ---> 
<cfschedule
action="update"
task="Notification task"
URL="http://dev.testsite.com/scheduledtasks/notifyScheduler.cfm"
interval="daily"
operation="HTTPRequest"
startDate="#CreateDate(2014,10,21)#"
startTime="#CreateTime(0,0,0)#">
 We can also use the below code to pause,resume,delete and run the scheduled tasks.
 Code:
 <!--- For pausing a scheduled task  --->  
<cfschedule  
action="pause"
task="Notification task"> 
<!--- For resuming a scheduled task  --->  
<cfschedule  
action="resume"
task="Notification task">
<!--- For deleting a scheduled task  --->  
 <cfschedule  
action = "delete"  
task = "Notification task">
<!--- For running a scheduled task  --->  
 <cfschedule  
action = "run"  
task = "Notification task">
All the above operations have same effect on the tasks as doing via ColdFusion Administrator.

     In order to perform scheduled task configuration operations programmatically we must ensure the CFSCHEDULE tag is not disabled by Administrator Sandbox security setting for the template we are using to configure it.

NOTE: The CFSCHEDULE tag (and many others) can be disabled via the ColdFusion Administrator(Sandbox security page).

How to know the status of the Scheduled Tasks?

      In order to know the status of the scheduled tasks we need to check the scheduler log file located under CF_directory.(<CF_Directory>\cfusion\logs\scheduler.log)

Sample:

      In the log file the required informations(Severity, ThreadID, Date, Time, Application, Message) for the scheduled task are logged once it runs. 
      In logged Message mostly we will find :
  • "Task <TaskName> triggered." - This refers that the scheduled task started at correct time.
  • "[<TaskName>] Pausing the task because of user request at <logged time>" - This refers that the scheduled task has been paused by the user.
  •  "Task <TaskName> misfired." - Misfire** is a fancy name for a task that didn't start when it should have.

*Cron-Expressions are strings made up of seven sub-expressions (Seconds, Minutes, Hours, Day-of-Month, Month, Day-of-Week, and Year (optional field)), that describe individual details of the schedule(http://www.quartz-scheduler.org/docs/tutorial/TutorialLesson06.html)

**If the task was scheduled to start at 10:00:00 and it is now 10:00:01 and the task has not started, it is now considered misfired.(Misfire can be caused by various reasons like configuring the start of a scheduled task in past,unavailability of worker threads,scheduler engine down etc.)