Skip to content

Commit 529230f

Browse files
committed
version 5.0.0
1 parent 377bef2 commit 529230f

29 files changed

+1832
-942
lines changed

README.md

Lines changed: 189 additions & 138 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.3",
15+
"php": ">=5.3",
1616
"ext-mbstring": "*"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^4.8.35 || ^5.7",
20-
"fzaninotto/faker": "~1.8",
2120
"squizlabs/php_codesniffer": "^3.5"
2221
},
2322
"suggest": {
@@ -26,10 +25,14 @@
2625
"autoload": {
2726
"psr-4": {
2827
"Ddrv\\Mailer\\": "src/"
29-
}
28+
},
29+
"files": [
30+
"fn.php"
31+
]
3032
},
3133
"autoload-dev": {
3234
"psr-4": {
35+
"Stuff\\Ddrv\\Mailer\\": "stuff/",
3336
"Tests\\Ddrv\\Mailer\\": "tests/"
3437
}
3538
},

fn.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
if (!function_exists('random_int')) {
4+
5+
/**
6+
* @param int $min
7+
* @param int $max
8+
* @return int
9+
*/
10+
function random_int($min, $max)
11+
{
12+
return rand((int)$min, (int)$max);
13+
}
14+
}
15+
16+
if (!function_exists('mime_content_type')) {
17+
18+
/**
19+
* @param string $filename
20+
* @return string
21+
*/
22+
function mime_content_type($filename)
23+
{
24+
if (function_exists('finfo_open')) {
25+
/** @noinspection PhpComposerExtensionStubsInspection */
26+
$info = finfo_open(FILEINFO_MIME);
27+
/** @noinspection PhpComposerExtensionStubsInspection */
28+
$mime = finfo_file($info, $filename);
29+
/** @noinspection PhpComposerExtensionStubsInspection */
30+
finfo_close($info);
31+
return $mime;
32+
}
33+
$types = array(
34+
'txt' => 'text/plain',
35+
'htm' => 'text/html',
36+
'html' => 'text/html',
37+
'php' => 'text/html',
38+
'css' => 'text/css',
39+
'js' => 'application/javascript',
40+
'json' => 'application/json',
41+
'xml' => 'application/xml',
42+
'swf' => 'application/x-shockwave-flash',
43+
'flv' => 'video/x-flv',
44+
45+
// images
46+
'png' => 'image/png',
47+
'jpe' => 'image/jpeg',
48+
'jpeg' => 'image/jpeg',
49+
'jpg' => 'image/jpeg',
50+
'gif' => 'image/gif',
51+
'bmp' => 'image/bmp',
52+
'ico' => 'image/vnd.microsoft.icon',
53+
'tiff' => 'image/tiff',
54+
'tif' => 'image/tiff',
55+
'svg' => 'image/svg+xml',
56+
'svgz' => 'image/svg+xml',
57+
58+
// archives
59+
'zip' => 'application/zip',
60+
'rar' => 'application/x-rar-compressed',
61+
'exe' => 'application/x-msdownload',
62+
'msi' => 'application/x-msdownload',
63+
'cab' => 'application/vnd.ms-cab-compressed',
64+
65+
// audio/video
66+
'mp3' => 'audio/mpeg',
67+
'qt' => 'video/quicktime',
68+
'mov' => 'video/quicktime',
69+
70+
// adobe
71+
'pdf' => 'application/pdf',
72+
'psd' => 'image/vnd.adobe.photoshop',
73+
'ai' => 'application/postscript',
74+
'eps' => 'application/postscript',
75+
'ps' => 'application/postscript',
76+
77+
// ms office
78+
'doc' => 'application/msword',
79+
'rtf' => 'application/rtf',
80+
'xls' => 'application/vnd.ms-excel',
81+
'ppt' => 'application/vnd.ms-powerpoint',
82+
83+
// open office
84+
'odt' => 'application/vnd.oasis.opendocument.text',
85+
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
86+
);
87+
$arr = explode('.', $filename);
88+
$ext = strtolower(array_pop($arr));
89+
if (array_key_exists($ext, $types)) {
90+
return $types[$ext];
91+
}
92+
return 'application/octet-stream';
93+
}
94+
}
95+
96+
/**
97+
* @param string $fileOrContents
98+
* @return string
99+
*/
100+
function ddrv_mailer_define_mime_type($fileOrContents)
101+
{
102+
if (file_exists($fileOrContents)) {
103+
return mime_content_type($fileOrContents);
104+
}
105+
if (function_exists('finfo_open')) {
106+
/** @noinspection PhpComposerExtensionStubsInspection */
107+
$info = finfo_open(FILEINFO_MIME);
108+
/** @noinspection PhpComposerExtensionStubsInspection */
109+
$mime = finfo_buffer($info, $fileOrContents);
110+
/** @noinspection PhpComposerExtensionStubsInspection */
111+
finfo_close($info);
112+
return $mime;
113+
}
114+
return 'application/octet-stream';
115+
}
116+
117+
function ddrv_mailer_encode_mime_header($string, $offset = 0)
118+
{
119+
$max = 74;
120+
$map = array(
121+
'=00', '=01', '=02', '=03', '=04', '=05', '=06', '=07', '=08', '=09', '=0A', '=0B', '=0C', '=0D', '=0E', '=0F',
122+
'=10', '=11', '=12', '=13', '=14', '=15', '=16', '=17', '=18', '=19', '=1A', '=1B', '=1C', '=1D', '=1E', '=1F',
123+
'=20', '=21', '=22', '=23', '=24', '=25', '=26', '=27', '=28', '=29', '=2A', '=2B', '=2C', '=2D', '=2E', '=2F',
124+
'=30', '=31', '=32', '=33', '=34', '=35', '=36', '=37', '=38', '=39', '=3A', '=3B', '=3C', '=3D', '=3E', '=3F',
125+
'=40', '=41', '=42', '=43', '=44', '=45', '=46', '=47', '=48', '=49', '=4A', '=4B', '=4C', '=4D', '=4E', '=4F',
126+
'=50', '=51', '=52', '=53', '=54', '=55', '=56', '=57', '=58', '=59', '=5A', '=5B', '=5C', '=5D', '=5E', '=5F',
127+
'=60', '=61', '=62', '=63', '=64', '=65', '=66', '=67', '=68', '=69', '=6A', '=6B', '=6C', '=6D', '=6E', '=6F',
128+
'=70', '=71', '=72', '=73', '=74', '=75', '=76', '=77', '=78', '=79', '=7A', '=7B', '=7C', '=7D', '=7E', '=7F',
129+
'=80', '=81', '=82', '=83', '=84', '=85', '=86', '=87', '=88', '=89', '=8A', '=8B', '=8C', '=8D', '=8E', '=8F',
130+
'=90', '=91', '=92', '=93', '=94', '=95', '=96', '=97', '=98', '=99', '=9A', '=9B', '=9C', '=9D', '=9E', '=9F',
131+
'=A0', '=A1', '=A2', '=A3', '=A4', '=A5', '=A6', '=A7', '=A8', '=A9', '=AA', '=AB', '=AC', '=AD', '=AE', '=AF',
132+
'=B0', '=B1', '=B2', '=B3', '=B4', '=B5', '=B6', '=B7', '=B8', '=B9', '=BA', '=BB', '=BC', '=BD', '=BE', '=BF',
133+
'=C0', '=C1', '=C2', '=C3', '=C4', '=C5', '=C6', '=C7', '=C8', '=C9', '=CA', '=CB', '=CC', '=CD', '=CE', '=CF',
134+
'=D0', '=D1', '=D2', '=D3', '=D4', '=D5', '=D6', '=D7', '=D8', '=D9', '=DA', '=DB', '=DC', '=DD', '=DE', '=DF',
135+
'=E0', '=E1', '=E2', '=E3', '=E4', '=E5', '=E6', '=E7', '=E8', '=E9', '=EA', '=EB', '=EC', '=ED', '=EE', '=EF',
136+
'=F0', '=F1', '=F2', '=F3', '=F4', '=F5', '=F6', '=F7', '=F8', '=F9', '=FA', '=FB', '=FC', '=FD', '=FE', '=FF',
137+
);
138+
$symbols = str_split($string);
139+
unset($string);
140+
$result = '';
141+
$coding = false;
142+
$all = count($symbols);
143+
$position = 0;
144+
foreach ($symbols as $num => $symbol) {
145+
$line = '';
146+
$add = 0;
147+
$char = ord($symbol);
148+
$ascii = ($char >= 32 && $char <= 60) || ($char >= 62 && $char <= 126);
149+
if ($char === 32 && $num + 1 === $all) {
150+
$ascii = false;
151+
}
152+
if ($num < $offset) {
153+
$ascii = true;
154+
$coding = false;
155+
}
156+
if (!$coding && $char === 61 && preg_match('/;(\s+)?([a-z0-9\-]+)(\s+)?(=(\s+)?\"[^\"]+)?/ui', $result)) {
157+
$ascii = true;
158+
}
159+
if ($ascii) {
160+
if ($coding) {
161+
$coding = false;
162+
$line = '?=' . $symbol;
163+
$add = 3;
164+
} else {
165+
$line = $symbol;
166+
$add = 1;
167+
}
168+
} else {
169+
if (!$coding) {
170+
$coding = true;
171+
$line = '=?utf-8?Q?';
172+
$add = 10;
173+
}
174+
$line .= $map[$char];
175+
$add += 3;
176+
}
177+
if ($position + $add >= $max) {
178+
$line = "=\r\n $line";
179+
$position = $add + 1;
180+
}
181+
$result .= $line;
182+
$position += $add;
183+
}
184+
if ($coding) {
185+
$line = '?=';
186+
if ($position + 3 >= $max) {
187+
$line = "=\r\n $line";
188+
}
189+
$result .= $line;
190+
}
191+
return $result;
192+
}

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414

