ContactForm.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace frontend\models\form;
  9. use Yii;
  10. use yii\base\Model;
  11. /**
  12. * ContactForm is the model behind the contact form.
  13. */
  14. class ContactForm extends Model
  15. {
  16. public $name;
  17. public $email;
  18. public $subject;
  19. public $body;
  20. public $verifyCode;
  21. /**
  22. * @inheritdoc
  23. */
  24. public function rules()
  25. {
  26. return [
  27. // name, email, subject and body are required
  28. [['name', 'email', 'subject', 'body'], 'required'],
  29. // email has to be a valid email address
  30. ['email', 'email'],
  31. // verifyCode needs to be entered correctly
  32. ['verifyCode', 'captcha'],
  33. ];
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'verifyCode' => 'Verification Code',
  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. return Yii::$app->mailer->compose()
  53. ->setTo($email)
  54. ->setFrom([$this->email => $this->name])
  55. ->setSubject($this->subject)
  56. ->setTextBody($this->body)
  57. ->send();
  58. }
  59. }