Send automatic birthday greeting

The next enhancement to your web application is meant to ease the task of checking daily which contact's birthday it is and use your e-mail client to send a greeting. It will all be done in an automatic manner at the click of a button.

You will have to create a new page in your site's root folder, which will contain the elements that send the e-mail. To create it, select the New option of the File menu, in the Files tab. If the Files tab is not visible, open it from Window -> Files. Give it a suggestive name (e.g. birthday.php). You will later on add a link in the main page, which will point to this one.

Sending the birthday greetings will be done in a semi-automatic manner at first, the page displaying a list of all persons that were born on this day, and allowing you to decide whether to send the e-mail or not. The automatic part is that you'll only have to click one single button to send the greeting to all people in the list.

To start creating the page, open it in Dreamweaver, and continue with this tutorial: you will create the design first, and later on, you'll add the code that does all of the actions.

 

When creating the basic design, the approach is the same as for the contact or company insert and update pages:

  1. On top of the page, using a header 2 format, place the page title: "Persons born today".
  2. Place a horizontal rule, to separate the header from the content.
  3. Create a new paragraph where to display the list.

The list of people born on the day the page is being accessed is in fact a dynamic table, as it should retrieve the names from the contacts table. Before using the Dynamic Table command however, you have to create a recordset containing the names.

To create the recordset, follow the instructions below:

  1. Click on the Plus (+) button of the Bindings panel. If the Bindings panel is not visible, open it from Window -> Bindings.
  2. Select Recordset (Query) from the pop-up menu.
  3. The Recordset dialog box opens in the Basic form. Switch to the Advanced form by clicking on the Advanced button. In this form, the user interface allows you to enter the SQL query by hand. This way, you can create more complex queries that do exactly what you need.
  4. In the Connection drop-down menu select the database connection used for your site.
  5. In the SQL text area, enter the following query:
    For MySQL:

    SELECT *
    FROM contact_con
    WHERE date_format(birthday_con, '%m%d') = date_format(now(), '%m%d')


    For Microsoft Access:

    SELECT *
    FROM contact_con
    WHERE birthday_con = Date()


    Note
    : Microsoft Access uses the same date format as your operating system to store date values.

    For Microsoft SQL Server:
    SELECT *
    FROM dbo.contact_con
    WHERE DAY(dbo.contact_con.birthday_con) = DATEPART(DAY,GETDATE())
    and MONTH(dbo.contact_con.birthday_con) = DATEPART(MONTH,GETDATE()).


    For PostgreSQL:
    SELECT *
    FROM contact_con
    WHERE birthday_con = NOW()

  6. The query above is used to retrieve from the contact_con table only the entries containing in their birthday field the same day-month combination as for the current date. The date_format function retrieves from the data source specified as the first parameter, only the format specified by the second parameter. The call: date_format("2005-03-15", '%m%d') will return "03-15", meaning the month and day. The now() function returns the current date.
  7. As you can see, the query above returns only the entries that have the birthday field set on the current day. The SQL query is the last thing done on the new recordset dialog box. Click the Test button to see if anyone is born on this day, or the OK button to add the recordset into the page.


       

After having created the recordset, you can create the listing of its content, with the use of a dynamic table. To add a dynamic table, click on its button in the Application tab of the Insert bar. Configure it to display entries from the recordset created earlier, rsContacts, and to display all records. Also, set the table border, cell spacing and padding to 0. Clicking on OK will add the dynamic table into the page.

 

At this point, a table with all data concerning the contacts has been added in the last page paragraph. Not all of the entries are useful, or meaningful, as only the actual contact name is of interest. Therefore, delete all table columns except the name_con one.

For the remaining column, change the name_con static text into a more understandable one: "Name". Also, set the cell as having the header property, from the Property inspector.

Now your page should look like this:

 

Next comes the switch that will start the mail sending operation: an HTML form button. Place one in a new paragraph below the dynamic table. When asked if a form tag should be added, answer Yes. To make the button more intuitive, replace the Submit label with: "Send birthday greetings". Leave the button's name as is, "Submit".

