FundaLetter

From FundaWiki

Jump to: navigation, search

Short guide - more will be added later.

1. Add a subscription page with the following code

subscribe page

$GLOBALS["D"] =& $this->D;
include MODULE_ROOT."fundaLetter/includes/datastructure.inc";
include MODULE_ROOT."fundaLetter/includes/library.inc";
include MODULE_ROOT."fundaLetter/includes/subscriptionmanager.class";

// Send e-mail to everyone
if (isset($_REQUEST["name"]) && !empty($_REQUEST["name"])) {

    $name = urldecode($_REQUEST["name"]);

    // Send subscription e-mail to owner
    $mail = "
A visitor on the website has subscribed to the newsletter

Name: $name
";

    @mail("owner@example.com", "Some domain - New newsletter subscriber", $mail,
               "From: newsletter-subscribe@example.com\r\n");

    print '<br><strong>Subscription:</strong><br>';

    // Collect information + sanity checking
    if (isset($_REQUEST["email"]) && !empty($_REQUEST["email"])) {
        $email = urldecode($_REQUEST["email"]);

        if (isset($_REQUEST["news_list"]) && is_numeric($_REQUEST["news_list"])) {
            $list = new LetterList($_REQUEST["news_list"]);
            // News letter list could not be found in database.
            if ($list == FALSE) {
                print "<br><strong>Error:</strong> The specified newsletter list could not be found in our databases.";
                exit;
            }
        } else {
            exit;
        }

        $manager = new SubscriptionManager();
        $result = $manager->Subscribe($email, $list);

        switch ($result) {
            case $manager->RESULT_USER_EXIST:
                print '<br><strong>Error:</strong> The specified e-mail address is already subscribed to the newsletter list.';
                print '<br><br><a href="javascript:history.back();">« Back</a>';
                break;

            case $manager->RESULT_MTA_ERROR:
                print '<br><strong>Error:</strong> An error occured while sending the confirmation e-mail. Try again, and if the error continue to exist please send an e-mail to <a href="mailto:owner@example.com">owner@example.com</a>.';
                print '<br><br><a href="javascript:history.back();">« Back</a>';
                break;
        
            case $manager->RESULT_MAILSENT:
                print 'The subscription succeeded - the confirmation e-mail has been sent to the e-mail address.';
                break;
        
            default:
                break;
        }

    } else if (isset($_REQUEST["email"]) && empty($_REQUEST["email"])) {
        print '<br><strong>Error:</strong> An e-mail address was not specified.';
        print '<br><br><a href="javascript:history.back();">« Back</a>';
    }
} else {

print '
<form style="margin: 4px;" action="'. $this->printUrl($this->ID, true) .'" method="get" onsubmit="return validateInput(this);">
<input type="hidden" name="news_list" value="1">
<input type="hidden" name="ID" value="'. $this->ID .'">
<input type="hidden" name="lang" value="'. $this->lang .'">

<div style="margin-top: 5px;">
  <div style="width: 100px; float: left;">Name:</div>
  <div><input type="text" name="name" value="" size="40" style="border: 1px solid #858384;"> *</div>
</div>
<div style="margin-top: 5px;">
  <div style="width: 100px; float: left;">E-mail:</div>
  <div><input type="text" name="email" value="" size="40" style="border: 1px solid #858384;"></div>
</div>
<div style="margin-top: 15px;">
  <div style="width: 100px; float: left;"> </div>
  <div>Fields with * are required</div>
</div>
<div style="margin-top: 15px;">
  <div style="width: 100px; float: left;"> </div>
  <div><input type="submit" name="subscribe" value="Subscribe" style="border: 1px solid #858384; background-color: #e9e9e9;"></div>
</div>
<div style="clear: both;"> </div>

</form>
';

}

news_list is set to the ID of the news letter list you want users to subscribe to. Otherwise a dropdown could be created.

The mail() must be customized and the error messages can be localized.

Activation page

2. Create a hidden page (maybe as a sub page to the subscription page) with the following code.

$GLOBALS["D"] =& $this->D;

include MODULE_ROOT."fundaLetter/includes/datastructure.inc";
include MODULE_ROOT."fundaLetter/includes/library.inc";
include MODULE_ROOT."fundaLetter/includes/subscriptionmanager.class";

// Collect information + sanity checking
$code = $_REQUEST["code"];
$action = $_REQUEST["action"];

$manager = new SubscriptionManager();
$result = $manager->Activate($code, $action);

switch ($result) {
    case $manager->RESULT_ACTIVATION_ERROR:
        print "Confirmation code could not be accepted";
        break;

    case $manager->RESULT_USER_SUBSCRIBED:
        print "Your subscription has been activated.";
        break;

    case $manager->RESULT_USER_UNSUBSCRIBED:
        print "Your unsubscription has been activated and your e-mail address has been removed from our database.";
        break;

    default:
        break;
}

Unsubscription page

$GLOBALS["D"] =& $this->D;

include MODULE_ROOT."fundaLetter/includes/datastructure.inc";
include MODULE_ROOT."fundaLetter/includes/library.inc";
include MODULE_ROOT."fundaLetter/includes/subscriptionmanager.class";

// Collect information + sanity checking
if (isset($_REQUEST["email"]) && !empty($_REQUEST["email"])) {
    $email = $_REQUEST["email"];
} else {
    exit;
}

if (isset($_REQUEST["news_list"]) && is_numeric($_REQUEST["news_list"])) {
    $list = new LetterList($_REQUEST["news_list"]);
    // News letter list could not be found in database.
    if ($list == FALSE) {
        print "The specified newsletter list could not be found in our databases.";
        exit;
    }
} else {
    exit;
}

$manager = new SubscriptionManager();
$result = $manager->Unsubscribe($email, $list);

switch ($result) {
    case $manager->RESULT_USER_NOTFOUND:
        print "The specified e-mail address does not subscribe to the newsletter";
        break;

    case $manager->RESULT_CONFIRM_EXIST:
        print "The confirmation e-mail hasalready been sent.";
        break;

    case $manager->RESULT_MTA_ERROR:
        print '<br><strong>Error:</strong> An error occured while sending the confirmation e-mail. Try again, and if the error continue to exist please send an e-mail to <a href="mailto:owner@example.com">owner@example.com</a>.';
        print '<br><br><a href="javascript:history.back();">« Back</a>';
        break;

    case $manager->RESULT_MAILSENT:
        print "Unsubscription started - confirmation e-mail has been sent.";
        break;

    default:
        break;
}

4. fundanemt/modules/fundaLetter/config.php is modified

The three url variables is hardcoded to point to the different pages.

Personal tools