|
- <?php
-
-
-
- namespace backend\models;
-
- use Yii;
- use yii\base\Model;
-
-
- class MailForm extends Model
- {
-
- public $subject;
- public $message;
-
-
-
- public function rules()
- {
- return [
- [['subject', 'message'], 'required', 'message' => 'Champs obligatoire'],
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'subject' => 'Sujet',
- 'message' => 'Message',
- ];
- }
-
-
-
- public function sendEmail($usersArray)
- {
- $producer = Producer::getCurrent() ;
-
- $mj = new \Mailjet\Client(
- $producer->getApiKeyMailjet('public'),
- $producer->getApiKeyMailjet('private'),
- true,
- ['version' => 'v3.1']
- );
-
- $body = ['Messages' => []] ;
-
- foreach($usersArray as $user) {
- $body['Messages'][] = [
- 'From' => [
- 'Email' => $producer->slug.'@opendistrib.net',
- 'Name' => $producer->name
- ],
- 'To' => [
- [
- 'Email' => $user['email'],
- 'Name' => $user['name'].' '.$user['lastname']
- ]
- ],
- 'Subject' => $this->subject,
- 'TextPart' => $this->message,
- 'HTMLPart' => nl2br($this->message)
- ] ;
- }
-
- $response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
-
- return $response->success() ;
- }
-
- }
|