0
0

ContactForm.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace frontend\models;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * ContactForm is the model behind the contact form.
  7. */
  8. class ContactForm extends Model
  9. {
  10. public $name;
  11. public $email;
  12. public $subject;
  13. public $body;
  14. public $verifyCode;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. // name, email, subject and body are required
  22. [['name', 'email', 'subject', 'body'], 'required'],
  23. // email has to be a valid email address
  24. ['email', 'email'],
  25. // verifyCode needs to be entered correctly
  26. ['verifyCode', 'captcha'],
  27. ];
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function attributeLabels()
  33. {
  34. return [
  35. 'verifyCode' => 'Verification Code',
  36. ];
  37. }
  38. /**
  39. * Sends an email to the specified email address using the information collected by this model.
  40. *
  41. * @param string $email the target email address
  42. * @return bool whether the email was sent
  43. */
  44. public function sendEmail($email)
  45. {
  46. return Yii::$app->mailer->compose()
  47. ->setTo($email)
  48. ->setFrom([$this->email => $this->name])
  49. ->setSubject($this->subject)
  50. ->setTextBody($this->body)
  51. ->send();
  52. }
  53. }