1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once 'Mailer'.DS.'class.phpmailer.php'; |
4
|
|
|
require_once 'Mailer'.DS.'class.smtp.php'; |
5
|
|
|
require_once 'Mailer'.DS.'class.pop3.php'; |
6
|
|
|
|
7
|
|
|
class Ajde_Mailer extends PHPMailer |
8
|
|
|
{ |
9
|
|
|
public function __construct($exceptions = false) |
10
|
|
|
{ |
11
|
|
|
parent::__construct($exceptions); |
12
|
|
|
if (config('mail.mailer') == 'smtp') { |
13
|
|
|
$this->isSMTP(); |
14
|
|
|
$configs = config('mail.config'); |
15
|
|
|
foreach ($configs as $k => $v) { |
16
|
|
|
$this->$k = $v; |
17
|
|
|
} |
18
|
|
|
} else { |
19
|
|
|
$this->isMail(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function sendUsingModel($identifier, $toEmail, $toName = '', $data = []) |
24
|
|
|
{ |
25
|
|
|
$email = new EmailModel(); |
26
|
|
|
if ($email->loadByField('identifier', $identifier)) { |
27
|
|
|
$template = $email->getTemplate(); |
28
|
|
|
|
29
|
|
|
$fromName = $email->getFromName(); |
30
|
|
|
$fromEmail = $email->getFromEmail(); |
31
|
|
|
$subject = $this->replaceData($template->getSubject(), $data); |
32
|
|
|
|
33
|
|
|
$markup = $this->rel2abs($this->replaceData($template->getMarkup(), $data)); |
|
|
|
|
34
|
|
|
$body = PHP_EOL.$this->rel2abs($this->replaceData($template->getContent($markup), $data)); |
35
|
|
|
|
36
|
|
|
// reset recipients |
37
|
|
|
$this->clearAllRecipients(); |
38
|
|
|
|
39
|
|
|
// to |
40
|
|
|
$this->addAddress($toEmail, $toName); |
41
|
|
|
|
42
|
|
|
// from |
43
|
|
|
$this->From = $fromEmail; |
44
|
|
|
|
45
|
|
|
// fromName |
46
|
|
|
$this->FromName = $fromName; |
47
|
|
|
|
48
|
|
|
// subject |
49
|
|
|
$this->Subject = $subject; |
50
|
|
|
|
51
|
|
|
// utf8 please |
52
|
|
|
$this->CharSet = 'utf-8'; |
53
|
|
|
|
54
|
|
|
// body |
55
|
|
|
$this->msgHTML($body); |
56
|
|
|
|
57
|
|
|
// send! |
58
|
|
|
$status = $this->send(); |
59
|
|
|
|
60
|
|
|
// log |
61
|
|
View Code Duplication |
if (class_exists('MailerlogModel')) { |
|
|
|
|
62
|
|
|
MailerlogModel::log($fromEmail, $fromName, $toEmail, $toName, $subject, $body, $status ? 1 : 0); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $status; |
66
|
|
|
} else { |
67
|
|
|
throw new Ajde_Exception('Email with identifier '.$identifier.' not found'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function rel2abs($text) |
72
|
|
|
{ |
73
|
|
|
$base = config('app.rootUrl'); |
74
|
|
|
$replace = '$1'.$base.'$2$3'; |
75
|
|
|
|
76
|
|
|
// Look for images |
77
|
|
|
$pattern = "#(<\s*?img\s*?[^>]*src\s*?=[\"'])(?!http)([^\"'>]+)([\"'>]+)#"; |
78
|
|
|
$text = preg_replace($pattern, $replace, $text); |
79
|
|
|
|
80
|
|
|
// Look for links |
81
|
|
|
$pattern = "#(<\s*?a\s*?[^>]*href\s*?=[\"'])(?!http)([^\"'>]+)([\"'>]+)#"; |
82
|
|
|
$text = preg_replace($pattern, $replace, $text); |
83
|
|
|
|
84
|
|
|
return $text; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function mergeData($data) |
88
|
|
|
{ |
89
|
|
|
$defaultData = [ |
90
|
|
|
'sitename' => config('app.title'), |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
return array_merge($defaultData, $data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function replaceData($string, $data) |
97
|
|
|
{ |
98
|
|
|
$data = $this->mergeData($data); |
99
|
|
|
|
100
|
|
|
foreach ($data as $key => $value) { |
101
|
|
|
$string = str_replace('%'.$key.'%', $value, $string); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $string; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function SendQuickMail($to, $from, $fromName, $subject, $body, $toName = '') |
108
|
|
|
{ |
109
|
|
|
// set class to use PHP mail function |
110
|
|
|
// $this->IsMail(); |
111
|
|
|
|
112
|
|
|
// reset recipients |
113
|
|
|
$this->clearAllRecipients(); |
114
|
|
|
|
115
|
|
|
// to |
116
|
|
|
$this->addAddress($to, $toName); |
117
|
|
|
|
118
|
|
|
// from |
119
|
|
|
$this->From = $from; |
120
|
|
|
|
121
|
|
|
// fromName |
122
|
|
|
$this->FromName = $fromName; |
123
|
|
|
|
124
|
|
|
// subject |
125
|
|
|
$this->Subject = $subject; |
126
|
|
|
|
127
|
|
|
// body |
128
|
|
|
$this->Body = $body; |
129
|
|
|
|
130
|
|
|
// alt body |
131
|
|
|
$this->AltBody = strip_tags($body); |
132
|
|
|
|
133
|
|
|
// set html content type |
134
|
|
|
$this->isHTML(true); |
135
|
|
|
|
136
|
|
|
// send! |
137
|
|
|
$status = $this->send(); |
138
|
|
|
|
139
|
|
|
// log |
140
|
|
View Code Duplication |
if (class_exists('MailerlogModel')) { |
|
|
|
|
141
|
|
|
MailerlogModel::log($from, $fromName, $to, $toName, $subject, $body, $status ? 1 : 0); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $status; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function addAddress($address, $name = '') |
148
|
|
|
{ |
149
|
|
|
if (config('mail.debug') === true) { |
150
|
|
|
$address = config('app.email'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return parent::addAnAddress('to', $address, $name); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: