emails.php 5.03 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Wrapper functions 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
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
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
	exit;
}

require_once QA_INCLUDE_DIR . 'app/options.php';


/**
 * Suspend the sending of all email notifications via qa_send_notification(...) if $suspend is true, otherwise
 * reinstate it. A counter is kept to allow multiple calls.
 * @param bool $suspend
 */
function qa_suspend_notifications($suspend = true)
{
	global $qa_notifications_suspended;

	$qa_notifications_suspended += ($suspend ? 1 : -1);
}


/**
 * Send email to person with $userid and/or $email and/or $handle (null/invalid values are ignored or retrieved from
 * user database as appropriate). Email uses $subject and $body, after substituting each key in $subs with its
 * corresponding value, plus applying some standard substitutions such as ^site_title, ^handle and ^email.
 * @param $userid
 * @param $email
 * @param $handle
 * @param $subject
 * @param $body
 * @param $subs
 * @param bool $html
 * @return bool
 */
function qa_send_notification($userid, $email, $handle, $subject, $body, $subs, $html = false)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	global $qa_notifications_suspended;

	if ($qa_notifications_suspended > 0)
		return false;

	require_once QA_INCLUDE_DIR . 'db/selects.php';
	require_once QA_INCLUDE_DIR . 'util/string.php';

	if (isset($userid)) {
		$needemail = !qa_email_validate(@$email); // take from user if invalid, e.g. @ used in practice
		$needhandle = empty($handle);

		if ($needemail || $needhandle) {
			if (QA_FINAL_EXTERNAL_USERS) {
				if ($needhandle) {
					$handles = qa_get_public_from_userids(array($userid));
					$handle = @$handles[$userid];
				}
Scott committed
78

Scott committed
79 80
				if ($needemail)
					$email = qa_get_user_email($userid);
Scott committed
81

Scott committed
82 83 84 85 86 87 88 89 90
			} else {
				$useraccount = qa_db_select_with_pending(
					array(
						'columns' => array('email', 'handle'),
						'source' => '^users WHERE userid = #',
						'arguments' => array($userid),
						'single' => true,
					)
				);
Scott committed
91

Scott committed
92 93
				if ($needhandle)
					$handle = @$useraccount['handle'];
Scott committed
94

Scott committed
95 96
				if ($needemail)
					$email = @$useraccount['email'];
Scott committed
97 98 99 100
			}
		}
	}

Scott committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	if (isset($email) && qa_email_validate($email)) {
		$subs['^site_title'] = qa_opt('site_title');
		$subs['^handle'] = $handle;
		$subs['^email'] = $email;
		$subs['^open'] = "\n";
		$subs['^close'] = "\n";

		return qa_send_email(array(
			'fromemail' => qa_opt('from_email'),
			'fromname' => qa_opt('site_title'),
			'toemail' => $email,
			'toname' => $handle,
			'subject' => strtr($subject, $subs),
			'body' => (empty($handle) ? '' : qa_lang_sub('emails/to_handle_prefix', $handle)) . strtr($body, $subs),
			'html' => $html,
		));
	}
Scott committed
118

Scott committed
119 120
	return false;
}
Scott committed
121 122


Scott committed
123 124 125 126 127 128 129 130 131
/**
 * Send the email based on the $params array - the following keys are required (some can be empty): fromemail,
 * fromname, toemail, toname, subject, body, html
 * @param $params
 * @return bool
 */
function qa_send_email($params)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott committed
132

Scott committed
133
	// @error_log(print_r($params, true));
Scott committed
134

Scott committed
135
	require_once QA_INCLUDE_DIR . 'vendor/PHPMailer/PHPMailerAutoload.php';
Scott committed
136

Scott committed
137 138
	$mailer = new PHPMailer();
	$mailer->CharSet = 'utf-8';
Scott committed
139

Scott committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	$mailer->From = $params['fromemail'];
	$mailer->Sender = $params['fromemail'];
	$mailer->FromName = $params['fromname'];
	$mailer->addAddress($params['toemail'], $params['toname']);
	if (!empty($params['replytoemail'])) {
		$mailer->addReplyTo($params['replytoemail'], $params['replytoname']);
	}
	$mailer->Subject = $params['subject'];
	$mailer->Body = $params['body'];

	if ($params['html'])
		$mailer->isHTML(true);

	if (qa_opt('smtp_active')) {
		$mailer->isSMTP();
		$mailer->Host = qa_opt('smtp_address');
		$mailer->Port = qa_opt('smtp_port');

		if (qa_opt('smtp_secure')) {
			$mailer->SMTPSecure = qa_opt('smtp_secure');
		} else {
			$mailer->SMTPOptions = array(
				'ssl' => array(
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => true,
				),
			);
Scott committed
168 169
		}

Scott committed
170 171 172 173
		if (qa_opt('smtp_authenticate')) {
			$mailer->SMTPAuth = true;
			$mailer->Username = qa_opt('smtp_username');
			$mailer->Password = qa_opt('smtp_password');
William W committed
174
		}
Scott committed
175 176
	}

Scott committed
177 178 179 180 181 182
	$send_status = $mailer->send();
	if (!$send_status) {
		@error_log('PHP Question2Answer email send error: ' . $mailer->ErrorInfo);
	}
	return $send_status;
}