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
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
Description: Wrapper class for sending email notifications to users
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Notifications;
use PHPMailer;
use Q2A\Exceptions\FatalErrorException;
use Q2A\Notifications\Mailer;
class Email
{
private $email;
private $handle;
private function __construct($email, $handle)
{
require_once QA_INCLUDE_DIR . 'db/selects.php'; //required for qa_db_select_with_pending()
require_once QA_INCLUDE_DIR . 'app/options.php'; //required for qa_opt()
$this->email = $email;
$this->handle = $handle;
}
/**
* Factory method.
* @param type $userid
* @param type $email
* @param type $handle
* @return \self
* @throws FatalErrorException
*/
public static function createByUserID($userid, $email = '', $handle = '')
{
if (!(int)$userid) {
throw new FatalErrorException('User ID not specified in Notifications/Email/CreateByUserID.');
}
$needemail = !PHPMailer::validateAddress($email); // take from user if invalid
$needhandle = empty($handle);
if (QA_FINAL_EXTERNAL_USERS) {
if ($needhandle) {
$handles = qa_get_public_from_userids(array($userid));
$handle = isset($handles[$userid]) ? $handles[$userid] : '';
}
if ($needemail) {
$email = qa_get_user_email($userid);
}
} elseif ($needemail || $needhandle) {
$useraccount = qa_db_select_with_pending(
array(
'columns' => array('email', 'handle'),
'source' => '^users WHERE userid = #',
'arguments' => array($userid),
'single' => true,
)
);
if ($needhandle) {
$handle = $useraccount['handle'];
}
if ($needemail) {
$email = $useraccount['email'];
}
}
return new self($email, $handle);
}
/**
* Factory method.
* @param type $email
* @param type $handle
* @return \self
*/
public static function createByEmailAddress($email, $handle = '')
{
return new self($email, $handle);
}
public function sendMessage($subject, $body, $subs, $html = false)
{
$fullsubs = $this->buildSubs($subs);
$bodyPrefix = (empty($this->handle) ? '' : qa_lang_sub('emails/to_handle_prefix', $this->handle));
if (PHPMailer::validateAddress($this->email)) {
$mailer = new Mailer(array(
'fromemail' => qa_opt('from_email'),
'fromname' => qa_opt('site_title'),
'toemail' => $this->email,
'toname' => $this->handle,
'subject' => strtr($subject, $fullsubs),
'body' => $bodyPrefix . strtr($body, $fullsubs),
'html' => $html,
));
$send_status = $mailer->send();
if (!$send_status) {
error_log('PHP Question2Answer email send error: ' . $mailer->ErrorInfo);
}
return $send_status;
}
return false;
}
protected function buildSubs($subs)
{
$addsubs = array(
'^site_title' => qa_opt('site_title'),
'^site_url' => qa_opt('site_url'),
'^handle' => $this->handle,
'^email' => $this->email,
'^open' => "\n",
'^close' => "\n"
);
return array_merge($subs, $addsubs);
}
}