Send Messages to contacts

When sending message to Contacts in Ofuz we suggest to use the Contact object Method Contact::sendMessage()

The method send the message by Email but will in a near future send it through Twitter (private message) and, if allowed, through facebook.

It also include an opt-out feature in the footer in case one of the Ofuz users is spaming his contacts.

To send a message you need an instantiated Contact object and an EmailTemplate

Simple Example

Quick example to send on email message to a Contact (idcontact = 2342).

 
    $contact = new Contact();
    $contact->getId(2342);
    $contact->sendMessage(new EmailTemplate("regthank"));
 

This will use EmailTemplate object called: regthank stored in the email_template table and send it to the Contact with id 2342. The email will have for sender the currently signed in User.

Custom template Example

This example uses a Email Template in the PHP code.

    $do_template = new EmailTemplate();
    $do_template->setSubject("This is an example")
                ->setMessage("[firstname], \nThis is the content of the sample message");
 
    $contact = new Contact();
    $contact->getId(2342);
    $contact->sendMessage($do_template);

This will send a simple text email to the Contact with id 2342. By default the email sender is the currently signed in User. To change the sender add the setFrom() method:

    $do_template->setFrom("phil@sqlfusion.com", "Philippe Lewicki")
                ->setSubject("This is an example")
                ->setMessage("[firstname], \nThis is the content of the sample message");

Messages managed this way do not take advantage of the multi-language feature of Email_Templates stored in the email_template table.

Data Merge example

Email template can contain merge fields like [firstname] or [lastname]. The merge fields are in between [].

By default the Contact::sendMessage() will use all the variables of Contact Object.

To use a different set of variables pass them as an Array to the sendMessage() method.

For example lets send a message to all contacts with the tag testing to confirm their phone numbers.

<?php
 
    $do_template = new EmailTemplate();
    $do_template->setSubject("{testing} Phone number confirmation")
                 ->setMessage("[firstname], 
                               This is the phone numbers we have in file:
 
                               [phone_numbers]
 
                               Please send me an email if any of those are incorrect.
                               Thank you, 
 
                               Philippe");
 
    $contacts_from_tag = new Tag();
    $contacts_from_tag->query("SELECT idreference FROM tag 
                               WHERE iduser='".$_SESSION['do_User']->iduser."' 
                               AND reference_type='contact' 
                               AND tag_name='testing' ");
 
    while ($contacts_from_tag->next()) {
        $contact = new Contact();
        $contact->getId($contacts_from_tag->idreference) ;
        $phones = $contact->getChildContactPhone();
        $str_phone_numbers = '';
        while ($phones->next()) {
            $str_phone_numbers .= "\n".$phones->phone_type.': '.$phones->phone_number;
        }
        if (!empty($str_phone_numbers)) {
            $merge_values = array_merge(
                             $contact->getValues(), 
                             Array('phone_numbers'=>$str_phone_numbers)
                             );
 
            $contact->sendMessage($do_template, $merge_values);
        }
    }
?>

So first we create an EmailTemplate object with 2 merge fields: [firstname] and [phone_numbers] Next we select the contact id associated with the tag testing for the currently sign-in user with the following SQL Query:

       SELECT idreference FROM tag 
                          WHERE iduser='".$_SESSION['do_User']->iduser."' 
                            AND reference_type='contact' 
                            AND tag_name='testing'

We use the persistent object do_User to get the currently sign in iduser. do_User object is created from the User class when the user sign in Ofuz and its destroy when the user sign-out.

The last part is about getting the phone numbers for each contact and cumulating them in a string str_phone_numbers. That string is then merged with the Contact variables and passed to the Contact→sendMessage() property.

 
tutorial/send_emails_to_contacts.txt · Last modified: 2010/08/24 18:35 by philippe
 
Copyright © SQLFusion LLC all rights reserved