This repository was archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpMailSmtp.php
120 lines (109 loc) · 4.56 KB
/
PhpMailSmtp.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
<?php
/*
* Copyright 2007-2017 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
namespace Pydio\Mailer\Implementation;
use Exception;
use PHPMailer;
use phpmailerException;
use Pydio\Core\Model\Context;
use Pydio\Core\Model\ContextInterface;
use Pydio\Mailer\Core\Mailer;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Send notifications to user on some predefined actions
* @package AjaXplorer_Plugins
* @subpackage Mailer
*/
class PhpMailSmtp extends Mailer
{
/**
* @param ContextInterface $ctx
* @param $recipients
* @param $subject
* @param $body
* @param null $from
* @param array $images
* @param bool $useHtml
* @throws Exception
* @throws phpmailerException
*/
protected function sendMailImpl(ContextInterface $ctx, $recipients, $subject, $body, $from = null, $images = array(), $useHtml = true)
{
require_once("vendor/autoload.php");
$realRecipients = $this->resolveAdresses($ctx, $recipients);
if(!count($realRecipients)){
return;
}
// NOW IF THERE ARE RECIPIENTS FOR ANY REASON, GO
$mail = new PHPMailer(true);
//All option are set in the PHPMailer class
$mail->Mailer = $this->getContextualOption(Context::emptyContext(), "MAILER");
// see https://github.com/PHPMailer/PHPMailer for references
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth=true;
$mail->Username= $this->getContextualOption(Context::emptyContext(), "SMTP_USER");
$mail->Password= $this->getContextualOption(Context::emptyContext(), "SMTP_PASS");
$mail->Host= $this->getContextualOption(Context::emptyContext(), "SMTP_HOST");
$mail->Port= $this->getContextualOption(Context::emptyContext(), "SMTP_PORT");
$mail->SMTPSecure= $this->getContextualOption(Context::emptyContext(), "SMTP_PREFIX");
$mail->Sendmail = $this->getContextualOption(Context::emptyContext(), "SENDMAIL_PATH");
$from = $this->resolveFrom($ctx, $from);
if (!is_array($from) || empty($from["adress"])) {
throw new Exception("Cannot send email without a FROM address. Please check your core.mailer configuration.");
}
if (!empty($from)) {
if ($from["adress"] != $from["name"]) {
$mail->setFrom($from["adress"], $from["name"]);
} else {
$mail->setFrom($from["adress"]);
}
}
foreach ($realRecipients as $address) {
if ($address["adress"] == $address["name"]) {
$mail->addAddress(trim($address["adress"]));
} else {
$mail->addAddress(trim($address["adress"]), trim($address["name"]));
}
}
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->isHTML($useHtml); // set email format to HTML
$mail->CharSet = "utf-8";
$mail->Encoding = $this->getContextualOption(Context::emptyContext(), "MAIL_ENCODING");
foreach ($images as $image) {
$mail->addEmbeddedImage($image["path"], $image["cid"], '', 'base64', 'image/png');
}
$mail->Subject = $subject;
if ($useHtml) {
if (strpos($body, "<html") !== false) {
$mail->Body = $body;
} else {
$mail->Body = "<html><body>" . nl2br($body) . "</body></html>";
}
$mail->AltBody = Mailer::simpleHtml2Text($mail->Body);
} else {
$mail->Body = Mailer::simpleHtml2Text($body);
}
if (!$mail->send()) {
$message = "Message could not be sent\n";
$message .= "Mailer Error: " . $mail->ErrorInfo;
throw new Exception($message);
}
}
}