Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
sendmail.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Send document mail with SMTP protocol
8  *
9  * @author Anakeen
10  * @version $Id: sendmail.php,v 1.4 2007/10/10 16:15:35 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 /**
17  * Send mail via smtp server
18  * @param string $to mail addresses (, separate)
19  * @param string $cc mail addresses (, separate)
20  * @param string $bcc mail addresses (, separate)
21  * @param string $from mail address
22  * @param string $subject mail subject
23  * @param Mail_mime|\Dcp\Mail\Message &$mimemail mail mime object
24  * @return string error message : if no error: empty if no error
25  */
26 function sendmail($to, $from, $cc, $bcc, $subject, &$mimemail, $multipart = null)
27 {
28  if (is_a($mimemail, \Dcp\Mail\Message::className)) {
29  return __sendmail_Dcp_Mail_Message($to, $from, $cc, $bcc, $subject, $mimemail);
30  }
31 
32  require_once 'PEAR.php';
33 
34  include_once ('Mail/mime.php');
35  include_once ('Net/SMTP.php');
36  $rcpt = array_merge(explode(',', $to) , explode(',', $cc) , explode(',', $bcc));
37 
38  $host = getParam('SMTP_HOST', 'localhost');
39  $port = getParam('SMTP_PORT', 25);
40  $login = getParam('SMTP_LOGIN');
41  $password = getParam('SMTP_PASSWORD');
42 
43  if (is_a($mimemail, 'Mail_Mime')) {
44  $mimemail->setFrom($from);
45  if ($cc != '') $mimemail->addCc($cc);
46  }
47 
48  $xh['To'] = $to;
49  /* Create a new Net_SMTP object. */
50  if (!($smtp = new Net_SMTP($host, $port))) {
51  die("Unable to instantiate Net_SMTP object\n");
52  }
53  $smtp->setDebug(false);
54  /* Connect to the SMTP server. */
55  if (PEAR::isError($e = $smtp->connect())) {
56  return ("smtp connect:" . $e->getMessage());
57  }
58 
59  if ($login) {
60  if (PEAR::isError($e = $smtp->auth($login, $password))) {
61  return ("smtp login:" . $e->getMessage());
62  }
63  }
64  /* Send the 'MAIL FROM:' SMTP command. */
65  $smtp_from = $from;
66  if (preg_match('/<(?<from>[^>]*)>/', $from, $reg)) {
67  $smtp_from = $reg['from'];
68  }
69  if (PEAR::isError($smtp->mailFrom($smtp_from))) {
70  return ("Unable to set sender to <$smtp_from>");
71  }
72  /* Address the message to each of the recipients. */
73  foreach ($rcpt as $v) {
74  $v = trim($v);
75  if ($v) {
76  if (preg_match("/<([^>]*)>/", $v, $reg)) {
77  $v = $reg[1];
78  }
79  if (PEAR::isError($res = $smtp->rcptTo($v))) {
80  return ("Unable to add recipient <$v>: " . $res->getMessage());
81  }
82  }
83  }
84  setlocale(LC_TIME, 'C');
85 
86  $data = '';
87 
88  if (is_a($mimemail, 'Fdl_Mail_mimePart')) {
89 
90  $mm = new Mail_Mime();
91  $mm->_build_params['head_charset'] = 'UTF-8';
92 
93  $mm->setFrom($from);
94  if ($cc != '') {
95  $mm->addCc($cc);
96  }
97  /*
98  * @var Fdl_Mail_mimePart $mimemail
99  */
100  $email = $mimemail->encode();
101  if (PEAR::isError($email)) {
102  $err = sprintf("Error encoding Fdl_Mail_mimePart : %s", $email->message);
103  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . $err);
104  return $err;
105  }
106 
107  $txtHeaders = $mm->txtHeaders(array_merge($email['headers'], array(
108  'To' => $to,
109  'Subject' => $subject,
110  'Date' => strftime("%a, %d %b %Y %H:%M:%S %z", time()) ,
111  'Message-Id' => sprintf("<%s@%s>", strftime("%Y%M%d%H%M%S-", time()) . rand(1, 65535) , $host) ,
112  'User-Agent' => sprintf("Dynacase Platform %s", getParam('VERSION'))
113  )));
114 
115  $data = $txtHeaders . $mm->_build_params['eol'] . $email['body'];
116  } else {
117 
118  $body = $mimemail->get();
119 
120  $xh['Date'] = strftime("%a, %d %b %Y %H:%M:%S %z", time());
121  // $xh['Content-type']= "multipart/related";
122  $xh['Subject'] = $subject;
123  $xh['Message-Id'] = '<' . strftime("%Y%M%d%H%M%S-", time()) . rand(1, 65535) . "@$host>";
124 
125  $xh['User-Agent'] = sprintf("Dynacase Platform %s", getParam('VERSION'));
126  $data = "";
127  $h = $mimemail->headers($xh);
128  if ($multipart) $h['Content-Type'] = str_replace("mixed", $multipart, $h['Content-Type']);
129 
130  foreach ($h as $k => $v) {
131  $data.= "$k: $v\r\n";
132  }
133 
134  $data.= "\r\n" . $body;
135  }
136  /* Set the body of the message. */
137  if (PEAR::isError($smtp->data($data))) {
138  return ("Unable to send data");
139  }
140  /* Disconnect from the SMTP server. */
141  $smtp->disconnect();
142  return '';
143 }
144 function __sendmail_Dcp_Mail_Message($to, $from, $cc, $bcc, $subject, \Dcp\Mail\Message & $message)
145 {
146  $message->setFrom($from);
147  $message->addTo($to);
148  $message->addCc($cc);
149  $message->addBcc($bcc);
150  $message->setSubject($subject);
151  return $message->send();
152 }
153 /**
154  * record message sent from freedom
155  */
156 function createSentMessage($to, $from, $cc, $bcc, $subject, &$mimemail, &$doc = null)
157 {
158  include_once ('WHAT/Lib.Common.php');
159  $err = '';
160  $msg = createDoc(getDbAccessFreedom() , "SENTMESSAGE", true);
161  if ($msg) {
162  /* Drop the display name if present, and keep only the mail address */
163  try {
164  $mailAddrParser = new \Dcp\Mail\MailAddrParser();
165  $res = $mailAddrParser->parse($from);
166  if (count($res) > 0) {
167  $from = $res[0]->address;
168  }
169  }
170  catch(\Dcp\Mail\MailAddrParserException $e) {
171  }
172  $msg->setValue("emsg_from", $from);
173  $msg->setValue("emsg_date", Doc::getTimeDate());
174  $msg->setValue("emsg_subject", $subject);
175  /*
176  * @var Doc $doc
177  */
178  if ($doc && $doc->id) {
179  $msg->setValue("emsg_refid", $doc->id);
180  $msg->profid = $doc->profid;
181  }
182  $trcp = array();
183  foreach (explode(',', $to) as $v) {
184  if ($v) $msg->addArrayRow("emsg_t_recipient", array(
185  "emsg_sendtype" => "to",
186  "emsg_recipient" => $v
187  ));
188  }
189  foreach (explode(',', $cc) as $v) {
190  if ($v) $msg->addArrayRow("emsg_t_recipient", array(
191  "emsg_sendtype" => "cc",
192  "emsg_recipient" => $v
193  ));
194  }
195  foreach (explode(',', $bcc) as $v) {
196  if ($v) $msg->addArrayRow("emsg_t_recipient", array(
197  "emsg_sendtype" => "bcc",
198  "emsg_recipient" => $v
199  ));
200  }
201 
202  if (is_a($mimemail, 'Fdl_Mail_mimePart')) {
203  // Flatten the MIME parts by expanding and removing the mutipart entities
204  $partList = array(&$mimemail
205  );
206  $i = 0;
207  while ($i < count($partList)) {
208  if (count($partList[$i]->_subparts) <= 0) {
209  $i++;
210  continue;
211  }
212  $multipart = $partList[$i];
213  array_splice($partList, $i, 1);
214  foreach ($multipart->_subparts as & $part) {
215  $partList[] = & $part;
216  }
217  unset($part);
218  }
219  // Search for a text/plain part and extract it
220  $textPart = null;
221  foreach ($partList as $i => & $part) {
222  if (preg_match("|^text/plain|", $part->_headers['Content-Type'])) {
223  $textPart = $part;
224  array_splice($partList, $i, 1);
225  break;
226  }
227  }
228  unset($part);
229  // Search for a text/html part and extract it
230  $htmlPart = null;
231  foreach ($partList as $i => & $part) {
232  if (preg_match("|^text/html|", $part->_headers['Content-Type'])) {
233  $htmlPart = $part;
234  array_splice($partList, $i, 1);
235  break;
236  }
237  }
238  unset($part);
239  // Store the text part
240  $textBody = '';
241  if ($textPart !== null) {
242  /*
243  * @var Mail_mimePart $textPart
244  */
245  if ($textPart->_body_file != '') {
246  $textBody = file_get_contents($textPart->_body_file);
247  } else {
248  $textBody = $textPart->_body;
249  }
250  $msg->setValue('emsg_textbody', $textBody);
251  }
252  // Store the HTML part
253  $htmlBody = '';
254  if ($htmlPart !== null) {
255  /*
256  * @var Mail_mimePart $htmlPart
257  */
258  if ($htmlPart->_body_file != '') {
259  $htmlBody = file_get_contents($htmlPart->_body_file);
260  } else {
261  $htmlBody = $htmlPart->_body;
262  }
263  $msg->setValue('emsg_htmlbody', $htmlBody);
264  }
265  // Store the remaining parts
266  foreach ($partList as $i => & $part) {
267  $tmpfile = tempnam(getTmpDir() , 'fdl_attach');
268  if ($part->_body_file != '') {
269  copy($part->_body_file, $tmpfile);
270  } else {
271  file_put_contents($tmpfile, $part->_body);
272  }
273  $msg->storeFile('emsg_attach', $tmpfile, $part->_filename, $i);
274  @unlink($tmpfile);
275  }
276  unset($part);
277 
278  $err = $msg->add();
279  if ($err != '') {
280  return $err;
281  }
282 
283  if ($htmlPart !== null && $htmlBody != '') {
284  // Re-link the HTML part CIDs
285  foreach ($partList as $i => & $part) {
286  $cid = preg_replace('/^<(.+)>$/', '\1', isset($part->_headers['Content-ID']) ? $part->_headers['Content-ID'] : '');
287  if ($cid != '') {
288  $htmlBody = str_replace(sprintf("cid:%s", $cid) , $msg->getfileLink('emsg_attach', $i) , $htmlBody);
289  }
290  }
291  unset($part);
292 
293  $msg->disableEditControl();
294  $msg->setValue('emsg_htmlbody', $htmlBody);
295  $err = $msg->modify(true);
296  $msg->enableEditControl();
297 
298  if ($err != '') {
299  return $err;
300  }
301  }
302 
303  return '';
304  } elseif (is_a($mimemail, \Dcp\Mail\Message::className)) {
305  /**
306  * @var \Dcp\Mail\Message $mimemail
307  */
308  /**
309  * @var \Dcp\Mail\DataSource[] $partList
310  */
311  $partList = array();
312  if (isset($mimemail->body)) {
313  $partList[] = $mimemail->body;
314  }
315  if (count($mimemail->bodyRelated) > 0) {
316  foreach ($mimemail->bodyRelated as $part) {
317  $partList[] = $part;
318  }
319  }
320  if (isset($mimemail->altBody)) {
321  $partList[] = $mimemail->altBody;
322  }
323  if (count($mimemail->attachments) > 0) {
324  foreach ($mimemail->attachments as $part) {
325  $partList[] = $part;
326  }
327  }
328  /**
329  * @var \Dcp\Mail\DataSource $textPart
330  */
331  $textPart = null;
332  /**
333  * @var \Dcp\Mail\DataSource $htmlPart
334  */
335  $htmlPart = null;
336  /**
337  * @var \Dcp\Mail\DataSource[] $otherPartList
338  */
339  $otherPartList = array();
340  foreach ($partList as $i => $part) {
341  if (!isset($textPart) && isset($part) && $part->getMimeType() == 'text/plain') {
342  $textPart = $part;
343  } elseif (!isset($htmlPart) && isset($part) && $part->getMimeType() == 'text/html') {
344  $htmlPart = $part;
345  } else {
346  $otherPartList[] = $part;
347  }
348  }
349  /* Store text part */
350  if ($textPart !== null) {
351  $data = $textPart->getData();
352  $msg->setValue(\Dcp\AttributeIdentifiers\Sentmessage::emsg_textbody, $data);
353  }
354  /* Store html part */
355  if ($htmlPart !== null) {
356  $data = $htmlPart->getData();
357  $msg->setValue(\Dcp\AttributeIdentifiers\Sentmessage::emsg_htmlbody, $data);
358  }
359  /* Store remaining parts */
360  foreach ($otherPartList as $i => $part) {
361  $tmpfile = tempnam(getTmpDir() , 'Body_getFile');
362  if ($tmpfile === false) {
363  break;
364  }
365  if (file_put_contents($tmpfile, $part->getData()) === false) {
366  unlink($tmpfile);
367  break;
368  }
369  $msg->setFile(\Dcp\AttributeIdentifiers\Sentmessage::emsg_attach, $tmpfile, $part->getName() , $i);
370  unlink($tmpfile);
371  }
372 
373  $err = $msg->add();
374  if ($err != '') {
375  return $err;
376  }
377 
378  if ($htmlPart !== null) {
379  $htmlBody = $htmlPart->getData();
380  // Re-link the HTML part CIDs
381  foreach ($otherPartList as $i => $part) {
382  if (isset($part->cid)) {
383  $htmlBody = str_replace(sprintf("cid:%s", $part->cid) , $msg->getfileLink('emsg_attach', $i) , $htmlBody);
384  }
385  }
386 
387  $msg->disableEditControl();
388  $msg->setValue('emsg_htmlbody', $htmlBody);
389  $err = $msg->modify(true);
390  $msg->enableEditControl();
391 
392  if ($err != '') {
393  return $err;
394  }
395  }
396 
397  return '';
398  }
399 
400  $msg->setValue("emsg_textbody", $mimemail->_txtbody);
401  $msg->setValue("emsg_htmlbody", $mimemail->_htmlbody);
402  $linkedbody = $mimemail->_htmlbody;
403  foreach ($mimemail->_parts as $k => $v) {
404  $tmpfile = tempnam(getTmpDir() , 'fdl_attach');
405  file_put_contents($tmpfile, $v["body"]);
406  $msg->setFile("emsg_attach", $tmpfile, $v["name"], $k);
407  @unlink($tmpfile);
408  }
409 
410  $err = $msg->add();
411  // relink body
412  if ($err == "") {
413  $linkedbody = $mimemail->_htmlbody;
414  foreach ($mimemail->_parts as $k => $v) {
415  $linkedbody = str_replace("cid:" . $v["cid"], $msg->getFileLink("emsg_attach", $k) , $linkedbody);
416  }
417  $msg->disableEditControl();
418  $msg->setValue("emsg_htmlbody", $linkedbody);
419  $err = $msg->modify(true);
420  }
421  }
422  return $err;
423 }
$tmpfile
sendmail($to, $from, $cc, $bcc, $subject, &$mimemail, $multipart=null)
Definition: sendmail.php:26
$message
$to
$subject
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
$login
Definition: dav.php:40
$bcc
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
getDbAccessFreedom()
Definition: Lib.Common.php:378
$cc
$from
static getTimeDate($hourdelta=0, $second=false)
Definition: Class.Doc.php:8826
__sendmail_Dcp_Mail_Message($to, $from, $cc, $bcc, $subject,\Dcp\Mail\Message &$message)
Definition: sendmail.php:144
if($file) if($subject==""&&$file) if($subject=="") $err
$data
← centre documentaire © anakeen