Skip to content

Commit 46c517a

Browse files
committed
Test successfully all send functionality
1 parent 40b7584 commit 46c517a

File tree

7 files changed

+450
-31
lines changed

7 files changed

+450
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ composer.phar
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
77
/composer.lock
8+
/set-local-test-env.sh

set-test-env.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
#######################################################
4+
#
5+
# TEST HOWTO
6+
#
7+
# Copy this file to e.g. set-local-test-env.sh and
8+
# set the various variables below. Then source the
9+
# file on you bash shell:
10+
#
11+
# ~/git/php-email$ . ./set-local-test-env.sh
12+
#
13+
# and start the PHP Unit tests:
14+
#
15+
# ~/git/php-email$ ./vendor/bin/phpunit tests
16+
#
17+
#######################################################
18+
19+
# Set this to 2 if your tests fail (will show SMTP debug)
20+
DEBUG_LEVEL=0
21+
22+
# Timezone for dates
23+
TIMEZONE="Europe\\/Berlin"
24+
25+
# The email address that all test emails shall be sent to
26+
TARGET_EMAIL=john.doe@example.com
27+
28+
# The default sender address
29+
SENDER_EMAIL=jane.doe@example.com
30+
31+
# The SMTP information
32+
SMTP_HOST=www.example.com
33+
SMTP_PORT=587
34+
SMTP_AUTH=true
35+
SMTP_USER=username
36+
SMTP_PASS=password
37+
SMTP_SECURE=tls
38+
39+
# The Debug Address (for test mail feature
40+
DEBUG_EMAIL=$TARGET_EMAIL
41+
42+
# The BCC email address (BCC mail mode)
43+
BCC_EMAIL=hans.mustermann@example.com
44+
45+
# The Reroute email address (REROUTE mail mode)
46+
REROUTE_EMAIL=$BCC_EMAIL
47+
48+
# The database information
49+
DB_HOST=localhost
50+
DB_PORT=3306
51+
DB_NAME=databasename
52+
DB_USER=username
53+
DB_PASS=password
54+
DB_PREFIX=phpunittest_
55+
56+
################ DO NOT EDIT FROM HERE ON #############
57+
BCC_CONFIG="{\"recipients\":\"${BCC_EMAIL}\"}"
58+
REROUTE_CONFIG="{\"recipients\":\"${REROUTE_EMAIL}\",\"subjectPrefix\":\"[Rerouted]\"}"
59+
60+
SMTP_CONFIG="{\"host\":\"${SMTP_HOST}\",\"port\":${SMTP_PORT},\"debugLevel\":${DEBUG_LEVEL},\"auth\":${SMTP_AUTH},\"credentials\":{\"user\":\"${SMTP_USER}\",\"pass\":\"${SMTP_PASS}\"},\"secureOption\":\"${SMTP_SECURE}\",\"charset\":\"utf8\"}"
61+
62+
EMAIL_TEST_SMTP="{\"timezone\":\"${TIMEZONE}\",\"mailMode\":\"default\",\"targetAddress\":\"${TARGET_EMAIL}\", \"smtpConfig\":${SMTP_CONFIG},\"rerouteConfig\":${REROUTE_CONFIG},\"bccConfig\":${BCC_CONFIG},\"debugAddress\":\"${DEBUG_EMAIL}\",\"defaultSender\":\"${SENDER_EMAIL}\",\"subjectPrefix\":\"[PHPUnitTest] \"}"
63+
EMAIL_DATABASE="{\"host\":\"${DB_HOST}\",\"port\":${DB_PORT},\"dname\":\"${DB_NAME}\",\"user\":\"${DB_USER}\",\"pass\":\"${DB_PASS}\",\"tablePrefix\":\"${DB_PREFIX}\"}"
64+
65+
export EMAIL_TEST_SMTP
66+
export EMAIL_DATABASE
67+
68+
69+
70+

src/TgEmail/Email.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Email {
1515
public const PROCESSING = 'processing';
1616
public const SENT = 'sent';
1717
public const FAILED = 'failed';
18+
public const HTML = 'html';
19+
public const TEXT = 'text';
1820

1921
public $uid;
2022
public $status;
@@ -44,7 +46,7 @@ public function getSender() {
4446
return $this->sender;
4547
}
4648

47-
public function setSender($email, $name) {
49+
public function setSender($email, $name = NULL) {
4850
$this->sender = EmailAddress::from($email, $name);
4951
return $this;
5052
}
@@ -55,10 +57,10 @@ public function getReplyTo() {
5557
$this->reply_to = EmailAddress::from($this->reply_to);
5658
}
5759
}
58-
return $this->sender;
60+
return $this->reply_to;
5961
}
6062

