forked from yamaha252/yii2-mailgun-mailer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mailer.php
122 lines (106 loc) · 3.13 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace e96\mailgunmailer;
use Mailgun\Connection\Exceptions\MissingRequiredParameters;
use Yii;
use yii\mail\BaseMailer;
use Mailgun\Mailgun;
/**
* Mailer implements a mailer based on Mailgun.
*
* To use Mailer, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => [
* ...
* 'mailer' => [
* 'class' => 'e96\mailgunmailer\Mailer',
* 'domain' => 'example.com',
* 'key' => 'key-somekey',
* 'tags' => ['yii'],
* 'clickTracking' => false,
* 'opensTracking' => false
* ],
* ...
* ],
* ~~~
*/
class Mailer extends BaseMailer
{
/**
* [$messageClass description]
* @var string message default class name.
*/
public $messageClass = 'e96\mailgunmailer\Message';
public $domain;
public $key;
public $from;
public $tags = [];
public $campaignId;
public $dkim;
public $testMode;
public $clickTracking;
public $opensTracking;
private $_mailgunMailer;
/**
* @return Mailgun Mailgun mailer instance.
*/
public function getMailgunMailer()
{
if (!is_object($this->_mailgunMailer)) {
$this->_mailgunMailer = $this->createMailgunMailer();
}
return $this->_mailgunMailer;
}
/**
* @param \e96\mailgunmailer\Message $message
* @return bool
* @throws MissingRequiredParameters
*/
protected function sendMessage($message)
{
if (!$this->domain) {
throw new MissingRequiredParameters('Domain property is required');
}
if ($this->from && $message->from === null) {
$message->from = $this->from;
}
if ($this->tags && $message->tags === null) {
$message->tags = $this->tags;
}
if ($this->campaignId !== null && $message->campaignId === null) {
$message->campaignId = $this->campaignId;
}
if ($this->dkim !== null && $message->dkim === null) {
$message->dkim = $this->dkim;
}
if ($this->testMode !== null && $message->testMode === null) {
$message->testMode = $this->testMode;
}
if ($this->clickTracking !== null && $message->clickTracking === null) {
$message->clickTracking = $this->clickTracking;
}
if ($this->opensTracking !== null && $message->opensTracking === null) {
$message->opensTracking = $this->opensTracking;
}
Yii::info('Sending email', __METHOD__);
$response = $this->getMailgunMailer()->post(
"{$this->domain}/messages",
$message->getMessage(),
$message->getFiles()
);
Yii::info('Response : '.print_r($response, true), __METHOD__);
return true;
}
/**
* Creates Mailgun mailer instance.
* @return Mailgun mailer instance.
* @throws MissingRequiredParameters
*/
protected function createMailgunMailer()
{
if (!$this->key) {
throw new MissingRequiredParameters('API key property is required');
}
return new Mailgun($this->key);
}
}