1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Email |
5
|
|
|
* |
6
|
|
|
* Send messages via Email services. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Email { |
15
|
|
|
use Module; |
16
|
|
|
|
17
|
|
|
protected static $driver; |
18
|
|
|
|
19
|
|
|
protected static function agent(){ |
20
|
|
|
return static::$driver; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function using($driver, $options = null){ |
24
|
|
|
$class = '\\Email\\'.ucfirst(strtolower($driver)); |
25
|
|
|
if ( ! class_exists($class) ) throw new \Exception("[Core.Email] : $driver driver not found."); |
26
|
|
|
$driver_service = Service::registerFactory('core.email',function() use ($class, $options){ |
|
|
|
|
27
|
|
|
return new $class($options); |
28
|
|
|
}); |
29
|
|
|
static::$driver = Service::{'core.email'}(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected static function get_email_parts($value){ |
33
|
|
|
if (strpos($value,'<')!==false){ |
34
|
|
|
$value = str_replace('>','',$value); |
35
|
|
|
$parts = explode('<',$value,2); |
36
|
|
|
$name = trim(current($parts)); |
37
|
|
|
$email = trim(end($parts)); |
38
|
|
|
return (object) [ |
39
|
|
|
'name' => $name, |
40
|
|
|
'email' => $email, |
41
|
|
|
]; |
42
|
|
|
} else { |
43
|
|
|
return (object) [ |
44
|
|
|
'name' => '', |
45
|
|
|
'email' => $value, |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public static function makeEnvelope(){ |
|
|
|
|
51
|
|
|
$uid = md5(uniqid(time())); |
52
|
|
|
$head = $body = []; |
53
|
|
|
|
54
|
|
|
if ($this->from) $head[] = 'From: ' . $this->from; |
|
|
|
|
55
|
|
|
if ($this->replyTo) $head[] = 'Reply-To: ' . $this->replyTo; |
|
|
|
|
56
|
|
|
|
57
|
|
|
$head[] = 'MIME-Version: 1.0'; |
58
|
|
|
$head[] = "Content-Type: multipart/mixed; boundary=\"".$uid."\""; |
59
|
|
|
|
60
|
|
|
$body[] = "--$uid"; |
61
|
|
|
$body[] = "Content-type: text/html; charset=UTF-8"; |
62
|
|
|
$body[] = "Content-Transfer-Encoding: quoted-printable"; |
63
|
|
|
$body[] = ''; |
64
|
|
|
$body[] = quoted_printable_encode($this->message); |
|
|
|
|
65
|
|
|
$body[] = ''; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
foreach ($this->attachments as $file) { |
|
|
|
|
69
|
|
|
if (is_string($file)) { |
70
|
|
|
$name = basename($file); |
71
|
|
|
$data = file_get_contents($file); |
72
|
|
|
} else { |
73
|
|
|
$name = $file['name']; |
74
|
|
|
$data = $file['content']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$body[] = "--$uid"; |
78
|
|
|
$body[] = "Content-type: application/octet-stream; name=\"".$name."\""; |
79
|
|
|
$body[] = "Content-Transfer-Encoding: base64"; |
80
|
|
|
$body[] = "Content-Disposition: attachment; filename=\"".$name."\""; |
81
|
|
|
$body[] = ''; |
82
|
|
|
$body[] = chunk_split(base64_encode($data)); |
83
|
|
|
$body[] = ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$body[] = "--$uid--"; |
87
|
|
|
|
88
|
|
|
$success = true; |
89
|
|
|
$head = implode("\r\n",$head); |
90
|
|
|
$body = implode("\r\n",$body); |
91
|
|
|
|
92
|
|
|
foreach ($this->recipients as $to) { |
|
|
|
|
93
|
|
|
$current_success = mail( |
94
|
|
|
$to, |
95
|
|
|
$this->subject, |
|
|
|
|
96
|
|
|
$body, |
97
|
|
|
$head |
98
|
|
|
); |
99
|
|
|
\Event::trigger('core.email.send', $to, $this->from, $this->subject, $body, $success); |
100
|
|
|
$success = $success && $current_success; |
101
|
|
|
} |
102
|
|
|
return $success; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected static function send(array $options){ |
106
|
|
|
$mail = static::agent(); |
107
|
|
|
|
108
|
|
|
$options = array_merge([ |
109
|
|
|
'to' => '', |
110
|
|
|
'from' => '', |
111
|
|
|
'replyTo' => '', |
112
|
|
|
'subject' => '', |
113
|
|
|
'message' => '', |
114
|
|
|
'attachments' => [], |
115
|
|
|
], $options); |
116
|
|
|
|
117
|
|
|
// To |
118
|
|
|
foreach ((array)$options['to'] as $value){ |
119
|
|
|
$to = static::get_email_parts($value); |
120
|
|
|
empty($to->name) |
121
|
|
|
? $mail->addAddress($to->email) |
122
|
|
|
: $mail->addAddress($to->email,$to->name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// From |
126
|
|
|
$from = static::get_email_parts($options['from']); |
127
|
|
|
empty($from->name) |
128
|
|
|
? $mail->from($from->email) |
129
|
|
|
: $mail->from($from->email,$from->name); |
130
|
|
|
|
131
|
|
|
// Reply |
132
|
|
|
$replyTo = static::get_email_parts($options['replyTo']); |
133
|
|
|
empty($replyTo->name) |
134
|
|
|
? $mail->replyTo($replyTo->email) |
135
|
|
|
: $mail->replyTo($replyTo->email,$replyTo->name); |
136
|
|
|
|
137
|
|
|
// Subjects |
138
|
|
|
$mail->subject($options['subject']); |
139
|
|
|
|
140
|
|
|
// Message |
141
|
|
|
$mail->message($options['message']); |
142
|
|
|
|
143
|
|
|
// Attachments |
144
|
|
|
foreach ((array)$options['attachments'] as $value){ |
145
|
|
|
$mail->addAttachment($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$result = $mail->send(); |
149
|
|
|
|
150
|
|
|
static::$driver = Service::{'core.email'}(); |
151
|
|
|
|
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
Email::using('native'); |
159
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.