ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
EMail.php
Go to the documentation of this file.
1 <?php
2 
10 
11 
12  private $to = NULL;
13  private $cc = NULL;
14  private $body = NULL;
15  private $from = NULL;
16  private $replyto = NULL;
17  private $subject = NULL;
18  private $headers = array();
19 
20 
24  function __construct($to, $subject, $from = NULL, $cc = NULL, $replyto = NULL) {
25  $this->to = $to;
26  $this->cc = $cc;
27  $this->from = $from;
28  $this->replyto = $replyto;
29  $this->subject = $subject;
30  }
31 
32  function setBody($body) {
33  $this->body = $body;
34  }
35 
36  private function getHTML($body) {
37  return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
38  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
39 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
40 <head>
41  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
42  <title>SimpleSAMLphp Email report</title>
43  <style type="text/css">
44 pre, div.box {
45  margin: .4em 2em .4em 1em;
46  padding: 4px;
47 
48 }
49 pre {
50  background: #eee;
51  border: 1px solid #aaa;
52 }
53  </style>
54 </head>
55 <body>
56 <div class="container" style="background: #fafafa; border: 1px solid #eee; margin: 2em; padding: .6em;">
57 ' . $body . '
58 </div>
59 </body>
60 </html>';
61  }
62 
63  function send() {
64  if ($this->to == NULL) throw new Exception('EMail field [to] is required and not set.');
65  if ($this->subject == NULL) throw new Exception('EMail field [subject] is required and not set.');
66  if ($this->body == NULL) throw new Exception('EMail field [body] is required and not set.');
67 
68  $random_hash = bin2hex(openssl_random_pseudo_bytes(16));
69 
70  if (isset($this->from))
71  $this->headers[]= 'From: ' . $this->from;
72  if (isset($this->replyto))
73  $this->headers[]= 'Reply-To: ' . $this->replyto;
74 
75  $this->headers[] = 'Content-Type: multipart/alternative; boundary="simplesamlphp-' . $random_hash . '"';
76 
77  $message = '
78 --simplesamlphp-' . $random_hash . '
79 Content-Type: text/plain; charset="utf-8"
80 Content-Transfer-Encoding: 8bit
81 
82 ' . strip_tags(html_entity_decode($this->body)) . '
83 
84 --simplesamlphp-' . $random_hash . '
85 Content-Type: text/html; charset="utf-8"
86 Content-Transfer-Encoding: 8bit
87 
88 ' . $this->getHTML($this->body) . '
89 
90 --simplesamlphp-' . $random_hash . '--
91 ';
92  $headers = implode("\n", $this->headers);
93 
94  $mail_sent = @mail($this->to, $this->subject, $message, $headers);
95  SimpleSAML\Logger::debug('Email: Sending e-mail to [' . $this->to . '] : ' . ($mail_sent ? 'OK' : 'Failed'));
96  if (!$mail_sent) throw new Exception('Error when sending e-mail');
97  }
98 
99 }
File written to
__construct($to, $subject, $from=NULL, $cc=NULL, $replyto=NULL)
Constructor.
Definition: EMail.php:24
static debug($string)
Definition: Logger.php:213
mail($to, $subject, $message, $additional_headers=null, $additional_parameters=null)
catch(Exception $e) $message
Create styles array
The data for the language used.