emails.php 5.06 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-app-emails.php
	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
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
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	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
79

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

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

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

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

Scott committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	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
119

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


Scott committed
124 125 126 127 128 129 130 131 132
/**
 * 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
133

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

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

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

Scott committed
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 168
	$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
169 170
		}

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

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