Johan Broddfelt
/* Comments on code */

Send mail on new comments

Now when we have the functionallity for sending newsletters and we have the subscriptions setup we might just as well add mail uppdates when posts get new comments. I know there is a lot of mail stuff right now. But this should be the last post for a while on that topic. I hope this will be a short one.

First of all, we would like to allow our visitors to opt in or opt out from mail updates when they make a post. And they should also have the ability to opt in without having to make a post. Now we have a choise, should the default be to opt in or not when posting a comment? Basically that choise comes don to what we would prefere our visitors to do. In my case I want my visitors to opt in. But if they do not provide a valid mail-address then there is no point in adding them to the list, so we want to add a check for that.

// views/comment/add.php
// befor the send button
        <tr>
            <th>
              Send mail uppdates on new comments
            </th>
            <td>
                <input 
                       type="checkbox"
                       value="1"
                       name="subscribe"
                       checked="checked"
                    >
            </td>
        </tr>

// after the $com->update();
        // Add visitor to comments subscription
        if (Http::filter_input(INPUT_POST, 'subscribe') == 1) {
            if (Http::filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL)) {
                $mail = Http::filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL);
                // Check if user exist
                $uArr = $user->fetchArray('WHERE LOWER(mail) LIKE '' . $mail . ''');
                $u = $uArr[0];

                // If not then register the user
                if ((int)$u->id == 0) {
                    $u = new User();
                    $u->mail = $mail;
                    $mailArr = explode('@', $mail);
                    $u->username = $mailArr[0];
                    $u->created = date('Y-m-d H:i:s');
                    $u->status = 0; // Indictes that this is not an active user of our system.
                    $u->update();
                }

                // Add user to subscription
                $s = new Subscription();
                $s->userId = $u->id;
                $s->type = 1; // Comments
                $s->itemId = $obj->id; // The id of the current post
                $s->created = date('Y-m-d');
                $s->update();
            }
        }

I realized that we might end up adding a visitors multiple times to a list if they add more then one comment to a post. So we need to add tihs litle check in our class. This will also prevent users from subscribing muliple times on my newsletter.

//classes/Subscription.php
    function update() {
        // Check if the user is allready added to this subscription
        $res = $this->fetchArray(' WHERE user_id=' . $this->userId 
                                 . ' AND type='    . $this->type
                                 . ' AND item_id=' . $this->itemId
                                 . ' AND closed LIKE '0000-00-00''
                                );
        if (count($res) == 0) {
            parent::update();
        }
    }

Now all we need is a script that add these comments to the mail queue.

// views/comment/add.php (after you added the user to the mailinglist.)
        if (!$com->isBlocked) {
            // Send mail to all subscribers of this post
            $com->sendMail($mail, $obj);

            $m = new Mail();
            $m->to = 'your@mail.dom';
            $m->subject = 'New comment at "' . $obj->title . '"';
            $m->message = $com->name . ' (' . $com->mail . ') skriver:' . PHP_EOL  . PHP_EOL . $com->comment . PHP_EOL  . PHP_EOL . 'Link ' . Http::currentURL();
            $m->sendMail();
        }
    }

- Framework, PHP, Comments

Follow using RSS

<< SEO friendly urls RSSfeed for the masses >>

Comment

Name
Mail (Not public)
Send mail uppdates on new comments
0 comment