Drupal investigation

ContactForm.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\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. * @return array the validation rules.
  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. * @return array customized attribute labels
  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. * @param string $email the target email address
  41. * @return bool whether the model passes validation
  42. */
  43. public function contact($email)
  44. {
  45. if ($this->validate()) {
  46. Yii::$app->mailer->compose()
  47. ->setTo($email)
  48. ->setFrom([$this->email => $this->name])
  49. ->setSubject($this->subject)
  50. ->setTextBody($this->body)
  51. ->send();
  52. return true;
  53. }
  54. return false;
  55. }
  56. }