1. Go to this page and download the library: Download jub/craft-newsletter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use yii\base\Event;
use juban\googlerecaptcha\GoogleRecaptcha;
Event::on(
NewsletterForm::class,
NewsletterForm::EVENT_AFTER_VALIDATE,
static function (Event $event) {
$form = $event->sender;
$isSpam = // custom spam detection logic...
if($isSpam) {
$this->addError('email', 'Spam detected!');
}
}
);
namespace juban\newsletter\adapters;
use Craft;
class MySuperNewsletterAdapter extends BaseNewsletterAdapter
{
// Declare every attributes */
public function behaviors(): array
{
// Support for setting defined in environment variables or aliases
$behaviors = parent::behaviors();
$behaviors['parser'] = [
'class' => EnvAttributeParserBehavior::class,
'attributes' => [
'apiKey'
],
];
return $behaviors;
}
/**
* @inheritdoc
*/
public static function displayName(): string
{
return 'My Service Name'; // Service name as shown in the adapter type dropdown
}
/**
* @inheritdoc
*/
public function getSettingsHtml()
{
// Render the adapter settings templates
// Adapt the path according to your module / plugin
return Craft::$app->getView()->renderTemplate('newsletter/newsletterAdapters/MySuperNewsletterAdapter/settings', [
'adapter' => $this
]);
}
/**
* Try to subscribe the given email into the newsletter mailing list service
* @param string $email
* @return bool
*/
public function subscribe(string $email): bool
{
// Call the service API here
$this->_errorMessage = null;
if(!MySuperNewsletterService::subscribe($email)) {
// If something goes wrong, store the error message
$this->_errorMessage = MySuperNewsletterService::getError();
return false;
}
return true;
}
/**
* Return the latest error message after a call to the subscribe method
* @return null|string
*/
public function getSubscriptionError(): string
{
return $this->_errorMessage;
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
// Validation rules for the adapter settings should be defined here
$rules = parent::defineRules();
$rules[] = [['apiKey'], 'trim'];
$rules[] = [['apiKey'], '
use craft\events\RegisterComponentTypesEvent;
use juban\newsletter\Newsletter;
Event::on(
Newsletter::class,
Newsletter::EVENT_REGISTER_NEWSLETTER_ADAPTER_TYPES,
static function (RegisterComponentTypesEvent $event) {
$event->types[] = MySuperNewsletterAdapter::class;
}
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.