Class app\models\ContactForm

Inheritanceapp\models\ContactForm » yii\base\Model
Source Code https://github.com/yiisoft/yii2-app-basic/blob/master/models/ContactForm.php

ContactForm is the model behind the contact form.

Public Methods

Hide inherited methods

Method Description Defined By
attributeLabels() app\models\ContactForm
contact() Sends an email to the specified email address using the information collected by this model. app\models\ContactForm
rules() app\models\ContactForm

Property Details

Hide inherited properties

$body public property
public $body null
$email public property
public $email null
$name public property
public $name null
$subject public property
public $subject null
$verifyCode public property
public $verifyCode null

Method Details

Hide inherited methods

attributeLabels() public method

public array attributeLabels ( )
return array

Customized attribute labels

                public function attributeLabels()
{
    return [
        'verifyCode' => 'Verification Code',
    ];
}

            
contact() public method

Sends an email to the specified email address using the information collected by this model.

public boolean contact ( $email )
$email string

The target email address

return boolean

Whether the model passes validation

                public function contact($email)
{
    if ($this->validate()) {
        Yii::$app->mailer->compose()
            ->setTo($email)
            ->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
            ->setReplyTo([$this->email => $this->name])
            ->setSubject($this->subject)
            ->setTextBody($this->body)
            ->send();
        return true;
    }
    return false;
}

            
rules() public method

public array rules ( )
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'],
    ];
}