2d674ac3d6f35525dde8d5252d00c073c3c9f1e6
[timetracker.git] / WEB-INF / lib / mail / Mailer.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class Mailer {
30   var $mMailMode;
31   var $mCharSet = 'iso-8859-1';
32   var $mContentType = 'text/plain';
33   var $mSender;
34   var $mReceiver;
35   var $mReceiverCC;
36   var $mReceiverBCC;
37
38   function __construct($type='mail') {
39     $this->mMailMode = $type;
40   }
41
42   function setMailMode($value) {
43     $this->mMailMode = $value;
44   }
45
46   function setCharSet($value) {
47     $this->mCharSet = $value;
48   }
49
50   function setContentType($value) {
51     $this->mContentType = $value;
52   }
53
54   function setReceiver($value) {
55     $this->mReceiver = $value;
56   }
57
58   function setReceiverCC($value) {
59     $this->mReceiverCC = $value;
60   }
61
62   function setReceiverBCC($value) {
63     $this->mReceiverBCC = $value;
64   }
65
66   function setSender($value) {
67     $this->mSender = $value;
68   }
69
70   function send($subject, $data) {
71     $data = chunk_split(base64_encode($data));
72     $subject = Mailer::mimeEncode($subject, $this->mCharSet);
73
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'));
82
83     // PEAR::Mail
84     require_once('Mail.php');
85
86     $recipients = $this->mReceiver;
87     switch ($this->mMailMode) {
88       case 'mail':
89         $mail = Mail::factory('mail');
90         break;
91
92     case 'smtp':
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);
97         }
98         if (!empty($this->mReceiverBCC)) {
99           // make exactly one space after a comma
100           $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverBCC);
101         }
102
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 = (defined('MAIL_SMTP_AUTH') && isTrue(MAIL_SMTP_AUTH)) ? true : false;
108         $debug = (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG)) ? true : false;
109
110         $mail = Mail::factory('smtp', array ('host' => $host,
111           'port' => $port,
112           'username' => $username,
113           'password' => $password,
114           'auth' => $auth,
115           'debug' => $debug));
116         break;
117     }
118
119     if (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG))
120       PEAR::setErrorHandling(PEAR_ERROR_PRINT);
121
122     $res = $mail->send($recipients, $headers, $data);
123     return (!is_a($res, 'PEAR_Error'));
124   }
125
126   function mimeEncode($in_str, $charset) {
127     $out_str = $in_str;
128     if ($out_str && $charset) {
129       $start = '=?'.strtoupper($charset).'?B?';
130       $end = '?=';
131       $out_str = base64_encode($out_str);
132       $out_str = $start . $out_str . $end;
133     }
134     return $out_str;
135   }
136 }