-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleMail.class.php
More file actions
153 lines (141 loc) · 3.91 KB
/
Copy pathsimpleMail.class.php
File metadata and controls
153 lines (141 loc) · 3.91 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* This file contains a single class for sending emails with attachemnts.
*
* PHP Version 7.1
*
* @category SimpleMail
* @package SimpleMail
* @author Evan Wills <evan.i.wills@gmail.com>
* @license GPL2 <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
* @link https://github.com/evanwills/php-utilsplif/
*/
/**
* Simple email class that can also attach files.
*
* @category SimpleMail
* @package SimpleMail
* @author Evan Wills <evan.i.wills@gmail.com>
* @license GPL2 <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>
* @link https://github.com/evanwills/php-utils
*/
class SimpleMail
{
/**
* Email address of recipient
*
* @var string
*/
private $_toAddress = '';
/**
* Subject line for email
*
* @var string
*/
private $_subject = '';
/**
* Email headers
*
* @var string
*/
private $_headers = '';
/**
* Email body
*
* @var string
*/
private $_message = '';
/**
* String that divides parts of the email
*
* @var string
*/
private $_mimeBoundary = '';
/**
* Sets up variables and mail email
*
* @param string $toAddress Email recipient
* @param string $subject Email subject
* @param string $message Email message text
* @param string $headers Extra headers
*
* @return void
*/
function __construct(
string $toAddress, string $subject,
string $message, string $headers
) {
$this->_toAddress = $toAddress;
$this->_subject = $subject;
$this->_headers = $headers;
$semi_rand = md5(time());
$this->_mimeBoundary = "==Multipart_Boundary_x{$semi_rand}x";
$this->_headers .=
"\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$this->_mimeBoundary}\"";
$this->_message .=
"This is a multi-part message in MIME format.\n\n" .
"--{$this->_mimeBoundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 8bit\n\n" .
$message .
"\n\n";
}
/**
* Add attachment to email
*
* @param string $fileatt_type Attachemnt content type
* @param string $fileatt_name Attachment file name
* @param mixed $fileatt_content Contents of attachment
*
* @return void
*/
function attach(string $fileatt_type, string $fileatt_name, $fileatt_content)
{
$data = chunk_split(base64_encode($fileatt_content));
$this->_message .= "\n--{$this->_mimeBoundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
trim($data)."\n" .
"--{$this->_mimeBoundary}";
// unset($file);
unset($data, $fileatt, $fileatt_type, $fileatt_name);
}
/**
* Send email
*
* @return void
*/
function send() : bool
{
$this->_message .= "--\n";
//echo('<pre>' . $this->_message . '</pre>');
return mail(
$this->_toAddress,
$this->_subject,
$this->_message,
$this->_headers
);
}
/**
* Read file and add as attachment
*
* @param string $file File system path to attachment file
* @param string $name Alternate name for file
*
* @return void
*/
function file($file, $name = "")
{
$o = fopen($file, "rb");
$content = fread($o, filesize($file));
fclose($o);
$_name = $name == ""
? basename($file)
: $name;
$type = "application/octet-stream";
$this->attach($type, $_name, $content);
}
}