|
- <?php
-
-
-
- namespace common\helpers;
-
- use yii\helpers\Html;
-
- class Mailjet
- {
- public static function sendMail($params = [])
- {
- $mj = new \Mailjet\Client(
- self::getApiKey('public'),
- self::getApiKey('private'),
- true,
- ['version' => 'v3.1']
- );
-
- $body = ['Messages' => []] ;
-
- $body['Messages'][] = [
- 'From' => [
- 'Email' => $params['from_email'],
- 'Name' => $params['from_name']
- ],
- 'To' => [
- [
- 'Email' => $params['to_email'],
- 'Name' => $params['to_name']
- ]
- ],
- 'Subject' => $params['subject'],
- 'TextPart' => Yii::$app->view->renderFile($params['content_view_text'], $params['content_params']),
- 'HTMLPart' => Yii::$app->view->renderFile($params['content_view_html'], $params['content_params'])
- ] ;
-
- $response = $mj->post(\Mailjet\Resources::$Email, ['body' => $body]);
-
- $success = $response->success() ;
-
- if(!$success) {
- Yii::error($response->getBody(), 'Mailjet');
- }
-
- return $success ;
- }
-
- public static function getApiKey($type = 'private')
- {
- $filename = '../../common/config/mailjet/api.key';
- if (file_exists($filename)) {
- $handle = fopen($filename, "r");
- $filesize = filesize($filename);
- if ($handle && $filesize) {
- $apiKeys = fread($handle, $filesize);
- fclose($handle);
- $apiKeysArray = explode(':', $apiKeys);
- if (count($apiKeysArray) == 2) {
- if ($type == 'private') {
- $key = $apiKeysArray[1];
- } else {
- $key = $apiKeysArray[0];
- }
- return trim($key);
- }
- }
- }
-
- return '';
- }
- }
|