Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ContactForm.php 1.7KB

8 anos atrás
8 anos atrás
8 anos atrás
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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', 'message' => 'Champs obligatoire'],
  23. // email has to be a valid email address
  24. ['email', 'email','message' => 'Email incorrect'],
  25. // verifyCode needs to be entered correctly
  26. ['verifyCode', 'captcha','message'=> 'Veuillez recopier le code de vérification'],
  27. ];
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function attributeLabels()
  33. {
  34. return [
  35. 'name' => 'Nom',
  36. 'email' => 'Email',
  37. 'subject' => 'Sujet',
  38. 'body' => 'Message',
  39. 'verifyCode' => 'Code de vérification',
  40. ];
  41. }
  42. /**
  43. * Sends an email to the specified email address using the information collected by this model.
  44. *
  45. * @param string $email the target email address
  46. * @return boolean whether the email was sent
  47. */
  48. public function sendEmail($email)
  49. {
  50. return Yii::$app->mailer->compose([
  51. 'html' => 'contact-html',
  52. 'text' => 'contact-text'],
  53. [ 'content' => $this->body,
  54. 'nom' => $this->name
  55. ])
  56. ->setTo($email)
  57. ->setFrom([$this->email => $this->name])
  58. ->setSubject('[La boîte à pain] Contact : '.$this->subject)
  59. ->send();
  60. }
  61. }