1515
<!-- Paths to check -->
1616
<file>src</file>
17+
<file>stuff</file>
1718
<file>tests</file>
1819
</ruleset>

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit bootstrap="vendor/autoload.php" colors="true">
33
<testsuites>
44
<testsuite name="all">
5-
<directory>tests/Unit</directory>
5+
<directory>tests</directory>
66
</testsuite>
77
</testsuites>
88
</phpunit>

src/Contract/Message.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Contract;
4+
5+
use Ddrv\Mailer\Exception\InvalidEmailException;
6+
use Serializable;
7+
8+
interface Message extends Serializable
9+
{
10+
11+
const RECIPIENT_TO = 'to';
12+
const RECIPIENT_CC = 'cc';
13+
const RECIPIENT_BCC = 'bcc';
14+
15+
/**
16+
* @param string $email Recipient email.
17+
* @param string|null $name Recipient name.
18+
* @param string $type Recipient type. May be 'to', 'cc' or 'bcc'. Default 'to'.
19+
* @return self
20+
* @throws InvalidEmailException
21+
*/
22+
public function addRecipient($email, $name = null, $type = self::RECIPIENT_TO);
23+
24+
/**
25+
* @param string $email Recipient email.
26+
* @return string|null Recipient name or null.
27+
*/
28+
public function getRecipientName($email);
29+
30+
/**
31+
* @param string $type Recipient type. May be 'to', 'cc', 'bcc' or null. Default null.
32+
* @return self
33+
*/
34+
public function removeRecipients($type = null);
35+
36+
/**
37+
* @param string $email Sender email.
38+
* @param string|null $name Sender name.
39+
* @return self
40+
*/
41+
public function setSender($email, $name = null);
42+
43+
/**
44+
* @return string[] Recipients emails.
45+
*/
46+
public function getRecipients();
47+
48+
/**
49+
* @return string|null Mail subject.
50+
*/
51+
public function getSubject();
52+
53+
/**
54+
* @return string Rew string as email headers
55+
*/
56+
public function getHeadersRaw();
57+
58+
/**
59+
* @return string Raw string of email body
60+
*/
61+
public function getBodyRaw();
62+
}