61-
public function setReplyTo($email, $name) {
63+
public function setReplyTo($email, $name = NULL) {
6264
$this->reply_to = EmailAddress::from($email, $name);
6365
return $this;
6466
}
@@ -88,7 +90,7 @@ protected function convertToAddresses($arr) {
8890
}
8991

9092
public function getTo() {
91-
return getRecipients()->to;
93+
return $this->getRecipients()->to;
9294
}
9395

9496
public function addTo($address, $name = NULL) {
@@ -97,17 +99,17 @@ public function addTo($address, $name = NULL) {
9799
$this->addTo($a);
98100
}
99101
} else if (is_string($address)) {
100-
getRecipients()->to[] = EmailAddress::from($address, $name);
102+
$this->getRecipients()->to[] = EmailAddress::from($address, $name);
101103
} else if (is_object($address)) {
102-
getRecipients()->to[] = EmailAddress::from($address);
104+
$this->getRecipients()->to[] = EmailAddress::from($address);
103105
} else {
104106
throw new EmailException('Cannot add TO recipient(s)');
105107
}
106108
return $this;
107109
}
108110

109111
public function getCc() {
110-
return getRecipients()->cc;
112+
return $this->getRecipients()->cc;
111113
}
112114

113115
public function addCc($address, $name = NULL) {
@@ -116,17 +118,17 @@ public function addCc($address, $name = NULL) {
116118
$this->addCc($a);
117119
}
118120
} else if (is_string($address)) {
119-
getRecipients()->cc[] = EmailAddress::from($address, $name);
121+
$this->getRecipients()->cc[] = EmailAddress::from($address, $name);
120122
} else if (is_object($address)) {
121-
getRecipients()->cc[] = EmailAddress::from($address);
123+
$this->getRecipients()->cc[] = EmailAddress::from($address);
122124
} else {
123125
throw new EmailException('Cannot add CC recipient(s)');
124126
}
125127
return $this;
126128
}
127129

128130
public function getBcc() {
129-
return getRecipients()->bcc;
131+
return $this->getRecipients()->bcc;
130132
}
131133

