ContactForm.php 1.09 KB
Newer Older
1 2
<?php

3
namespace frontend\models;
4

5
use Yii;
6 7 8 9 10 11 12 13 14 15 16 17 18 19
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model
{
	public $name;
	public $email;
	public $subject;
	public $body;
	public $verifyCode;

	/**
20
	 * @inheritdoc
21 22 23
	 */
	public function rules()
	{
24
		return [
25
			// name, email, subject and body are required
26
			[['name', 'email', 'subject', 'body'], 'required'],
27
			// email has to be a valid email address
slavcodev committed
28
			['email', 'email'],
29
			// verifyCode needs to be entered correctly
slavcodev committed
30
			['verifyCode', 'captcha'],
31
		];
32 33 34
	}

	/**
35
	 * @inheritdoc
36 37 38
	 */
	public function attributeLabels()
	{
39
		return [
40
			'verifyCode' => 'Verification Code',
41
		];
42 43 44 45
	}

	/**
	 * Sends an email to the specified email address using the information collected by this model.
46
	 *
47
	 * @param string $email the target email address
48
	 * @return boolean whether the email was sent
49
	 */
50
	public function sendEmail($email)
51
	{
52 53 54 55 56 57
		return Yii::$app->mail->compose()
			->setTo($email)
			->setFrom([$this->email => $this->name])
			->setSubject($this->subject)
			->setTextBody($this->body)
			->send();
58
	}
59
}