src/Contract/Spool.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Contract;
4+
5+
interface Spool
6+
{
7+
8+
/**
9+
* @param Message $message
10+
* @param int $attempt
11+
*/
12+
public function push(Message $message, $attempt);
13+
14+
/**
15+
* @param int $attempt
16+
* @return Message|null
17+
*/
18+
public function pull($attempt);
19+
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
<?php
22

3-
namespace Ddrv\Mailer;
3+
namespace Ddrv\Mailer\Contract;
44

5-
use Closure;
65
use Ddrv\Mailer\Exception\RecipientsListEmptyException;
6+
use Ddrv\Mailer\Exception\TransportException;
77

8-
interface TransportInterface
8+
interface Transport
99
{
1010

1111
/**
12-
* Send mail
13-
*
1412
* @param Message $message
1513
* @return bool
1614
* @throws RecipientsListEmptyException
15+
* @throws TransportException
1716
*/
1817
public function send(Message $message);
19-
20-
/**
21-
* @param Closure $logger
22-
* @return void
23-
*/
24-
public function setLogger(Closure $logger);
2518
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
final class HeaderNotModifiedException extends InvalidArgumentException
8+
{
9+
10+
public function __construct($header, $method = null)
11+
{
12+
$message = 'header "' . $header . '" is not modified.';
13+
$code = 2;
14+
if ($method) {
15+
$message .= ' use ' . $method . ' method for it.';
16+
$code = 1;
17+
}
18+
parent::__construct($message, $code);
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
final class InvalidAttachmentNameException extends InvalidArgumentException
8+
{
9+
10+
public function __construct($name)
11+
{
12+
parent::__construct('invalid attachment ' . $name, 1);
13+
}
14+
}

0 commit comments

Comments
 (0)