File: email.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (227 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download
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
137
138
139
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/* 	OpenDb - Open Media Lending Database
	Copyright (C) 2001,2002 by Jason Pell

	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.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

include_once("./functions/logging.php");
include_once("./functions/user.php");
include_once("./functions/utils.php");
include_once("./functions/http.php");

/**
	A simple email validation function.  Used by the main 'email' sending
	routine in this script, which allows calling programs to test their
	email addresses with the same test.
	
	Also used in functions/widgets.php 
*/
function is_valid_email_addr($email_addr)
{
	if ( strlen($email_addr)==0 || !ereg("^.+@.+\\..+$", $email_addr) || strpos($email_addr, ">")!==FALSE)
		return FALSE;
	else
		return TRUE;
}

/**
	Return email footer
*/
function get_email_footer()
{
	// from config.php
	global $CONFIG_VARS;
	global $LANG_VARS;
	
	if(strlen($LANG_VARS['email_footer'])>0)
	{
		$site_url = get_site_url();

		// This footer text will include any newlines that are required, and because the email
		// is text based, these newlines should be respected.  I am not sure what will happen
		// however on Mac / Windows systems, because we only understand '\n' as a newline
		// identifier in the language variables.
		$footer_text = trim(replace_lang_vars(array('site'=>$CONFIG_VARS['site.title'],'version'=>$CONFIG_VARS['site.version'],'site_url'=>$site_url), expand_langvar_newlines($LANG_VARS['email_footer'])));

		// Now lets explode any line breaks into separate lines so we
		// can work out how long the dashed lines need to be.
		$lines_r = explode("\n", $footer_text);
		if(is_array($lines_r) && count($lines_r)>1)
		{
			$length = 0;
			while(list(,$line) = @each($lines_r))
			{
				// We need to get the longest line.
				if(strlen($line)>$length)
					$length = strlen($line);
			}
		}
		else // Only one line.
		{
			$length = strlen($footer_text);
		}
		
		if($length>0)
		{
			for($i=0;$i<$length;$i++)
				$dashed_line .= "-";
			
			// Now return the complete footer text
			return	$dashed_line
					."\n"
					.$footer_text
					."\n"
					.$dashed_line;
		}
	}

	//else - no footer.
	return "";
}

/**
* Email to be sent from one OpenDb user to another
*/
function opendb_user_email($to_userid, $from_userid, $subject, $message)
{
	// from config.php
	global $CONFIG_VARS;
	global $LANG_VARS;

	if(is_user_valid($to_userid) && is_user_valid($from_userid))
	{
		if(opendb_email(
				fetch_user_email($to_userid), 
				fetch_user_name($to_userid), 
				fetch_user_email($from_userid), 
				fetch_user_name($from_userid), 
				$subject, 
				$message))
		{
			// at this point insert a record into the database 
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	else
	{
		return FALSE;
	}
}

/**
	@param to
	@param toname
	@param from
	@param fromname
	@param subject
	@param message
	@param append_site_to_subject Whether the [OpenDb] should be appended to 
			the subject line.

	@returns TRUE on success, or array of errors on failure.
*/
function opendb_email($to, $toname, $from, $fromname, $subject, $message, $append_site_to_subject = TRUE)
{
	// from config.php
	global $CONFIG_VARS;
	global $LANG_VARS;
	
	// trim once only!
	$to = trim($to);
	$from = trim($from);

	if(!is_valid_email_addr($from))
		$errors[] = $LANG_VARS['invalid_from_address'];
	if (strlen(trim($subject))==0)
		$errors[] = $LANG_VARS['invalid_subject'];

	// Check 'to' address
	if(!is_valid_email_addr($to))
		$errors[] = $LANG_VARS['invalid_to_address'];
	
	// Any errors.
	if(is_not_empty_array($errors))
		 return $errors;
	else
	{
		// If friendly email format, include \"Person name\" <email address>
		// Otherwise only specify the <email address>.
		if($CONFIG_VARS['email.friendly_email_format'] !== FALSE)
		{
			// For windows, the $to argument to the mail function is actually
			// a RECPT-TO header.
			if($CONFIG_VARS['email.win32_smtp_email'])
			{
				if(strlen($toname)>0)
					$headers .= "To: \"$toname\" <".$to.">\r\n";
				else
					$headers .= "To: <".$to.">\r\n";
			}
			else
			{
				// *nix machines support including friendly name in $to argument to mail in most cases.
				$to = "\"$toname\" <".$to.">";
			}
			
			if(strlen($fromname)>0)
			{
				$from = "\"$fromname\" <".$from.">";
				$headers .= "From: $from\r\n";
			}
			else
				$headers .= "From: <".$from.">\r\n";
		}
		else
		{
			if($CONFIG_VARS['email.win32_smtp_email'])
				$headers .= "To: <".$to.">\r\n";
			
			$headers .= "From: <$from>\r\n";
		}
		
		// additional header pieces for errors
		$headers .= "X-Sender: <".$CONFIG_VARS['site.admin_email'].">\r\n"; 
		$headers .= "X-Mailer: PHP/".phpversion()."\r\n"; // mailer
		$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
		$headers .= "Return-Path: <".$CONFIG_VARS['site.admin_email'].">\r\n"; // Return path for errors
		
		$message = str_replace("\n", "\r\n", // fix for some email setups which require \r\n newlines for all line endings.
				stripslashes($message).
					"\n\n\n".
					get_email_footer());
					
		$subject = stripslashes($subject).
					($append_site_to_subject?" [".$CONFIG_VARS['site.title']."]":"");
		
		if(@mail($to, $subject, $message, $headers))
		{
			// No errors returned indicates correct execution.
			opendb_log("Email sent to $to from $from");
			return TRUE;
		}
		else
		{
			// No errors returned indicates correct execution.
			opendb_log("Email not sent to $to from $from");
			return FALSE;
		}
	}
}
?>