Renamed a function for clarity.
[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
37   function __construct($type='mail') {
38     $this->mMailMode = $type;
39   }
40
41   function setMailMode($value) {
42     $this->mMailMode = $value;
43   }
44
45   function setCharSet($value) {
46     $this->mCharSet = $value;
47   }
48
49   function setContentType($value) {
50     $this->mContentType = $value;
51   }
52
53   function setReceiver($value) {
54     $this->mReceiver = $value;
55   }
56
57   function setReceiverCC($value) {
58     $this->mReceiverCC = $value;
59   }
60
61   function setSender($value) {
62     $this->mSender = $value;
63   }
64
65   function send($subject, $data) {
66     $data = chunk_split(base64_encode($data));
67     $subject = Mailer::mimeEncode($subject, $this->mCharSet);
68
69     $headers = array('From' => $this->mSender, 'To' => $this->mReceiver);
70     if (isset($this->mReceiverCC)) $headers = array_merge($headers, array('CC' => $this->mReceiverCC));
71     $headers = array_merge($headers, array(
72       'Subject' => $subject,
73       'MIME-Version' => '1.0',
74       'Content-Type' => $this->mContentType.'; charset='.$this->mCharSet,
75       'Content-Transfer-Encoding' => 'BASE64'));
76
77     // PEAR::Mail
78     require_once('Mail.php');
79
80     $recipients = $this->mReceiver;
81     switch ($this->mMailMode) {
82       case 'mail':
83         $mail = Mail::factory('mail');
84         break;
85
86     case 'smtp':
87         // Mail_smtp does not do CC -> recipients conversion
88         if (!empty($this->mReceiverCC)) {
89           // make exactly one space after a comma
90           $recipients .= ', ' . preg_replace('/,[[:space:]]+/', ', ', $this->mReceiverCC);
91         }
92
93         $host = defined('MAIL_SMTP_HOST') ? MAIL_SMTP_HOST : 'localhost';
94         $port = defined('MAIL_SMTP_PORT') ? MAIL_SMTP_PORT : '25';
95         $username = defined('MAIL_SMTP_USER') ? MAIL_SMTP_USER : null;
96         $password = defined('MAIL_SMTP_PASSWORD') ? MAIL_SMTP_PASSWORD : null;
97         $auth = (defined('MAIL_SMTP_AUTH') && isTrue(MAIL_SMTP_AUTH)) ? true : false;
98         $debug = (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG)) ? true : false;
99
100         $mail = Mail::factory('smtp', array ('host' => $host,
101           'port' => $port,
102           'username' => $username,
103           'password' => $password,
104           'auth' => $auth,
105           'debug' => $debug));
106         break;
107     }
108
109     if (defined('MAIL_SMTP_DEBUG') && isTrue(MAIL_SMTP_DEBUG))
110       PEAR::setErrorHandling(PEAR_ERROR_PRINT);
111
112     $res = $mail->send($recipients, $headers, $data);
113     return (!is_a($res, 'PEAR_Error'));
114   }
115
116   function mimeEncode($in_str, $charset) {
117     $out_str = $in_str;
118     if ($out_str && $charset) {
119       $start = '=?'.strtoupper($charset).'?B?';
120       $end = '?=';
121       $out_str = base64_encode($out_str);
122       $out_str = $start . $out_str . $end;
123     }
124     return $out_str;
125   }
126 }