132134
public function addBcc($address, $name = NULL) {
@@ -135,9 +137,9 @@ public function addBcc($address, $name = NULL) {
135137
$this->addBcc($a);
136138
}
137139
} else if (is_string($address)) {
138-
getRecipients()->bcc[] = EmailAddress::from($address, $name);
140+
$this->getRecipients()->bcc[] = EmailAddress::from($address, $name);
139141
} else if (is_object($address)) {
140-
getRecipients()->bcc[] = EmailAddress::from($address);
142+
$this->getRecipients()->bcc[] = EmailAddress::from($address);
141143
} else {
142144
throw new EmailException('Cannot add BCC recipient(s)');
143145
}
@@ -156,7 +158,7 @@ public function setSubject($s) {
156158
public function getBody($type = 'text') {
157159
if (($this->body != NULL) && is_string($this->body)) {
158160
$this->body = json_decode($this->body);
159-
} else {
161+
} else if ($this->body == NULL) {
160162
$this->body = new \stdClass;
161163
}
162164
if (isset($this->body->$type)) {
@@ -168,7 +170,7 @@ public function getBody($type = 'text') {
168170
public function setBody($type = 'text', $body) {
169171
if (($this->body != NULL) && is_string($this->body)) {
170172
$this->body = json_decode($this->body);
171-
} else {
173+
} else if ($this->body == NULL) {
172174
$this->body = new \stdClass;
173175
}
174176
$this->body->$type = $body;
@@ -202,16 +204,16 @@ public function addAttachments(array $arr) {
202204
return $this;
203205
}
204206

205-
public function getSentTime() {
207+
public function getSentTime($timezone = 'UTC') {
206208
if (($this->sent_time != NULL) && is_string($this->sent_time)) {
207-
$this->sent_time = new Date($this->sent_time, 'Europe/Berlin');
209+
$this->sent_time = new Date($this->sent_time, $timezone);
208210
}
209211
return $this->sent_time;
210212
}
211213

212-
public function getQueuedTime() {
214+
public function getQueuedTime($timezone = 'UTC') {
213215
if (($this->queued_time != NULL) && is_string($this->queued_time)) {
214-
$this->queued_time = new Date($this->queued_time, 'Europe/Berlin');
216+
$this->queued_time = new Date($this->queued_time, $timezone);
215217
}
216218
return $this->queued_time;
217219
}

src/TgEmail/EmailQueue.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EmailQueue {
3131

3232
protected $mailDAO;
3333

34-
public function __construct(EmailConfig $config, EmailsDAO $mailDAO) {
34+
public function __construct($config, $mailDAO) {
3535
$this->config = $config;
3636
$this->mailDAO = $mailDAO;
3737
$this->mailer = NULL;
@@ -192,24 +192,29 @@ public function sendByUid($uid, $checkStatus = FALSE) {
192192
public function getReconfiguredEmail(Email $email) {
193193
$rc = new Email();
194194

195-
$rc->setSender($email->getSender());
195+
if ($email->getSender() != NULL) {
196+
$rc->setSender($email->getSender());
197+
} else {
198+
$rc->setSender($this->config->getDefaultSender());
199+
}
196200
$rc->setReplyTo($email->getReplyTo());
197201
$rc->addAttachments($email->getAttachments());
198202
$rc->setBody(Email::TEXT, $email->getBody(Email::TEXT));
199203
$rc->setBody(Email::HTML, $email->getBody(Email::HTML));
200204

201205
if ($this->config->getMailMode() == EmailQueue::REROUTE) {
202-
$rc->setSubject($this->config->getRerouteConfig()->getSubjectPrefix().' '.$email->getSubject().' - '.$email->stringify($email->getTo()));
206+
$rc->setSubject($this->config->getRerouteConfig()->getSubjectPrefix().$this->config->getSubjectPrefix().$email->getSubject().' - '.$email->stringify($email->getTo()));
203207
$rc->addTo($this->config->getRerouteConfig()->getRecipients());
204208
} else {
205-
$rc->setSubject($email->getSubject());
209+
$rc->setSubject($this->config->getSubjectPrefix().$email->getSubject());
206210
$rc->addTo($email->getTo());
207-
$rc->addCc($email->getCcc());
211+
$rc->addCc($email->getCc());
208212
$rc->addBcc($email->getBcc());
209213
if ($this->config->getMailMode() == EmailQueue::BCC) {
210214
$rc->addBcc($this->config->getBccConfig()->getRecipients());
211215
}
212216
}
217+
return $rc;
213218
}
214219

215220
public function send(Email $email) {
@@ -226,7 +231,7 @@ protected function _send(Email $email) {
226231
$phpMailer = $this->getMailer();
227232

228233
// Sender
229-
$phpMailer->setFrom($email->getSender()->email, $email-getSender()->name);
234+
$phpMailer->setFrom($email->getSender()->email, $email->getSender()->name);
230235

231236
// Reply-To
232237
if ($email->getReplyTo() != NULL) {
@@ -259,22 +264,22 @@ protected function _send(Email $email) {
259264
}
260265

261266
// Attachments
262-
foreach ($email->getAttachments as $a) {
267+
foreach ($email->getAttachments() as $a) {
263268
if ($a->type == Attachment::ATTACHED) {
264269
$phpMailer->AddAttachment($a->path, $a->name, 'base64', $a->mimeType);
265270
} else if ($a->type == 'embedded') {
266271
$phpMailer->AddEmbeddedImage($a->path, $a->cid, $a->name);
267272
}
268273
}
269274

270-
$rc = FALSE;
275+
$rc = TRUE;
271276
if ($this->config->getMailMode() != EmailQueue::BLOCK) {
272277
$rc = $phpMailer->send();
273278
Log::debug('Mail sent: '.$email->getLogString());
274279
if (!$rc) {
275280
Log::error("Mailer Error: " . $phpMailer->ErrorInfo);
276281
} else {
277-
foreach ($email->getAttachments as $a) {
282+
foreach ($email->getAttachments() as $a) {
278283
if ($a->deleteAfterSent) {
279284
unlink($a->path);
280285
}
@@ -304,7 +309,7 @@ protected function _queue($email) {
304309
$email->failed_attempts = 0;
305310
$email->sent_time = NULL;
306311
$rc = $this->mailDAO->create($email);
307-
return $rc;
312+
return is_int($rc);
308313
}
309314
throw new EmailException('Queueing is not supported. No DAO available.');
310315
}

tests/TgEmail/EmailConfigTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testSetBccConfig(): void {
5656
$this->assertEquals($bccConfig, $config->getBccConfig());
5757
}
5858

59-
public function testAddAddDebugAddressWithString(): void {
59+
public function testAddDebugAddressWithString(): void {
6060
$config = new EmailConfig();
6161
$config->addDebugAddress('John Doe <john.doe@example.com');
6262

@@ -65,7 +65,7 @@ public function testAddAddDebugAddressWithString(): void {
6565
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
6666
}
6767

68-
public function testAddAddDebugAddressWithObject(): void {
68+
public function testAddDebugAddressWithObject(): void {
6969
$config = new EmailConfig();
7070
$config->addDebugAddress(EmailAddress::from('John Doe <john.doe@example.com'));
7171

@@ -74,7 +74,7 @@ public function testAddAddDebugAddressWithObject(): void {
7474
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
7575
}
7676

77-
public function testAddAddDebugAddressWithArray(): void {
77+
public function testAddDebugAddressWithArray(): void {
7878
$config = new EmailConfig();
7979
$config->addDebugAddress(array(
8080
EmailAddress::from('John Doe <john.doe@example.com'),

0 commit comments

Comments
 (0)