Skip to content

Commit bd9e593

Browse files
committed
增加动态sender配置
1 parent 6d18d37 commit bd9e593

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

src/MailBean.php

+38-22
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ class MailBean
5151
*/
5252
public $attachments;
5353

54+
/**
55+
* Array 当前邮件发送时的 sender 配置信息
56+
*/
57+
public $senderConfig;
58+
5459
public function __construct(Array $mailInfo = array())
5560
{
56-
if ( !empty($mailInfo) ) {
61+
if (!empty($mailInfo)) {
5762
$rf = new \ReflectionObject($this);
58-
foreach ( $mailInfo as $propName => $value ) {
63+
foreach ($mailInfo as $propName => $value) {
5964
$property = $rf->getProperty($propName);
6065
$property->setValue($this, $value);
6166
}
@@ -136,13 +141,13 @@ public function getCC()
136141

137142
public function setCC($cc)
138143
{
139-
if ( is_string($cc) ) {
144+
if (is_string($cc)) {
140145
$this->cc[$cc] = '';
141-
} else if ( is_array($cc) ) {
142-
foreach ( $cc as $key => $val ) {
143-
if ( is_numeric($key) ) {
146+
} else if (is_array($cc)) {
147+
foreach ($cc as $key => $val) {
148+
if (is_numeric($key)) {
144149
$this->cc[$val] = '';
145-
} else if ( is_string($key) ) {
150+
} else if (is_string($key)) {
146151
$this->cc[$key] = $val;
147152
}
148153
}
@@ -158,13 +163,13 @@ public function getBCC()
158163

159164
public function setBCC($bcc)
160165
{
161-
if ( is_string($bcc) ) {
166+
if (is_string($bcc)) {
162167
$this->bcc[$bcc] = '';
163-
} else if ( is_array($bcc) ) {
164-
foreach ( $bcc as $key => $val ) {
165-
if ( is_numeric($key) ) {
168+
} else if (is_array($bcc)) {
169+
foreach ($bcc as $key => $val) {
170+
if (is_numeric($key)) {
166171
$this->bcc[$val] = '';
167-
} else if ( is_string($key) ) {
172+
} else if (is_string($key)) {
168173
$this->bcc[$key] = $val;
169174
}
170175
}
@@ -180,13 +185,13 @@ public function getTo()
180185

181186
public function setTo($to)
182187
{
183-
if ( is_string($to) ) {
188+
if (is_string($to)) {
184189
$this->to[$to] = '';
185-
} else if ( is_array($to) ) {
186-
foreach ( $to as $key => $val ) {
187-
if ( is_numeric($key) ) {
190+
} else if (is_array($to)) {
191+
foreach ($to as $key => $val) {
192+
if (is_numeric($key)) {
188193
$this->to[$val] = '';
189-
} else if ( is_string($key) ) {
194+
} else if (is_string($key)) {
190195
$this->to[$key] = $val;
191196
}
192197
}
@@ -202,13 +207,13 @@ public function getReplyTo()
202207

203208
public function setReplyTo($replyTo)
204209
{
205-
if ( is_string($replyTo) ) {
210+
if (is_string($replyTo)) {
206211
$this->replyTo[$replyTo] = '';
207-
} else if ( is_array($replyTo) ) {
208-
foreach ( $replyTo as $key => $val ) {
209-
if ( is_numeric($key) ) {
212+
} else if (is_array($replyTo)) {
213+
foreach ($replyTo as $key => $val) {
214+
if (is_numeric($key)) {
210215
$this->replyTo[$val] = '';
211-
} else if ( is_string($key) ) {
216+
} else if (is_string($key)) {
212217
$this->replyTo[$key] = $val;
213218
}
214219
}
@@ -228,6 +233,17 @@ public function setAttachments($attachments)
228233
return $this;
229234
}
230235

236+
public function getSenderConfig()
237+
{
238+
return $this->senderConfig;
239+
}
240+
241+
public function setSenderConfig($config)
242+
{
243+
$this->senderConfig = $config;
244+
return $this;
245+
}
246+
231247
public function toString()
232248
{
233249
$result = array();

src/Sender/Sender.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ class Sender extends ISender
7373
public function __construct()
7474
{
7575
$this->class = Mailer::$config->sender['class'];
76+
}
7677

78+
public function initSenderConfig()
79+
{
7780
if ( isset(Mailer::$config->sender[$this->class]) ) {
7881
foreach ( Mailer::$config->sender[$this->class] as $config => $value ) {
7982
$this->{$config} = $value;
@@ -100,9 +103,16 @@ public function consume($debug = false)
100103

101104
$driver->consume(function($payload, $debug) {
102105
if ( $debug ) Mailer::log($payload);
103-
$payload = json_decode($payload, true);
104106

107+
$payload = json_decode($payload, true);
105108
$this->mailBean = new MailBean($payload);
109+
110+
$senderConfig = $this->mailBean->getSenderConfig();
111+
if (!empty($senderConfig)) {
112+
Mailer::$config->setSenderConfig($senderConfig);
113+
$this->initSenderConfig();
114+
}
115+
106116
return $this->doSend();
107117
}, $debug);
108118
}

tests/TestAsyncSend.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testASyncSend()
2929
$mailer = new Mailer();
3030

3131
$mailer::$config->setDriverConfig(array(
32-
'bootstrap.servers' => '172.17.0.6:9092',
32+
'bootstrap.servers' => '192.168.1.6:9092',
3333
'message.send.max.retries' => 3,
3434
'client.id' => 'TicketsMailKafka',
3535
'topic' => 'tickets-email',
@@ -42,6 +42,16 @@ public function testASyncSend()
4242
->setTo(array('komazhang@foxmail.com', 'zhangqiang@easemob.com'))
4343
->setReplyTo('501729495@qq.com');
4444

45+
$mailBean->setSenderConfig(array(
46+
'auth' => true,
47+
'username' => 'ticket.support@easemob.com',
48+
'password' => 'password',
49+
'host' => 'smtp.exmail.qq.com',
50+
'port' => 587,
51+
'secure' => 'tsl',
52+
'autoTSL' => true
53+
));
54+
4555
$ret = $mailer->send($mailBean, true);
4656

4757
var_dump($ret);

tests/testConsumer.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99
$mailer = new \PHPMailerShell\Mailer();
1010

1111
$mailer::$config->setDriverConfig(array(
12-
'bootstrap.servers' => '172.17.0.6:9092',
12+
'bootstrap.servers' => '192.168.1.6:9092',
1313
'group.id' => 'TicketMailConsumer',
1414
'topic' => ['tickets-email'], //注意对于消费者而言,topic是一个数组,
15-
'offset' => 'latest', //latest, beginning
15+
'offset' => 'beginning', //latest, beginning
1616
'timeout' => 12*1000
1717
));
1818

19-
$mailer::$config->setSenderConfig(array(
20-
'auth' => true,
21-
'username' => 'ticket.support@easemob.com',
22-
'password' => 'password',
23-
'host' => 'smtp.exmail.qq.com',
24-
'port' => 587,
25-
'secure' => 'tsl',
26-
'autoTSL' => true
27-
));
19+
20+
//这里可以不使用全局设置,那么在每个mailBean中必须填充sender配置项
21+
//$mailer::$config->setSenderConfig(array(
22+
// 'auth' => true,
23+
// 'username' => 'ticket.support@easemob.com',
24+
// 'password' => 'password',
25+
// 'host' => 'smtp.exmail.qq.com',
26+
// 'port' => 587,
27+
// 'secure' => 'tsl',
28+
// 'autoTSL' => true
29+
//));
2830

2931
try {
3032
$mailer->consume();

0 commit comments

Comments
 (0)