1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SMTP Server class |
4
|
|
|
* |
5
|
|
|
* This file describes the SMTP Server class |
6
|
|
|
* |
7
|
|
|
* PHP version 5 and 7 |
8
|
|
|
* |
9
|
|
|
* @author Patrick Boyd / [email protected] |
10
|
|
|
* @copyright Copyright (c) 2016, Austin Artistic Reconstruction |
11
|
|
|
* @license http://www.apache.org/licenses/ Apache 2.0 License |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Email; |
15
|
|
|
|
16
|
|
|
require '/var/www/common/libs/PHPMailer/PHPMailerAutoload.php'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An class to represent an SMTPSErver |
20
|
|
|
*/ |
21
|
|
|
class SMTPServer extends EmailService |
22
|
|
|
{ |
23
|
|
|
protected $smtp; |
24
|
|
|
|
25
|
|
|
public function __construct($params) |
26
|
|
|
{ |
27
|
|
|
$this->smtp = new \PHPMailer(); |
28
|
|
|
$this->smtp->SMTPDebug = 3; |
29
|
|
|
$this->smtp->isSMTP(); |
30
|
|
|
|
31
|
|
|
$this->smtp->Host = $params['host']; |
32
|
|
|
if(isset($params['port'])) |
33
|
|
|
{ |
34
|
|
|
$this->smtp->Port = $params['port']; |
35
|
|
|
} |
36
|
|
|
if(isset($params['encryption'])) |
37
|
|
|
{ |
38
|
|
|
$this->smtp->SMTPSecure = $params['encryption']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if(isset($params['username'])) |
42
|
|
|
{ |
43
|
|
|
$this->smtp->SMTPAuth = true; |
44
|
|
|
$this->smtp->Username = $params['username']; |
45
|
|
|
$this->smtp->Password = $params['password']; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function canSend() |
50
|
|
|
{ |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function decodeAddress($address) |
55
|
|
|
{ |
56
|
|
|
$pos = strpos($address, '<'); |
57
|
|
|
if($pos === false) |
58
|
|
|
{ |
59
|
|
|
return array($address); |
60
|
|
|
} |
61
|
|
|
$ret = array(); |
62
|
|
|
$ret[0] = trim(substr($address, $pos+1), '>'); |
63
|
|
|
$ret[1] = substr($address, 0, $pos-1); |
64
|
|
|
return $ret; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function sendEmail($email) |
68
|
|
|
{ |
69
|
|
|
foreach($email->getToAddresses() as $to) |
70
|
|
|
{ |
71
|
|
|
if(strstr($to, 'free.fr') !== false) |
72
|
|
|
{ |
73
|
|
|
die('Spammer abuse filter!'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->smtp->isHTML(true); |
78
|
|
|
$from = $this->decodeAddress($email->getFromAddress()); |
79
|
|
|
call_user_func_array(array($this->smtp, 'setFrom'), $from); |
80
|
|
|
$to = $email->getToAddresses(); |
81
|
|
|
foreach($to as $recip) |
82
|
|
|
{ |
83
|
|
|
$this->smtp->addAddress($recip); |
84
|
|
|
} |
85
|
|
|
$cc = $email->getCCAddresses(); |
86
|
|
|
foreach($cc as $recip) |
87
|
|
|
{ |
88
|
|
|
$this->smtp->addCC($recip); |
89
|
|
|
} |
90
|
|
|
$bcc = $email->getBCCAddresses(); |
91
|
|
|
foreach($bcc as $recip) |
92
|
|
|
{ |
93
|
|
|
$this->smtp->addBCC($recip); |
94
|
|
|
} |
95
|
|
|
$rep = $this->decodeAddress($email->getReplyTo()); |
96
|
|
|
call_user_func_array(array($this->smtp, 'addReplyTo'), $rep); |
97
|
|
|
$this->smtp->Subject = $email->getSubject(); |
98
|
|
|
$this->smtp->Body = $email->getHTMLBody(); |
99
|
|
|
$this->smtp->AltBody = $email->getTextBody(); |
100
|
|
|
if($email->hasAttachments()) |
101
|
|
|
{ |
102
|
|
|
$attachs = $email->getAttachments(); |
103
|
|
|
foreach($attachs as $attach) |
104
|
|
|
{ |
105
|
|
|
$this->smtp->addStringAttachment($attach['data'], $attach['name'], 'base64', $attach['mimeType']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return $this->smtp->send(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
112
|
|
|
?> |
|
|
|
|
113
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.