qa-app-emails.php 4.73 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-app-emails.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-app-options.php';


	function qa_suspend_notifications($suspend=true)
/*
	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.
*/
	{
		global $qa_notifications_suspended;
Scott Vivian committed
42

Gideon Greenspan committed
43 44
		$qa_notifications_suspended+=($suspend ? 1 : -1);
	}
Scott Vivian committed
45 46


47
	function qa_send_notification($userid, $email, $handle, $subject, $body, $subs, $html = false)
Gideon Greenspan committed
48 49 50 51 52 53
/*
	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.
*/
	{
Gideon Greenspan committed
54
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
55

Gideon Greenspan committed
56
		global $qa_notifications_suspended;
Scott Vivian committed
57

Gideon Greenspan committed
58 59
		if ($qa_notifications_suspended>0)
			return false;
Scott Vivian committed
60

Gideon Greenspan committed
61 62
		require_once QA_INCLUDE_DIR.'qa-db-selects.php';
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66
		if (isset($userid)) {
			$needemail=!qa_email_validate(@$email); // take from user if invalid, e.g. @ used in practice
			$needhandle=empty($handle);
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70 71 72 73
			if ($needemail || $needhandle) {
				if (QA_FINAL_EXTERNAL_USERS) {
					if ($needhandle) {
						$handles=qa_get_public_from_userids(array($userid));
						$handle=@$handles[$userid];
					}
Scott Vivian committed
74

Gideon Greenspan committed
75 76
					if ($needemail)
						$email=qa_get_user_email($userid);
Scott Vivian committed
77

Gideon Greenspan committed
78 79
				} else {
					$useraccount=qa_db_select_with_pending(
80 81 82 83 84 85
						array(
							'columns' => array('email', 'handle'),
							'source' => '^users WHERE userid = #',
							'arguments' => array($userid),
							'single' => true,
						)
Gideon Greenspan committed
86
					);
Scott Vivian committed
87

Gideon Greenspan committed
88 89
					if ($needhandle)
						$handle=@$useraccount['handle'];
Scott Vivian committed
90

Gideon Greenspan committed
91 92 93 94 95
					if ($needemail)
						$email=@$useraccount['email'];
				}
			}
		}
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99 100 101 102
		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";
Scott Vivian committed
103

Gideon Greenspan committed
104 105 106 107 108 109 110
			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),
111
				'html' => $html,
Gideon Greenspan committed
112
			));
Scott Vivian committed
113

Gideon Greenspan committed
114 115 116
		} else
			return false;
	}
Scott Vivian committed
117

Gideon Greenspan committed
118 119 120 121 122 123 124

	function qa_send_email($params)
/*
	Send the email based on the $params array - the following keys are required (some can be empty): fromemail,
	fromname, toemail, toname, subject, body, html
*/
	{
Gideon Greenspan committed
125
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
126

Gideon Greenspan committed
127
	//	@error_log(print_r($params, true));
Scott Vivian committed
128

Scott committed
129
		require_once QA_INCLUDE_DIR.'vendor/PHPMailer/PHPMailerAutoload.php';
Scott Vivian committed
130

Gideon Greenspan committed
131 132
		$mailer=new PHPMailer();
		$mailer->CharSet='utf-8';
Scott Vivian committed
133

Gideon Greenspan committed
134 135 136 137 138 139 140 141 142
		$mailer->From=$params['fromemail'];
		$mailer->Sender=$params['fromemail'];
		$mailer->FromName=$params['fromname'];
		$mailer->AddAddress($params['toemail'], $params['toname']);
		$mailer->Subject=$params['subject'];
		$mailer->Body=$params['body'];

		if ($params['html'])
			$mailer->IsHTML(true);
Scott Vivian committed
143

Gideon Greenspan committed
144 145 146 147
		if (qa_opt('smtp_active')) {
			$mailer->IsSMTP();
			$mailer->Host=qa_opt('smtp_address');
			$mailer->Port=qa_opt('smtp_port');
Scott Vivian committed
148

Gideon Greenspan committed
149 150
			if (qa_opt('smtp_secure'))
				$mailer->SMTPSecure=qa_opt('smtp_secure');
Scott Vivian committed
151

Gideon Greenspan committed
152 153 154 155 156 157
			if (qa_opt('smtp_authenticate')) {
				$mailer->SMTPAuth=true;
				$mailer->Username=qa_opt('smtp_username');
				$mailer->Password=qa_opt('smtp_password');
			}
		}
Scott Vivian committed
158

Gideon Greenspan committed
159 160 161 162 163 164
		return $mailer->Send();
	}


/*
	Omit PHP closing tag to help avoid accidental output
165
*/