summaryrefslogtreecommitdiff
path: root/library/WT/Mail.php
blob: 5506dd36f60b9a046c7db55217a3055027e4a0ff (plain)
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
<?php
// Send mail messages
//
// webtrees: Web based Family History software
// Copyright (c) 2013 webtrees development team
//
// 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

if (!defined('WT_WEBTREES')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

class WT_Mail {
	const EOL = "<br>\r\n"; // End-of-line that works for both TEXT and HTML messages

	// Audit information to add to email footer
	public static function auditFooter() {
		global $WT_REQUEST;

		return
			self::EOL .
			'---------------------------------------' . self::EOL .
			'IP ADDRESS: ' . $WT_REQUEST->getClientIp() . self::EOL .
			'LANGUAGE: '   . WT_LOCALE . self::EOL;
	}

	// Send an external email message
	// Caution! gmail may rewrite the "From" header unless you have added the address to your account.
	public static function send(WT_Tree $tree, $to_email, $to_name, $replyto_email, $replyto_name, $subject, $message) {
		try {
			$mail = new Zend_Mail('UTF-8');
			$mail
				->setSubject ($subject)
				->setBodyHtml($message)
				->setBodyText(WT_Filter::unescapeHtml($message))
				->setFrom    (WT_Site::preference('SMTP_FROM_NAME'), $tree->preference('title'))
				->addTo      ($to_email,                             $to_name)
				->setReplyTo ($replyto_email,                        $replyto_name)
				->send       (WT_Mail::transport());
		} catch (Exception $ex) {
			AddToLog('Mail: ' . $ex->getMessage(), 'error');
			return false;
		}
		return true;
	}

	// Send an automated system message (such as a password reminder) from a tree to a user.
	public static function system_message(WT_Tree $tree, $user_id, $subject, $message) {
		return self::send(
			$tree,
			getUserEmail($user_id),                getUserFullName($user_id),
			WT_Site::preference('SMTP_FROM_NAME'), $tree->preference('title'),
			$subject,
			$message
		);
	}


	// Create a transport mechanism for sending mail
	public static function transport() {
		switch (WT_Site::preference('SMTP_ACTIVE')) {
		case 'internal':
			return new Zend_Mail_Transport_Sendmail();
		case 'external':
			$config = array(
				'name' => WT_Site::preference('SMTP_HELO'),
				'port' => WT_Site::preference('SMTP_PORT'),
			);
			if (WT_Site::preference('SMTP_AUTH')) {
				$config['auth']     = 'login';
				$config['username'] = WT_Site::preference('SMTP_AUTH_USER');
				$config['password'] = WT_Site::preference('SMTP_AUTH_PASS');
			}
			if (WT_Site::preference('SMTP_SSL')) {
				$config['ssl'] = WT_Site::preference('SMTP_SSL');
			}

			return new Zend_Mail_Transport_Smtp(WT_Site::preference('SMTP_HOST'), $config);
		default:
			// For testing
			return new Zend_Mail_Transport_File();
		}
	}
}