Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:email_tools [2018/05/10 16:11]
smayr [Setup Emailer (Swiftmailer)]
systems:yii2:email_tools [2018/05/14 11:08] (current)
smayr [Setup Captcha]
Line 61: Line 61:
 </code> </code>
  
-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 ''@app/controllers/SiteController'':
 <code php> <code php>
-Yii::$app->mailer->compose('contact/html' /* view */, ['contactForm' => $form] /* view params */) +class SiteController extends \yii\web\Controller 
-    ->setFrom('from@domain.com'+
-    ->setTo($form->email) +    /** 
-    ->setSubject($form->subject) +     * @inheritdoc 
-    ->send();+     */ 
 +    public function behaviors() 
 +    { 
 +        return [ 
 +        'access' => [ 
 +                'class' => AccessControl::className(), 
 +                'rules' => [ 
 +                    //... 
 +                    [ 
 +                        // any user (authenticated or not) 
 +                        'actions' => ['contact', 'captcha', ...], 
 +                        'allow' => true, 
 +                    ], 
 +                ], 
 +            ], 
 +        ]; 
 +    } 
 +     
 +    //... 
 +     
 +    private function sendMessage($srcMail, $srcName, $dstEmail, $subject, $textBody) 
 +    { 
 +       $success = Yii::$app->mailer->compose('contact/html' /* view */, ['contactForm' => $form] /* view params */) 
 +          ->setFrom('from@domain.com'
 +          ->setTo($form->email) 
 +          ->setSubject($form->subject) 
 +          ->send(); 
 +      return $success; 
 +    }
 </code> </code>
  
Line 117: Line 146:
                 <?= $form->field($model, 'subject') ?>                 <?= $form->field($model, 'subject') ?>
                 <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>                 <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
-                <!-- // To regenerate new captcha after each refresh, call getVerifyCode().  +                 
-                     // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing +                <?php  
-                 --> +                    // To regenerate new captcha after each refresh, call getVerifyCode().  
-                <?php $this->context->createAction('captcha')->getVerifyCode(true) ?>+                    // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing 
 +                    $this->context->createAction('captcha')->getVerifyCode(true)
 +                ?>
                 <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [                 <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',                     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
Line 411: Line 442:
 } }
 </code> </code>
 +
 +== Setup Captcha ==
 +
 +''CaptchaAction'' requires either GD2 extension or ImageMagick PHP extension. Check that it meets this requirement.
 +
 +In controller's function ''behaviors()'', add ''captcha'' to ''actions'' for user group that requires the use ot if:
 +<code php>
 +/**
 + * Site controller
 + */
 +class SiteController extends Controller
 +{
 +    //...
 +    
 +    /*
 +     * @inheritdoc
 +     */
 +    public function behaviors()
 +    {
 +        return [
 +            'access' => [
 +                'class' => AccessControl::className(),
 +                'rules' => [
 +                    //...
 +                    [
 +                        // any user (authenticated or not)
 +                        'actions' => ['contact-us', 'captcha', ...],
 +                        'allow' => true,
 +                    ],
 +                ],
 +            ],
 +            //...
 +        ];
 +    }
 +    
 +    //...
 +}
 +</code>
 +
 +In controller, add ''CaptchaAction'' to ''actions()'':
 +<code php>
 +/**
 + * Site controller
 + */
 +class SiteController extends Controller
 +{
 +    //...
 +
 +    /**
 +     * @inheritdoc
 +     */
 +    public function actions()
 +    {
 +        return [
 +            'error' => [
 +                'class' => 'yii\web\ErrorAction',
 +            ],
 +            'captcha' => [
 +                'class'           => 'yii\captcha\CaptchaAction',
 +                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
 +                'testLimit'       => 1,
 +                'foreColor'       => 0xA02040,
 +            ],
 +        ];
 +    }
 +    //...
 +}
 +</code>
 +
 +In model using the captcha, add ''verifyCode'' entry to the ''rules()'' function:
 +<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
 +            [['name', 'email', 'subject', 'body'], 'required'],
 +            // email has to be a valid email address
 +            ['email', 'email'],
 +            // verifyCode needs to be entered correctly
 +            ['verifyCode', 'captcha', 'captchaAction' => 'site/captcha'],
 +        ];
 +    }
 +    
 +    //...
 +}
 +</code>
 +
 +In form view using the captcha, add ''verifyCode'' field:
 +<code php>
 +<?php 
 +   // To regenerate new captcha after each refresh, call getVerifyCode(). 
 +   // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing/.
 +   $this->context->createAction('captcha')->getVerifyCode(true); 
 +?>
 +<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
 +     'captchaAction' => 'site/captcha',  // redirect to correct controller where captcha is defined
 +     'template'      => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
 +]) ?>
 +</code>
 +
 +See more:
 +  * [[https://www.yiiframework.com/doc/api/2.0/yii-captcha-captchaaction|Yii CaptchaAction]]