2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
31 var $mCharSet = 'iso-8859-1';
32 var $mContentType = 'text/plain';
38 function __construct($type='mail') {
39 $this->mMailMode = $type;
42 function setMailMode($value) {
43 $this->mMailMode = $value;
46 function setCharSet($value) {
47 $this->mCharSet = $value;
50 function setContentType($value) {
51 $this->mContentType = $value;
54 function setReceiver($value) {
55 $this->mReceiver = $value;
58 function setReceiverCC($value) {
59 $this->mReceiverCC = $value;
62 function setReceiverBCC($value) {
63 $this->mReceiverBCC = $value;
66 function setSender($value) {
67 $this->mSender = $value;
70 function send($subject, $data) {
71 $data = chunk_split(base64_encode($data));
72 $subject = Mailer::mimeEncode($subject, $this->mCharSet);
74 $headers = array('From' => $this->mSender, 'To' => $this->mReceiver);
75 if (isset($this->mReceiverCC)) $headers = array_merge($headers, array('CC' => $this->mReceiverCC));
76 if (isset($this->mReceiverBCC)) $headers = array_merge($headers, array('BCC' => $this->mReceiverBCC));
77 $headers = array_merge($headers, array(
78 'Subject' => $subject,
79 'MIME-Version' => '1.0',
80 'Content-Type' => $this->mContentType.'; charset='.$this->mCharSet,
81 'Content-Transfer-Encoding' => 'BASE64'));
84 require_once('Mail.php');
86 $recipients = $this->mReceiver;
87 switch ($this->mMailMode) {
89 $mail = Mail::factory('mail');
93 // Mail_smtp does not do CC or BCC -> recipients conversion.
94 if (!empty($this->mReceiverCC)) {
95 // make exactly one space after a comma
96 $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverCC);
98 if (!empty($this->mReceiverBCC)) {
99 // make exactly one space after a comma
100 $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverBCC);
103 $host = defined('MAIL_SMTP_HOST') ? MAIL_SMTP_HOST : 'localhost';
104 $port = defined('MAIL_SMTP_PORT') ? MAIL_SMTP_PORT : '25';
105 $username = defined('MAIL_SMTP_USER') ? MAIL_SMTP_USER : null;
106 $password = defined('MAIL_SMTP_PASSWORD') ? MAIL_SMTP_PASSWORD : null;
107 $auth = isTrue('MAIL_SMTP_AUTH');
108 $debug = isTrue('MAIL_SMTP_DEBUG');
110 $mail = Mail::factory('smtp', array ('host' => $host,
112 'username' => $username,
113 'password' => $password,
119 if (isTrue('MAIL_SMTP_DEBUG'))
120 PEAR::setErrorHandling(PEAR_ERROR_PRINT);
122 $res = $mail->send($recipients, $headers, $data);
123 return (!is_a($res, 'PEAR_Error'));
126 function mimeEncode($in_str, $charset) {
128 if ($out_str && $charset) {
129 $start = '=?'.strtoupper($charset).'?B?';
131 $out_str = base64_encode($out_str);
132 $out_str = $start . $out_str . $end;