55 行
1.1KB

  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * ContactForm is the model behind the contact form.
  7. */
  8. class MailForm extends Model
  9. {
  10. public $subject;
  11. public $body;
  12. /**
  13. * @inheritdoc
  14. */
  15. public function rules()
  16. {
  17. return [
  18. // name, email, subject and body are required
  19. [['subject', 'body'], 'required', 'message' => 'Champs obligatoire'],
  20. ];
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'subject' => 'Sujet',
  29. 'body' => 'Message',
  30. ];
  31. }
  32. /**
  33. * Sends an email to the specified email address using the information collected by this model.
  34. *
  35. * @param string $email the target email address
  36. * @return boolean whether the email was sent
  37. */
  38. public function sendEmail($email)
  39. {
  40. return Yii::$app->mailer->compose()
  41. ->setTo($email)
  42. ->setFrom(['matthieu@lechatdesnoisettes.com' => 'Le Chat des Noisettes'])
  43. ->setSubject($this->subject)
  44. ->setTextBody($this->body)
  45. ->send();
  46. }
  47. }