forked from sitemason/sm6-devlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMEmail.php
executable file
·200 lines (150 loc) · 5.6 KB
/
SMEmail.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*------------------------------------------------------------------------------------------
File: SMEmail.php
Summary: Supports creating an email and sending it.
Version: 6.0
PRE-RELEASE
Copyright (C) 2014 Sitemason, Inc. All Rights Reserved.
------------------------------------------------------------------------------------------*/
include ('PHPMailer/class.phpmailer.php');
class SMEmail extends SMObject {
protected $attachmentPath;
protected $bccRecipients;
protected $body;
protected $ccRecipients;
protected $debugLevel;
protected $language;
protected $recipients;
protected $sender;
protected $senderName;
protected $subject;
protected $smtpAccounts;
protected $phpMailer;
function __construct() {
$this->smtpAccounts = array();
// TODO: get this data from Sitemason
/*
$smtpAccount = array(
'server' => 'smtp.emailsrvr.com',
'username' => 'user@domain.tld',
'password' => 'password'
);
$this->addSMTPAccount($smtpAccount);
*/
$this->language = 'en';
$this->isHTML = false;
$this->recipients = array();
$this->debugLevel = 0;
}
//!
//! Basic get/set methods
//!------------------------------
public function getAttachmentPath() { return $this->attachmentPath; }
public function setAttachmentPath($attachmentPath) { $this->attachmentPath = $attachmentPath; }
public function getBCCRecipients() { return $this->bccRecipients; }
public function addBCCRecipient($recipient) {
array_push($this->bccRecipients, $recipient);
}
public function setBccRecipients(array $recipients) { $this->bccRecipients = $recipients; }
public function getBody() { return $this->body; }
public function setBody($body) { $this->body = $body; }
public function getCCRecipients() { return $this->ccRecipients; }
public function addCCRecipient($recipient) {
array_push($this->ccRecipients, $recipient);
}
public function setCCRecipients(array $recipients) { $this->ccRecipients = $recipients; }
public function getDebugLevel() { return $this->debugLevel; }
public function setDebugLevel($debugLevel) { $this->debugLevel = $debugLevel; }
public function getError() { return $this->phpMailer->ErrorInfo; }
public function isHTML() {
if ($this->isHTML) { return true; }
else { return false; }
}
public function setIsHTML($isHTML) { $this->isHTML = $isHTML; }
public function getLanguage() { return $this->language; }
public function setLanguage($language) { $this->language = $language; }
public function getRecipients() { return $this->recipients; }
public function addRecipient($recipient) {
array_push($this->recipients, $recipient);
}
public function setRecipients(array $recipients) { $this->recipients = $recipients; }
public function getSender() { return $this->sender; }
public function setSender($sender) { $this->sender = $sender; }
public function getSenderName() { return $this->senderName; }
public function setSenderName($senderName) { $this->senderName = $senderName; }
/**
Add an SMTP Account to the
*/
public function addSMTPAccount($smtpAccountData) {
if ($smtpAccountData['username'] && $smtpAccountData['password'] && $smtpAccountData['server']) {
$accounts = $this->getSMTPAccounts();
array_push($accounts, $smtpAccountData);
$this->setSMTPAccounts($accounts);
return true;
}
else {
return false;
}
}
public function getSMTPAccounts() { return $this->smtpAccounts; }
protected function setSMTPAccounts($smtpAccounts) { $this->smtpAccounts = $smtpAccounts; }
public function getSubject() { return $this->subject; }
public function setSubject($subject) { $this->subject = $subject; }
//!
//! Email-sending methods
//!------------------------------
protected function selectRandomSMTPAccount() {
$accounts = $this->getSMTPAccounts();
$size = sizeof($accounts);
$random = rand(0,$size-1);
$account = $accounts[$random];
return $account;
}
public function send() {
$account = $this->selectRandomSMTPAccount();
if ($account && count($this->getRecipients()) > 0) {
$this->phpMailer = new PHPMailer();
// Recipients
foreach ($this->getRecipients() as $email) {
$this->phpMailer->AddAddress($email);
}
// CC Recipients
if (count($this->getCCRecipients()) > 0) {
foreach ($this->getCCRecipients() as $email) {
$this->phpMailer->AddCC($email);
}
}
// BCC Recipients
if (count($this->getBCCRecipients()) > 0) {
foreach ($this->getBCCRecipients() as $email) {
$this->phpMailer->AddBCC($email);
}
}
$this->phpMailer->Host = $account['server'];
$this->phpMailer->SMTPAuth = 'true';
$this->phpMailer->SMTPDebug = $this->getDebugLevel();
$this->phpMailer->Mailer = 'smtp';
$this->phpMailer->Username = $account['username'];
$this->phpMailer->Password = $account['password'];
if ($this->getAttachmentPath()) {
$this->phpMailer->AddAttachment($this->getAttachmentPath(), $name = "", $encoding = "base64", $type = "application/octet-stream");
}
$this->phpMailer->From = $this->getSender();
$this->phpMailer->FromName = $this->getSenderName();
$this->phpMailer->IsHTML($this->isHTML());
$this->phpMailer->Subject = $this->getSubject();
$this->phpMailer->Body = $this->getBody();
$this->phpMailer->SetLanguage($this->getLanguage());
$test = $this->phpMailer->Send();
$this->phpMailer->ClearAddresses();
$this->phpMailer->ClearAttachments();
if ($test) {
return true;
}
else {
return false;
}
}
}
}
?>