summaryrefslogtreecommitdiff
path: root/BitMailer.php
blob: 11e655e58942c7a3b6de53d79cfeaef46103ac44 (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
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
<?php

/**
 * @version $Header: /cvsroot/bitweaver/_bit_kernel/BitMailer.php,v 1.5 2008/06/18 10:10:08 lsces Exp $
 *
 * +----------------------------------------------------------------------+
 * | Copyright ( c ) 2008, bitweaver.org
 * +----------------------------------------------------------------------+
 * | All Rights Reserved. See copyright.txt for details and a complete
 * | list of authors.
 * | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE.
 * | See license.txt for details
 * |
 * | For comments, please use phpdocu.sourceforge.net standards!!!
 * | -> see http://phpdocu.sourceforge.net/
 * +----------------------------------------------------------------------+
 * | Authors: nick <nick@sluggardy.net>
 * +----------------------------------------------------------------------+
 *
 * BitMailer class
 *
 * This is a base class to derive more capabale mailing services
 *
 * @author   nick <nick@sluggardy.net>
 * @version  $Revision: 1.5 $
 * @package  kernel 
 */

/**
 * Initialization
 */
require_once( LIBERTY_PKG_PATH . 'LibertyBase.php' );

/**
 * BitMailer 
 * 
 * @package  kernel
 */
 class BitMailer extends LibertyBase {
	/**
	 * Sends an email to the specified recipients.
	 * This is a convenience method for packages
	 * to be able to use the email sending features
	 * found in this package.
	 *
	 * $pSubject - The Subject of the Email
	 * $pBody - The Body of the Email
	 * $pRecipients - An associative array with keys for email and optionally login and real_name
	 **/
	function sendEmail($pSubject, $pBody, $pRecipients, $pHeaders=array() ){
		global $gBitSystem;
		$message = $pHeaders;
		$message['subject'] = $pSubject;
		$message['message'] = $pBody;
		$mailer = $this->buildMailer($message);

		if( is_string( $pRecipients ) ) {
			$pRecipients = array( array( 'email' => $pRecipients ) );
		}

		foreach ($pRecipients as $to) {
			if( !empty($to['email'] ) ) {
				if (isset($to['real_name']) || isset($to['login'])) {
					$mailer->AddAddress( $to['email'], empty($to['real_name']) ? $to['login'] : $to['real_name'] );
				} else {
					$mailer->AddAddress( $to['email'] );
				}
				if( !$mailer->Send() ) {
					bit_log_error( $mailer->ErrorInfo );
				}
				$mailer->ClearAddresses();
			}
		}
		return $mailer->MessageID;
	}

	/**
	 * Returns a PHPMailer with everything set except the recipients
	 *
	 * $pMessage['subject'] - The subject
	 * $pMessage['message'] - The HTML body of the message
	 * $pMessage['alt_message'] - The Non HTML body of the message
	 */
	function buildMailer($pMessage) {
		global $gBitSystem, $gBitLanguage;

		require_once( UTIL_PKG_PATH.'phpmailer/class.phpmailer.php' );

		$mailer = new PHPMailer();
		$mailer->From     = !empty( $pMessage['from'] ) ? $pMessage['from'] : $gBitSystem->getConfig( 'bitmailer_sender_email', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) );
		$mailer->FromName = !empty( $pMessage['from_name'] ) ? $pMessage['from_name'] : $gBitSystem->getConfig( 'bitmailer_from', $gBitSystem->getConfig( 'site_title' ) );
		if( !empty( $pMessage['sender'] ) ) {
			$mailer->Sender = $pMessage['sender'];
		}
		$mailer->Host     = $gBitSystem->getConfig( 'bitmailer_servers', $gBitSystem->getConfig( 'kernel_server_name', '127.0.0.1' ) );
		$mailer->Mailer   = $gBitSystem->getConfig( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP()
		if( $gBitSystem->getConfig( 'bitmailer_smtp_username' ) ) {
			$mailer->SMTPAuth = TRUE;
			$mailer->Username = $gBitSystem->getConfig( 'bitmailer_smtp_username' );
		}
		if( $gBitSystem->getConfig( 'bitmailer_smtp_password' ) ) {
			$mailer->Password = $gBitSystem->getConfig( 'bitmailer_smtp_password' );
		}
		$mailer->WordWrap = $gBitSystem->getConfig( 'bitmailer_word_wrap', 75 );
		if( !$mailer->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_PATH.'phpmailer/language/' ) ) {
			$mailer->SetLanguage( 'en' );
		}

		if( !empty( $pMessage['x_headers'] ) && is_array( $pMessage['x_headers'] ) ) {
			foreach( $pMessage['x_headers'] as $name=>$value ) {
				if( !$mailer->set( $name, $value ) ) {
					$mailer->$name = $value;
					bit_log_error( $mailer->ErrorInfo );
				}
			}
		}

		$mailer->ClearReplyTos();
		$mailer->AddReplyTo( $gBitSystem->getConfig( 'bitmailer_from' ) );
		if (empty($pMessage['subject'])) {
			$mailer->Subject = $gBitSystem->getConfig('site_title', '').
				(empty($pMessage['package']) ? '' : " : ".$pMessage['package']).
				(empty($pMessage['type']) ? '' : " : ".$pMessage['type']);
		}
		else {
			$mailer->Subject = $pMessage['subject'];
		}

		if (!empty($pMessage['message'])) {
			$mailer->Body    = $pMessage['message'];
			$mailer->IsHTML( TRUE );
			if (!empty($pMessage['alt_message'])) {
				$mailer->AltBody = $pMessage['alt_message'];
			}
			else {
				$mailer->AltBody = '';
			}
		}
		elseif (!empty($pmessage['alt_message'])) {
			$mailer->Body = $pMessage['alt_message'];
			$mailer->IsHTML( FALSE );
		}

		return $mailer;
	}


}

?>