emails.php 4.94 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 23 24 25 26 27
<?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
*/

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

Scott committed
28
	require_once QA_INCLUDE_DIR.'app/options.php';
Scott committed
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


	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;

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


	function qa_send_notification($userid, $email, $handle, $subject, $body, $subs, $html = false)
/*
	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.
*/
	{
		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;

Scott committed
57
		require_once QA_INCLUDE_DIR.'db/selects.php';
Scott committed
58
		require_once QA_INCLUDE_DIR.'util/string.php';
Scott committed
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

		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];
					}

					if ($needemail)
						$email=qa_get_user_email($userid);

				} else {
					$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'];
				}
			}
		}

		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,
			));

		} else
			return false;
	}


	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
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	//	@error_log(print_r($params, true));

		require_once QA_INCLUDE_DIR.'vendor/PHPMailer/PHPMailerAutoload.php';

		$mailer=new PHPMailer();
		$mailer->CharSet='utf-8';

		$mailer->From=$params['fromemail'];
		$mailer->Sender=$params['fromemail'];
		$mailer->FromName=$params['fromname'];
133
		$mailer->addAddress($params['toemail'], $params['toname']);
134 135 136
		if(!empty($params['replytoemail'])){
			$mailer->addReplyTo($params['replytoemail'], $params['replytoname']);
		}
Scott committed
137 138 139 140
		$mailer->Subject=$params['subject'];
		$mailer->Body=$params['body'];

		if ($params['html'])
141
			$mailer->isHTML(true);
Scott committed
142 143

		if (qa_opt('smtp_active')) {
144
			$mailer->isSMTP();
Scott committed
145 146 147
			$mailer->Host=qa_opt('smtp_address');
			$mailer->Port=qa_opt('smtp_port');

148
			if (qa_opt('smtp_secure')){
Scott committed
149
				$mailer->SMTPSecure=qa_opt('smtp_secure');
150 151 152 153 154 155 156 157 158 159
			}
			else {
				$mailer->SMTPOptions=array(
					'ssl' => array(
						'verify_peer' => false,
						'verify_peer_name' => false,
						'allow_self_signed' => true
					)
				);
			}
Scott committed
160 161 162 163 164 165 166 167

			if (qa_opt('smtp_authenticate')) {
				$mailer->SMTPAuth=true;
				$mailer->Username=qa_opt('smtp_username');
				$mailer->Password=qa_opt('smtp_password');
			}
		}

168
		$send_status = $mailer->send();
William W committed
169 170 171 172
		if(!$send_status){
			@error_log('PHP Question2Answer email send error: '.$mailer->ErrorInfo);
		}
		return $send_status;
Scott committed
173 174 175 176 177 178
	}


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