Hook before a user is sent confirm subscription email?

Lakjin

Active Member
What is the code to write an extension where I can do something with an email address after a user submits a form to subscribe but before the email confirmation email is sent?
 
Each model, be it list subscriber, user, customer, etc, has a set of hooks defined for when they are saved, validated, etc.
For list subscriber, the hook name you are looking for is:
Code:
frontend_model_listsubscriber_aftersave
But keep in mind this is triggered absolutely each time when a subscriber is saved, be it a new record or an existing record.
So in your case, you could do something like:
PHP:
Yii::app()->hooks->addAction('frontend_model_listsubscriber_aftersave', function($subscriber){

    // make sure we only trigger this in the ListsController::actionSubscribe
    if (Yii::app()->getController()->getRoute() != 'lists/subscribe') {
        return;
    }

    // if this is not a new record, we stop
    if (!$subscriber->getIsNewRecord()) {
        return;
    }

    // do whatever you wish with $subscriber
});
The above is not tested, but should give you an idea at least.
 
Each model, be it list subscriber, user, customer, etc, has a set of hooks defined for when they are saved, validated, etc.
For list subscriber, the hook name you are looking for is:
Code:
frontend_model_listsubscriber_aftersave
But keep in mind this is triggered absolutely each time when a subscriber is saved, be it a new record or an existing record.
So in your case, you could do something like:
PHP:
Yii::app()->hooks->addAction('frontend_model_listsubscriber_aftersave', function($subscriber){

    // make sure we only trigger this in the ListsController::actionSubscribe
    if (Yii::app()->getController()->getRoute() != 'lists/subscribe') {
        return;
    }

    // if this is not a new record, we stop
    if (!$subscriber->getIsNewRecord()) {
        return;
    }

    // do whatever you wish with $subscriber
});
The above is not tested, but should give you an idea at least.
Thank you. Two questions.

Is it possible to get list information in the function as well, in addition to the subscriber? Also, is this triggered only when a user subscribers through a form or also when through API?
 
Is it possible to get list information in the function as well, in addition to the subscriber?
Sure, accessing
PHP:
$list = $subscriber->list;
Gives you the list.

Also, is this triggered only when a user subscribers through a form or also when through API?
The above is only triggered for the frontend area. For api you have more control since you do that programatically anyway.
 
Question. Does this hook trigger ONLY when a subscriber is successfully saved? For example, let's say someone on the blacklist is trying to subscribe. Does this trigger as soon as they hit submit? What I'm trying to do is manually erase an email off the blacklist so that they can subscribe again, if they submit via form.
 
Good question. It triggers on success save.
You can as well do
Code:
$subscriber->getIsConfirmed();
// or
$subscriber->getIsBlacklisted();
To see if the sub is still confirmed or has been blacklisted.
 
Back
Top