summaryrefslogtreecommitdiff
path: root/includes/classes/BitMailer.php
blob: dcf7460514816d906ca85d21c1a5069bd40065f7 (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$
 *
 * +----------------------------------------------------------------------+
 * | Copyright ( c ) 2008, bitweaver.org
 * +----------------------------------------------------------------------+
 * | All Rights Reserved. See below for details and a complete
 * | list of authors.
 * | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE.
 * | See http://www.gnu.org/copyleft/lesser.html 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$
 * @package  kernel 
 */

/**
 * Initialization
 */
use Bitweaver\Liberty\LibertyBase;

/**
 * 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=[] ){
		global $gBitSystem;
		$message = $pHeaders;
		$message['subject'] = $pSubject;
		if( is_string( $pBody ) ){
			$message['message'] = $pBody;
		}elseif( is_array( $pBody ) ){
			$message = array_merge( $message, $pBody );
		}
		$mailer = $this->buildMailer($message);

		if( is_string( $pRecipients ) ) {
			$pRecipients = [ [ '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() ) {
					\Bitweaver\bit_error_log( $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 EXTERNAL_LIBS_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()
		$mailer->CharSet  = 'UTF-8';
		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(), EXTERNAL_LIBS_PATH.'phpmailer/language/' ) ) {
			$mailer->SetLanguage( 'en' );
		}

		if( !empty( $pMessage['x_headers'] ) && is_array( $pMessage['x_headers'] ) ) {
			foreach( $pMessage['x_headers'] as $name=>$value ) {
				/* Not sure what this is intended to do
				   but nothing seems to use it yet but boards
				   that I am hacking on now. 29-11-08
				   XOXO - Nick
				if( !$mailer->set( $name, $value ) ) {
					$mailer->$name = $value;
					bit_error_log( $mailer->ErrorInfo );
				}
				*/
				$mailer->AddCustomHeader($name.":".$value);
			}
		}

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

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

		return $mailer;
	}

}