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