Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
systems:yii2:email_tools [2018/02/23 12:10] smayr created |
systems:yii2:email_tools [2018/05/14 11:08] (current) smayr [Setup Captcha] |
||
---|---|---|---|
Line 61: | Line 61: | ||
</ | </ | ||
- | To send an email with a form (view), you may use the following code in a controller: | + | To send an email with a form (view), you may use the following code in a controller. |
+ | Eg: controller '' | ||
<code php> | <code php> | ||
- | Yii:: | + | class SiteController extends \yii\web\Controller |
- | -> | + | { |
- | -> | + | /** |
- | -> | + | * @inheritdoc |
- | -> | + | */ |
+ | public function behaviors() | ||
+ | { | ||
+ | return [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | //... | ||
+ | [ | ||
+ | // any user (authenticated or not) | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ], | ||
+ | ]; | ||
+ | } | ||
+ | |||
+ | //... | ||
+ | |||
+ | private function sendMessage($srcMail, | ||
+ | { | ||
+ | | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | return $success; | ||
+ | } | ||
</ | </ | ||
Line 117: | Line 146: | ||
<?= $form-> | <?= $form-> | ||
<?= $form-> | <?= $form-> | ||
+ | | ||
+ | <? | ||
+ | // To regenerate new captcha after each refresh, call getVerifyCode(). | ||
+ | // See: http:// | ||
+ | $this-> | ||
+ | ?> | ||
<?= $form-> | <?= $form-> | ||
' | ' | ||
Line 407: | Line 442: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | == Setup Captcha == | ||
+ | |||
+ | '' | ||
+ | |||
+ | In controller' | ||
+ | <code php> | ||
+ | /** | ||
+ | * Site controller | ||
+ | */ | ||
+ | class SiteController extends Controller | ||
+ | { | ||
+ | //... | ||
+ | | ||
+ | /* | ||
+ | * @inheritdoc | ||
+ | */ | ||
+ | public function behaviors() | ||
+ | { | ||
+ | return [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | //... | ||
+ | [ | ||
+ | // any user (authenticated or not) | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ], | ||
+ | //... | ||
+ | ]; | ||
+ | } | ||
+ | | ||
+ | //... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In controller, add '' | ||
+ | <code php> | ||
+ | /** | ||
+ | * Site controller | ||
+ | */ | ||
+ | class SiteController extends Controller | ||
+ | { | ||
+ | //... | ||
+ | |||
+ | /** | ||
+ | * @inheritdoc | ||
+ | */ | ||
+ | public function actions() | ||
+ | { | ||
+ | return [ | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ]; | ||
+ | } | ||
+ | //... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In model using the captcha, add '' | ||
+ | <code php> | ||
+ | /** | ||
+ | * ContactForm is the model behind the contact form. | ||
+ | */ | ||
+ | class ContactForm extends Model | ||
+ | { | ||
+ | public $name; | ||
+ | public $email; | ||
+ | public $subject; | ||
+ | public $body; | ||
+ | public $verifyCode; | ||
+ | |||
+ | /** | ||
+ | * @return array the validation rules. | ||
+ | */ | ||
+ | public function rules() | ||
+ | { | ||
+ | return [ | ||
+ | // name, email, subject and body are required | ||
+ | [[' | ||
+ | // email has to be a valid email address | ||
+ | [' | ||
+ | // verifyCode needs to be entered correctly | ||
+ | [' | ||
+ | ]; | ||
+ | } | ||
+ | | ||
+ | //... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In form view using the captcha, add '' | ||
+ | <code php> | ||
+ | <? | ||
+ | // To regenerate new captcha after each refresh, call getVerifyCode(). | ||
+ | // See: http:// | ||
+ | | ||
+ | ?> | ||
+ | <?= $form-> | ||
+ | ' | ||
+ | ' | ||
+ | ]) ?> | ||
+ | </ | ||
+ | |||
+ | See more: | ||
+ | * [[https:// |