-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMTPMailer.inc.php
158 lines (122 loc) · 4.54 KB
/
SMTPMailer.inc.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
<?php
/**
* @file classes/mail/SMTPMailer.inc.php
*
* Copyright (c) 2013-2014 Simon Fraser University Library
* Copyright (c) 2000-2014 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class SMTPMailer
* @ingroup mail
*
* @brief Class defining a simple SMTP mail client (reference RFCs 821 and 2821).
*
* TODO: TLS support
*/
import('lib.pkp.classes.mail.Mail');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require 'vendor/autoload.php';
class SMTPMailer
{
/** @var $server string SMTP server hostname (default localhost) */
var $server;
/** @var $port string SMTP server port (default 25) */
var $port;
/** @var $auth string Authentication mechanism (optional) (PLAIN | LOGIN | CRAM-MD5 | DIGEST-MD5) */
var $auth;
/** @var $username string Username for authentication (optional) */
var $username;
/** @var $password string Password for authentication (optional) */
var $password;
/** @var $socket int SMTP socket */
var $socket;
/**
* Constructor.
*/
function SMTPMailer()
{
$this->server = 'email-smtp.us-east-1.amazonaws.com';
$this->port = 587;
$this->auth = true;
$this->username = 'Username';
$this->password = 'password';
if (!$this->server)
$this->server = 'localhost';
if (!$this->port)
$this->port = 25;
}
/**
* Send mail.
* @param $mail Mailer
* @param $recipients string
* @param $subject string
* @param $body string
* @param $headers string
*/
function mail(&$mail, $recipients, $subject, $body, $headers = '')
{
try {
$email = new PHPMailer(true);
$email->isSMTP();
$from = $mail->getFrom();
$email->addReplyTo($from['email'],$from['name']);
$currentUserEmail = '[email protected]';
$currentUserName = $from['name'];
$email->setFrom($currentUserEmail, $currentUserName);
$email->Username = $this->username;
$email->Password = $this->password;
$email->Host = $this->server;
$email->Port = $this->port;
$email->SMTPAuth = true;
$email->SMTPSecure = 'tls';
// $email->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);
// Specify the message recipients.
$to_email = $mail->getData('recipients')[0]['email'];
$email->addAddress($to_email);
import('classes.file.TemporaryFileManager');
$temporaryFileManager = new TemporaryFileManager();
$path = $temporaryFileManager->getBasePath();
//Multiple attachment
if (!empty($attachments = $mail->persistAttachments)) {
foreach ($attachments as $attachment) {
$fileName = $attachment->getData('fileName');
$originalFileName = $attachment->getData('originalFileName');
$fileType = $attachment->getData('filetype');
$email->addAttachment($path . $fileName, $originalFileName, 'base64', $fileType);
}
}
//Add CCs
if (!empty($ccs = $mail->getData('ccs'))) {
foreach ($ccs as $cc) {
$email->AddCC($cc['email'], $cc['name']);
}
}
//Add BCCs
if (!empty($bccs = $mail->getData('bccs'))) {
foreach ($bccs as $bcc) {
$email->AddBCC($bcc['email'], $bcc['name']);
}
}
// Specify the content of the message.
$email->Subject = $subject;
if (!empty($mail->getData('body'))) {
$email_body = $mail->getData('body');
}
echo 'body';
$email->Body = $email_body;
$email->Send();
// echo "Email sent!", PHP_EOL;
} catch (phpmailerException $e) {
$error = "An error occurred. ";
error_log('OJS SMTPMailer: ' . $error);
} catch (Exception $e) {
$error = "Email not sent.";
error_log('OJS SMTPMailer: ' . $error);
}
return 1;
}
}
?>