-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleMailer.class.php
More file actions
164 lines (131 loc) · 4.69 KB
/
SimpleMailer.class.php
File metadata and controls
164 lines (131 loc) · 4.69 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
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace SimpleMailer;
class SimpleMailer
{
private Array $from, $to;
private String $subject;
private Array $replyto;
private Array $template;
private \SimpleTpl $stpl;
private $attachments = array();
public function __construct()
{
$this->stpl = new \SimpleTpl();
}
private function validateEmail($email):bool
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
public function from($email, $fullname):bool
{
if ($this->validateEmail($email))
{
$this->from["email"] = $email;
$this->from["fullname"] = $fullname;
return true;
} else {
return false;
}
}
public function replyto(String $email, String $fullname):bool
{
if ($this->validateEmail($email))
{
$this->replyto = [
"email" => $email,
"fullname" => $fullname];
return true;
}
return false;
}
public function to(String $email, String $fullname):bool
{
if ($this->validateEmail($email))
{
$this->to[] = [
"email" => $email,
"fullname" => $fullname];
return true;
}
return false;
}
public function subject(String $subject):void
{
$this->subject = $subject;
}
public function template_plain(String $filename):void
{
if (file_exists($filename))
$this->template["plain"] = $filename;
else
throw new \Exception("Template `$filename`not found!");
}
public function template_html(String $filename):void
{
if (file_exists($filename))
$this->template["html"] = $filename;
else
throw new \Exception("Template `$filename`not found!");
}
public function assign(String $key, String $value):void
{
$this->stpl->assign($key, $value);
}
public function attachfile($uri, $filename, $type)
{
return $this->attach(file_get_contents($uri), $filename, $type);
}
public function attach($content, $filename, $type)
{
$this->attachments[] = array("content"=>$content, "filename"=>$filename, "type"=>$type);
}
public function send():void
{
$message = $headers="";
# Global Headers
$headers .= 'MIME-Version: 1.0'."\r\n";
if (isset($this->from)) // From-header
$headers .= 'From: '.$this->from["fullname"].' <'.$this->from["email"].'>' . "\r\n";
if (isset($this->replyto)) // Replyto-header
$headers .= 'Reply-To: '.$this->replyto["fullname"].' <'.$this->replyto["email"].'>' . "\r\n";
# Message specific headers
if ((isset($this->template["html"])) and (isset($this->template["plain"])) or ($this->attachments)) // Multimime
{
$boundary=md5(uniqid(rand()));
$headers .= 'Content-Type: multipart/alternative;boundary="alt-'.$boundary."\"\r\n";
if (($this->template["plain"]) and ($this->template["html"]))
{
$message = "This is multipart message using MIME\n";
}
if ($this->template["plain"])
{
$message .= "\r\n\r\n--alt-" . $boundary . "\r\n";
$message .= 'Content-type: text/plain;charset=UTF-8' . "\r\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= $this->stpl->fetch($this->template["plain"]);
}
if ($this->template["html"])
{
$message .= "\r\n\r\n--alt-" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= $this->stpl->fetch($this->template["html"]);
}
$message .= "--alt-" . $boundary . "--\n";
foreach ($this->attachments as $a)
{
$message .= "--mix-" . $boundary . "\n";
$message .= "Content-Type: ".$a["type"]."; name=\"" . $a["filename"] . "\"" . "\n";
$message .= "Content-Transfer-Encoding: base64" . "\n";
$message .= "Content-Disposition: attachment" . "\n\n";
$message .= base64_encode($a["content"]);
$message .= "\n";
}
$message .= "--mix-" . $boundary . "--";
}
# Send the messages
foreach ($this->to as $r)
mail($r["fullname"]. "<".$r["email"].">",$this->subject,$message,$headers);
}
}
?>