1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - smtp mailer using PHPMailer * |
---|
4 | * This file written by RalfBecker@outdoor-training.de * |
---|
5 | * ------------------------------------------------------------------------ * |
---|
6 | * This library is free software; you can redistribute it and/or modify it * |
---|
7 | * under the terms of the GNU Lesser General Public License as published by * |
---|
8 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
9 | * or any later version. * |
---|
10 | * This library is distributed in the hope that it will be useful, but * |
---|
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
13 | * See the GNU Lesser General Public License for more details. * |
---|
14 | * You should have received a copy of the GNU Lesser General Public License * |
---|
15 | * along with this library; if not, write to the Free Software Foundation, * |
---|
16 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
17 | \**************************************************************************/ |
---|
18 | |
---|
19 | |
---|
20 | require_once(PHPGW_API_INC.'/class.phpmailer.inc.php'); |
---|
21 | |
---|
22 | /** |
---|
23 | * New eGW send-class. It implements the old interface (msg-method) on top of PHPMailer. |
---|
24 | * |
---|
25 | * The configuration is read from Admin >> Site configuration and it does NOT depend on one of the email-apps anymore. |
---|
26 | * |
---|
27 | * @author RalfBecker@outdoor-training.de |
---|
28 | */ |
---|
29 | class send extends PHPMailer |
---|
30 | { |
---|
31 | var $err = array(); |
---|
32 | var $to_res = array(); |
---|
33 | |
---|
34 | /** |
---|
35 | * eGW specific initialisation of the PHPMailer: charset, language, smtp-host, ... |
---|
36 | * |
---|
37 | * To be able to call PHPMailer's Send function, we check if a subject, body or address is set and call it in that case, |
---|
38 | * else we do our constructors work. |
---|
39 | */ |
---|
40 | function send() |
---|
41 | { |
---|
42 | if ($this->Subject || $this->Body || count($this->to)) |
---|
43 | { |
---|
44 | return PHPMailer::Send(); |
---|
45 | } |
---|
46 | $this->CharSet = $GLOBALS['phpgw']->translation->charset(); |
---|
47 | list($lang,$nation) = explode('-',$GLOBALS['phpgw_info']['user']['preferences']['common']['lang']); |
---|
48 | $lang_path = PHPGW_SERVER_ROOT.'/phpgwapi/setup/'; |
---|
49 | if ($nation && file_exists($lang_path."phpmailer.lang-$nation.php")) // atm. only for pt-br => br |
---|
50 | { |
---|
51 | $lang = $nation; |
---|
52 | } |
---|
53 | $this->SetLanguage($lang,$lang_path); |
---|
54 | |
---|
55 | $this->IsSmtp(); |
---|
56 | $this->Host = $GLOBALS['phpgw_info']['server']['smtp_server']?$GLOBALS['phpgw_info']['server']['smtp_server']:'localhost'; |
---|
57 | $this->Port = $GLOBALS['phpgw_info']['server']['smtp_port']?$GLOBALS['phpgw_info']['server']['smtp_port']:25; |
---|
58 | $this->SMTPAuth = !empty($GLOBALS['phpgw_info']['server']['smtp_auth_user']); |
---|
59 | $this->Username = $GLOBALS['phpgw_info']['server']['smtp_auth_user']; |
---|
60 | $this->Password = $GLOBALS['phpgw_info']['server']['smtp_auth_passwd']; |
---|
61 | |
---|
62 | $this->Hostname = $GLOBALS['phpgw_info']['server']['hostname']; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Reset all Settings to send multiple Messages |
---|
67 | */ |
---|
68 | function ClearAll() |
---|
69 | { |
---|
70 | $this->err = array(); |
---|
71 | |
---|
72 | $this->Subject = $this->Body = $this->AltBody = ''; |
---|
73 | $this->IsHTML(False); |
---|
74 | $this->ClearAllRecipients(); |
---|
75 | $this->ClearAttachments(); |
---|
76 | $this->ClearCustomHeaders(); |
---|
77 | |
---|
78 | $this->FromName = $GLOBALS['phpgw_info']['user']['fullname']; |
---|
79 | $this->From = $GLOBALS['phpgw_info']['user']['email']; |
---|
80 | $this->Sender = ''; |
---|
81 | |
---|
82 | $this->AddCustomHeader('X-Mailer:eGroupWare (http://www.eGroupWare.org)'); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Emulating the old send::msg interface for compatibility with existing code |
---|
87 | * |
---|
88 | * You can either use that code or the PHPMailer variables and methods direct. |
---|
89 | */ |
---|
90 | function msg($service, $to, $subject, $body, $msgtype='', $cc='', $bcc='', $from='', $sender='', $content_type='', $boundary='Message-Boundary') |
---|
91 | { |
---|
92 | //echo "<p>send::msg(,to='$to',subject='$subject',,'$msgtype',cc='$cc',bcc='$bcc',from='$from',sender='$sender','$content_type','$boundary')<pre>$body</pre>\n"; |
---|
93 | $this->ClearAll(); // reset everything to its default, we might be called more then once !!! |
---|
94 | |
---|
95 | if ($service != 'email') |
---|
96 | { |
---|
97 | return False; |
---|
98 | } |
---|
99 | if ($from) |
---|
100 | { |
---|
101 | if (preg_match('/"?(.+)"?<(.+)>/',$from,$matches)) |
---|
102 | { |
---|
103 | list(,$this->FromName,$this->From) = $matches; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | $this->From = $from; |
---|
108 | $this->FromName = ''; |
---|
109 | } |
---|
110 | } |
---|
111 | if ($sender) |
---|
112 | { |
---|
113 | $this->Sender = $sender; |
---|
114 | } |
---|
115 | foreach(array('to','cc','bcc') as $adr) |
---|
116 | { |
---|
117 | if ($$adr) |
---|
118 | { |
---|
119 | if (preg_match_all('/"?(.+)"?<(.+)>,?/',$$adr,$matches)) |
---|
120 | { |
---|
121 | $names = $matches[1]; |
---|
122 | $addresses = $matches[2]; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | $addresses = split('[, ]',$$adr); |
---|
127 | $names = array(); |
---|
128 | } |
---|
129 | $method = 'Add'.($adr == 'to' ? 'Address' : $adr); |
---|
130 | |
---|
131 | foreach($addresses as $n => $address) |
---|
132 | { |
---|
133 | $this->$method($address,$names[$n]); |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | if (!empty($msgtype)) |
---|
138 | { |
---|
139 | $this->AddCustomHeader('X-eGW-Type: '.$msgtype); |
---|
140 | } |
---|
141 | if ($content_type) |
---|
142 | { |
---|
143 | $this->ContentType = $content_type; |
---|
144 | } |
---|
145 | $this->Subject = $subject; |
---|
146 | $this->Body = $body; |
---|
147 | |
---|
148 | //echo "PHPMailer = <pre>".print_r($this,True)."</pre>\n"; |
---|
149 | if (!$this->Send()) |
---|
150 | { |
---|
151 | $this->err = array( |
---|
152 | 'code' => 1, // we dont get a numerical code from PHPMailer |
---|
153 | 'msg' => $this->ErrorInfo, |
---|
154 | 'desc' => $this->ErrorInfo, |
---|
155 | ); |
---|
156 | return False; |
---|
157 | } |
---|
158 | return True; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * encode 8-bit chars in subject-line |
---|
163 | * |
---|
164 | * This is not needed any more, as it is done be PHPMailer, but older code depend on it. |
---|
165 | */ |
---|
166 | function encode_subject($subject) |
---|
167 | { |
---|
168 | return $subject; |
---|
169 | } |
---|
170 | } |
---|