Skip to content

Commit 2b17cae

Browse files
committed
push repository
0 parents  commit 2b17cae

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Ivan Dudarev, https://github.com/ddrv <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Mailer
2+
PHP Class for sending email.
3+
4+
## For example
5+
6+
```php
7+
8+
$params = [
9+
'sender' => '[email protected]',
10+
'charset' => 'utf8',
11+
];
12+
13+
$mailer = new \Ddrv\Mailer\Mailer($params);
14+
15+
// Simple text
16+
$mailer->send('[email protected]','Test Simple Text', 'This is simple text');
17+
18+
// Attachments
19+
$attachments = array(
20+
// Attachment from string
21+
array(
22+
'name' => 'attach_from_string.txt',
23+
'type' => 'string',
24+
'content' => 'Content of attach 1',
25+
),
26+
// Attachment from file
27+
array(
28+
'name' => 'attach_from_file.txt',
29+
'type' => 'file',
30+
'content' => '/path/to/file',
31+
)
32+
);
33+
$mailer->send('[email protected]','Test Attachments', '<i>This is Body</i>', $attachments);
34+
```

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "ddrv/mailer",
3+
"version":"1.0.0",
4+
"require":{
5+
"php":">=5.3.0"
6+
},
7+
"type": "library",
8+
"description": "PHP Class for sending email",
9+
"keywords": ["email", "mail", "send", "attachments"],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Ivan Dudarev",
14+
"email": "[email protected]",
15+
"homepage": "https://ddrv.ru"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"Ddrv\\Mailer\\": "src/"
21+
}
22+
}
23+
}

src/Mailer.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
namespace Ddrv\Mailer;
3+
/**
4+
* PHP Class for sending email.
5+
*
6+
* PHP version 5.3
7+
*
8+
* @category Ddrv
9+
* @package Mailer
10+
* @author Ivan Dudarev (https://github.com/ddrv)
11+
* @license MIT
12+
* @link http://ddrv.ru/
13+
*
14+
* @property string $sender
15+
* @property string $charset
16+
* @property array $headers
17+
*/
18+
19+
class Mailer {
20+
const MAILER_VERSION = '1.0.0';
21+
22+
/**
23+
* send from this Email
24+
*
25+
* @var string
26+
*/
27+
public $sender;
28+
29+
/**
30+
* headers of mail
31+
*
32+
* @var array
33+
*/
34+
public $headers;
35+
36+
/**
37+
* charset of message
38+
*
39+
* @var array
40+
*/
41+
public $charset = 'utf8';
42+
43+
/**
44+
* Consructor
45+
* @param array $sets
46+
*/
47+
public function __construct($sets=array()) {
48+
if ($sets) {
49+
foreach ($sets as $property => $value) {
50+
if (in_array($property, array('sender','headers')) ) {
51+
$this->$property = $value;
52+
}
53+
}
54+
$this->init();
55+
}
56+
}
57+
58+
/**
59+
* Initialization this component and first record
60+
* This method requred for Yii Framevork
61+
*/
62+
public function init () {
63+
if (empty($this->charset)) $this->charset = 'utf8';
64+
if (empty($this->sender)) $this->sender = 'sender@localhost';
65+
}
66+
67+
/**
68+
* Send email to address
69+
*
70+
* @param string $address
71+
* @param string $subject
72+
* @param string $message
73+
* @param array $attachments
74+
*/
75+
public function send($address,$subject,$message,$attachments=array()) {
76+
$rawAttachments = empty($attachments)?array():$this->getRawAttachment($attachments);
77+
$noModifiedHeaders = $this->headers;
78+
$this->setHeader('From', $this->sender, false);
79+
$this->setHeader('Reply-To', $this->sender, false);
80+
$this->setHeader('MIME-Version','1.0', false);
81+
$this->setHeader('X-Mailer', 'ddrvMailer-'.self::MAILER_VERSION.' (https://github.com/ddrv/mailer)', false);
82+
$body = empty($rawAttachments)?$this->getBodySimpleText($message):$this->getBodyMultipart($message, $rawAttachments);
83+
$headers = implode("\r\n",$this->headers);
84+
mail($address, $subject, $body, $headers);
85+
$this->headers = $noModifiedHeaders;
86+
}
87+
88+
protected function getRawAttachment($attachments=array()) {
89+
$rawAttachments = array();
90+
foreach ((array)$attachments as $attachment) {
91+
if (
92+
!empty($attachment['content'])
93+
&& !empty($attachment['name'])
94+
&& !empty($attachment['type'])
95+
&& in_array($attachment['type'], array('file','string'))
96+
) {
97+
$name = preg_replace('/[\\\\\/\:\*\?\"<>]/ui', '_', $attachment['name']);
98+
$content = null;
99+
switch ($attachment['type']) {
100+
case 'file':
101+
$content = (file_exists($attachment['content']))?chunk_split(base64_encode(file_get_contents($attachment['content']))):null;
102+
break;
103+
case 'string':
104+
$content = chunk_split(base64_encode($attachment['content']));
105+
break;
106+
}
107+
if ($name && $content) {
108+
$rawAttachments[$name] = $content;
109+
}
110+
}
111+
}
112+
return $rawAttachments;
113+
}
114+
115+
protected function getBodySimpleText($message) {
116+
$this->setHeader('Content-type','text/html; charset='.$this->charset, true);
117+
return $message;
118+
}
119+
120+
protected function getBodyMultipart($message, $attachments) {
121+
$separator = md5(time());
122+
$eol = "\r\n";
123+
$this->setHeader('Content-Type', 'multipart/mixed; boundary="'.$separator.'"', true);
124+
$this->setHeader('Content-Transfer-Encoding', '7bit', true);
125+
126+
$body[] = null;
127+
$b = $eol.'Content-type: text/html; charset='.$this->charset.$eol;
128+
$bits = '7bit';
129+
switch ($this->charset) {
130+
case 'utf-8':
131+
case 'utf8': $bits='8bit'; break;
132+
}
133+
$b .= 'Content-Transfer-Encoding: '.$bits.$eol;
134+
$b .= $eol.$message.$eol;
135+
$body[] = $b;
136+
foreach ($attachments as $file=>$content) {
137+
$b = $eol.'Content-Type: application/octet-stream; name="' . $file . '"' . $eol;
138+
$b .= 'Content-Transfer-Encoding: base64' . $eol;
139+
$b .= 'Content-Disposition: attachment' . $eol;
140+
$b .= $eol.$content.$eol;
141+
$body[] = $b;
142+
}
143+
$body[] = '--';
144+
return implode('--'.$separator,$body);
145+
}
146+
147+
public function setHeaderFrom($address) {
148+
$this->setHeader('From',$address);
149+
}
150+
151+
protected function setHeader($header, $value=null, $replace=true) {
152+
if (!$replace && !empty($this->headers[mb_strtolower($header)])) return;
153+
if (($value === null || $value === false) && !empty($this->headers[mb_strtolower($header)])) {
154+
unset($this->headers[mb_strtolower($header)]);
155+
} else {
156+
$this->headers[mb_strtolower($header)] = $header . ': ' . $value;
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)