The mail sending is implemented through the use of one of MX Kollection 3's trigger: the Send E-mail to Recipients from Recordset. However, as any trigger, it needs a transaction on page. As there is no need to add, update or delete anything, the only one that will suit this purpose is the custom transaction. The custom transaction allows you to define the fields to use, and the SQL which will use it. Also, you can just use it as a placeholder, with no SQL operation at all.

Add a Custom Transaction to the page, by clicking on the Plus (+) button of the Server Behaviors tab, and selecting MX Kollection -> Forms ->Advanced -> Custom Transaction. Configure it as follows:

  1. In the Connection drop-down menu select the database connection defined for your site. Although there will be no database operation involved, the transaction uses the connection for error handling.
  2. The actual mailing operation has to start only when you press the Send birthday greetings button. To enforce this, you must set it as a starting rule for the custom transaction. In the First check variable drop-down menu select Form variable and in the text field that follows, enter the button's name: Submit.
  3. In the When finished, go to text box, enter the page to return to after the e-mails have been sent. Either enter the name of the main page or select it by pressing on the Browse button.
  4. These are all of the options to set for the custom transaction. Click on the OK button to apply the configuration.

     

Now that a transaction exists on the page, you can also add triggers that register to it, as the Send E-mail trigger, for instance. Therefore, you can finally complete the last action in this tutorial - apply the Send E-mail to Recipients from Recordset trigger:

  1. To access the trigger, click on the Plus (+) button of the Server behaviors tab, and select MX Kollection -> Send E-mail -> Send E-mail to Recipients from Recordset.
  2. You can use this option instead of the regular Send E-mail trigger, as in the rsContacts recordset created earlier, only the contacts which have their birthday today are selected.
  3. Configure the trigger to send the correct e-mail to the correct addresses:
    · In the Recordset drop-down menu select rsContacts.
    · In the Email to Field select the table field that stores the e-mail address of the contact.
    · In the From text-box, enter the e-mail address from which the mail will appear to have originated. Enter an address that will identify you as the sender; e.g. admin@mysite.com.
    · In the Subject text-box enter the mail's subject. You can even enter dynamic data, by using the InterAKT Dynamic Data icon, and selecting the desired dynamic variable. For this mail, type the static text: "Happy Birthday", and then press the blue lightning icon to select the dynamic name of the contact. In the new dialog box set the values source to the rsContacts recordset, and the field to the one containing the contact's name:



    · When you click on OK in the InterAKT Dynamic Data interface, some code will be added to the Subject field. It will be replaced at runtime with its dynamic value.
    · In the Body drop-down menu, select the Write content option to type in the mail's content.
    · In the Content text area enter the message to pass to the recipient; in this case a Happy Birthday greeting from you:



    · This is all you have to configure for the trigger, in order for the e-mail sending to work. You should check the E-mail Settings in the InterAKT Control Panel, where you can set the main options for your server. If these are not properly set, the messages will not get delivered.
    · Click on the OK button to apply the Send E-mail to Recipients from Recordset server behavior.
 

Note: If using the ASP VBScript server model you must configure the E-mail server settings in the Control Panel. You must fill in the server address (or name), the port (by default it is 25), the user name and the password. Optionally, you can also set the default sender field.

This was the last step to create the birthday greetings page. You can now save, upload and test it. If no names appear in the list, it may be due to the fact that no contacts in your table are born on this day. Either try again another day, or enter a contact whose birthday is today.

When one of the contacts checks his mail, he will have a pleasant surprise:

 

Note: When using the Send E-mail to Recipients from Recordset server behavior to send messages to a large number of recipients, make sure the value of the max_execution_time variable in your php.ini configuration file is sufficiently large to support the execution of the code. Sending many e-mail messages might take longer than the default setting, depending on your server configuration and specifications. If you encounter an error similar to the following

Fatal error: Maximum execution time of 30 seconds exceeded in c:\server\site\includes\common\lib\email\Pear\Net\Socket.php on line 241
 

please increase the value of the max_execution_time variable in php.ini, or contact your network administrator.