From 2f8edc73f5e205e7d20944cbdcd624d6417898a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Bo=CC=88hnke?= Date: Wed, 19 Jun 2019 23:08:50 +0200 Subject: [PATCH 1/2] fix some code stylings --- Mailer.php | 55 +++++++++++--------------- Message.php | 111 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 89 insertions(+), 77 deletions(-) diff --git a/Mailer.php b/Mailer.php index 00c0a04..e30e5ed 100644 --- a/Mailer.php +++ b/Mailer.php @@ -15,7 +15,6 @@ class Mailer extends BaseMailer { - private $_mailjet; private $_apikey; @@ -41,20 +40,19 @@ class Mailer extends BaseMailer 'unsub', ]; - /** - * @var string message default class name. - */ + /** + * @var string message default class name. + */ public $messageClass = 'weluse\mailjet\Message'; - /** - * readonly - * @var $_response Mailjet\Response - */ + /** + * readonly + * @var $_response Mailjet\Response + */ private $_response; public function init() { - if (!$this->_apikey) { throw new InvalidConfigException(sprintf('"%s::apikey" cannot be null.', get_class($this))); } @@ -77,19 +75,17 @@ public function init() * @param string $secret * @throws InvalidConfigException */ - public function setSecret($secret) - { - - if (!is_string($secret)) { - throw new InvalidConfigException(sprintf('"%s::secret" should be a string, "%s" given.', get_class($this), gettype($apikey))); - } - $trimmedSecret = trim($secret); - if (!strlen($trimmedSecret) > 0) { - throw new InvalidConfigException(sprintf('"%s::secret" length should be greater than 0.', get_class($this))); - } - $this->_secret = $trimmedSecret; - - } + public function setSecret($secret) + { + if (!is_string($secret)) { + throw new InvalidConfigException(sprintf('"%s::secret" should be a string, "%s" given.', get_class($this), gettype($apikey))); + } + $trimmedSecret = trim($secret); + if (!strlen($trimmedSecret) > 0) { + throw new InvalidConfigException(sprintf('"%s::secret" length should be greater than 0.', get_class($this))); + } + $this->_secret = $trimmedSecret; + } /** * Sets the API key for Mailjet @@ -114,7 +110,6 @@ public function setApikey($apikey) */ public function createMailjet() { - $mj = new \Mailjet\Client($this->_apikey, $this->_secret); $this->_mailjet = $mj; @@ -130,7 +125,6 @@ public function getResponse() */ protected function sendMessage($message) { - $to = $cc = $bcc = []; foreach ($message->to as $email => $name) { @@ -149,7 +143,6 @@ protected function sendMessage($message) $cc[] = $address; } - foreach ($message->bcc as $email => $name) { $address = '<' . $email . '>'; if (!empty($name)) { @@ -158,16 +151,15 @@ protected function sendMessage($message) $bcc[] = $address; } - $body = [ 'Subject' => $message->subject, 'Text-part' => $message->textBody, 'Html-part' => $message->htmlBody, - 'To' => join(', ',$to), + 'To' => join(', ', $to), ]; if ($cc) { - $body['Cc'] = join(', ',$cc); + $body['Cc'] = join(', ', $cc); } if ($bcc) { $body['Bcc'] = join(', ', $bcc); @@ -181,7 +173,7 @@ protected function sendMessage($message) } //Adds Reply-To to header - if(!empty($message->replyTo)) { + if (!empty($message->replyTo)) { $body['Headers']['Reply-to'] = $message->replyTo; } @@ -194,7 +186,6 @@ protected function sendMessage($message) public function setTracking($tracking) { - if (is_array($tracking)) { $urlValidator = new UrlValidator; @@ -212,7 +203,6 @@ public function setTracking($tracking) throw new InvalidConfigException(sprintf('the %s event is not supported', $event)); } } - } else { throw new InvalidConfigException('The trackingActions must be an array'); } @@ -270,7 +260,6 @@ public function clearTracking($event) $eventCallbackurl = Resources::$Eventcallbackurl; $eventCallbackurl[1] = $event; - $response = $this->_mailjet->delete($eventCallbackurl); + $this->_mailjet->delete($eventCallbackurl); } - } diff --git a/Message.php b/Message.php index 1bbeda4..f3c901c 100644 --- a/Message.php +++ b/Message.php @@ -11,7 +11,8 @@ * * @package weluse/mailjet */ -class Message extends BaseMessage { +class Message extends BaseMessage +{ private $_charset; @@ -38,29 +39,32 @@ class Message extends BaseMessage { /** * @inheritdoc */ - public function getCharset() { + public function getCharset() + { return $this->_charset; } /** * @inheritdoc */ - public function setCharset($charset) { + public function setCharset($charset) + { $this->_charset = $charset; } /** * @inheritdoc */ - public function getFrom() { + public function getFrom() + { return $this->_from; } /** * @inheritdoc */ - public function setFrom($from) { - + public function setFrom($from) + { if (is_array($from)) { $this->_from = [ 'FromEmail' => key($from), @@ -76,14 +80,16 @@ public function setFrom($from) { /** * @inheritdoc */ - public function getTo() { + public function getTo() + { return $this->_to; } /** * @inheritdoc */ - public function setTo($to) { + public function setTo($to) + { if (!is_array($to)) { $to = [$to => '']; } @@ -94,14 +100,16 @@ public function setTo($to) { /** * @inheritdoc */ - public function getReplyTo() { + public function getReplyTo() + { return $this->_replyTo; } /** * @inheritdoc */ - public function setReplyTo($replyTo) { + public function setReplyTo($replyTo) + { $this->_replyTo = $replyTo; return $this; } @@ -109,14 +117,16 @@ public function setReplyTo($replyTo) { /** * @inheritdoc */ - public function getCc() { + public function getCc() + { return $this->_cc; } /** * @inheritdoc */ - public function setCc($cc) { + public function setCc($cc) + { if (!is_array($cc)) { $cc = [$cc => '']; } @@ -127,15 +137,17 @@ public function setCc($cc) { /** * @inheritdoc */ - public function getBcc() { + public function getBcc() + { return $this->_bcc; } /** * @inheritdoc */ - public function setBcc($bcc) { - if (!is_array($bcc)){ + public function setBcc($bcc) + { + if (!is_array($bcc)) { $bcc = [$bcc => '']; } $this->_bcc = $bcc; @@ -145,14 +157,16 @@ public function setBcc($bcc) { /** * @inheritdoc */ - public function getSubject() { + public function getSubject() + { return $this->_subject; } /** * @inheritdoc */ - public function setSubject($subject) { + public function setSubject($subject) + { $this->_subject = $subject; return $this; } @@ -160,37 +174,42 @@ public function setSubject($subject) { /** * return the plain text for the mail */ - public function getTextBody() { - return $this->_textBody; - } + public function getTextBody() + { + return $this->_textBody; + } /** - * @inheritdoc - */ - public function setTextBody($text) { + * @inheritdoc + */ + public function setTextBody($text) + { $this->_textBody = $text; return $this; } /** - * return the html text for the mail - */ - public function getHtmlBody() { + * return the html text for the mail + */ + public function getHtmlBody() + { return $this->_htmlBody; } /** - * @inheritdoc - */ - public function setHtmlBody($html) { + * @inheritdoc + */ + public function setHtmlBody($html) + { $this->_htmlBody = $html; return $this; } /** - * @inheritdoc - */ - public function attach($fileName, array $options = []) { + * @inheritdoc + */ + public function attach($fileName, array $options = []) + { $attachment = [ 'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : \yii\helpers\FileHelper::getMimeType($fileName), 'Filename' => isset($options['fileName']) ? $options['fileName'] : basename($fileName), @@ -201,9 +220,10 @@ public function attach($fileName, array $options = []) { } /** - * @inheritdoc - */ - public function attachContent($content, array $options = []) { + * @inheritdoc + */ + public function attachContent($content, array $options = []) + { $attachment = [ 'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : 'text/plain', 'Filename' => isset($options['fileName']) ? $options['fileName'] : 'attachment.txt', @@ -214,9 +234,10 @@ public function attachContent($content, array $options = []) { } /** - * @inheritdoc - */ - public function embed($fileName, array $options = []) { + * @inheritdoc + */ + public function embed($fileName, array $options = []) + { $attachment = [ 'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : \yii\helpers\FileHelper::getMimeType($fileName), 'Filename' => isset($options['fileName']) ? $options['fileName'] : basename($fileName), @@ -227,9 +248,10 @@ public function embed($fileName, array $options = []) { } /** - * @inheritdoc - */ - public function embedContent($content, array $options = []) { + * @inheritdoc + */ + public function embedContent($content, array $options = []) + { $attachment = [ 'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : 'text/plain', 'Filename' => isset($options['fileName']) ? $options['fileName'] : 'attachment.txt', @@ -240,9 +262,10 @@ public function embedContent($content, array $options = []) { } /** - * @inheritdoc - */ - public function toString() { + * @inheritdoc + */ + public function toString() + { return implode(',', $this->getTo()) . "\n" . $this->getSubject() . "\n" . $this->getTextBody(); From 0180d48da78df8c8d6861b3e850f025f8469da81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Bo=CC=88hnke?= Date: Wed, 19 Jun 2019 23:17:28 +0200 Subject: [PATCH 2/2] update readme --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d6ea83..45117dd 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,17 @@ https://goo.gl/YNWTwd ``` composer require weluse/yii2-mailjet ``` + or add it to your composer.json in the require section + ``` "weluse/yii2-mailjet": "*", ``` ## Setup + add/replace this in your config under the components key. + ``` 'components' => [ 'mailer' => [ @@ -26,7 +30,6 @@ add/replace this in your config under the components key. ], ``` - ## Example ``` @@ -37,8 +40,29 @@ Yii::$app->mailer->compose('signup', ['user' => $user]) ->send(); ``` +## Attachment example + +``` +// Mail with attachment from string via Message::attachContent() +Yii::$app->mailer->compose('view-name') +->setSubject('Mail with attachment from content') +->attachContent("This is the attachment content", ['fileName' => 'attachment.txt', 'contentType' => 'text/plain']) +->setTo('info@example.com') +->send(); + +// Mail with attachment from file via Message::attach() +$filePath = ... // a file path here; +Yii::$app->mailer->compose('view-name') +->setSubject('Mail with attachment from content') +->attach($filePath) +->setTo('info@example.com') +->send(); +``` + ## Setup Event Tracking + Write the tracking item to the mailer config. + ``` 'components' => [ 'mailer' => [ @@ -51,7 +75,9 @@ Write the tracking item to the mailer config. ], ], ``` + To activate this url you must run this command at one time. + ``` Yii::$app->mailer->activateTracking(); ```