Skip to content
Snippets Groups Projects
Commit e4d1ed98 authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg
Browse files

Prettified email error reports.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@914 44740490-163a-0410-bde0-09ae8108e29a
parent 5d901886
Branches
Tags
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<articleinfo> <articleinfo>
<date>2007-10-15</date> <date>2007-10-15</date>
<pubdate>Thu Aug 28 16:25:10 2008</pubdate> <pubdate>Wed Oct 8 15:18:15 2008</pubdate>
<author> <author>
<firstname>Andreas Åkre</firstname> <firstname>Andreas Åkre</firstname>
......
<?php
/**
* A minimalistic Emailer class. Creates and sends HTML emails.
*
* @author Andreas kre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
* @package simpleSAMLphp
* @version $Id$
*/
class SimpleSAML_XHTML_EMail {
private $to = NULL;
private $cc = NULL;
private $body = NULL;
private $from = NULL;
private $replyto = NULL;
private $subject = NULL;
private $headers = array();
/**
* Constructor
*/
function __construct($to, $subject, $from = NULL, $cc = NULL, $replyto = NULL) {
$this->to = $to;
$this->cc = $cc;
$this->from = $from;
$this->replyto = $replyto;
$this->subject = $subject;
}
function setBody($body) {
$this->body = $body;
}
private function getHTML($body) {
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>simpleSAMLphp Email report</title>
<style type="text/css">
pre, div.box {
margin: .4em 2em .4em 1em;
padding: 4px;
}
pre {
background: #eee;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<div class="container" style="background: #fafafa; border: 1px solid #eee; margin: 2em; padding: .6em;">
' . $body . '
</div>
</body>
</html>';
}
function send() {
if ($this->to == NULL) throw new Exception('EMail field [to] is required and not set.');
if ($this->subject == NULL) throw new Exception('EMail field [subject] is required and not set.');
if ($this->body == NULL) throw new Exception('EMail field [body] is required and not set.');
$random_hash = substr(0, 16, md5(date('r', time())));
if (isset($this->from))
$this->headers[]= 'From: ' . $this->from;
if (isset($this->replyto))
$this->headers[]= 'Reply-To: ' . $this->replyto;
$this->headers[] = 'Content-Type: multipart/alternative; boundary="simplesamlphp-' . $random_hash . '"';
$message = '
--simplesamlphp-' . $random_hash . '
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
' . strip_tags(html_entity_decode($this->body)) . '
--simplesamlphp-' . $random_hash . '
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit
' . $this->getHTML($this->body) . '
--simplesamlphp-' . $random_hash . '--
';
$headers = join("\r\n", $this->headers);
$mail_sent = @mail($this->to, $this->subject, $message, $headers);
if (!$mail_sent) throw new Exception('Error when sending e-mail');
}
}
?>
\ No newline at end of file
...@@ -17,6 +17,8 @@ if($_SERVER['REQUEST_METHOD'] !== 'POST') { ...@@ -17,6 +17,8 @@ if($_SERVER['REQUEST_METHOD'] !== 'POST') {
/* Format of the email. /* Format of the email.
* POST fields will be added to the email in the order they appear here, and with the description * POST fields will be added to the email in the order they appear here, and with the description
* from the value in the array. * from the value in the array.
*
* DEPRECATED. Included as reference of incomming parameters.
*/ */
$mailFormat = array( $mailFormat = array(
'email' => 'Email address of submitter', 'email' => 'Email address of submitter',
...@@ -41,87 +43,67 @@ $ignoredFields = array( ...@@ -41,87 +43,67 @@ $ignoredFields = array(
$reportId = SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(4)); $reportId = SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(4));
SimpleSAML_Logger::error('Error report with id ' . $reportId . ' generated.'); SimpleSAML_Logger::error('Error report with id ' . $reportId . ' generated.');
/* Build the email message. */
$message = '';
/** function getPValue($key) {
* Format and add a section to the email message. if (array_key_exists($key, $_POST)) {
* return strip_tags($_POST[$key]);
* @param $title The title of the section.
* @param $content The content of the section.
*/
function addMessageSection($title, $content) {
global $message;
$message .= $title . "\n";
$message .= "===============================================================\n";
foreach(split("\n", $content) as $line) {
$message .= ' ' . $line . "\n";
} }
return 'not set';
$message .= "\n";
} }
/* Add the default fields to the message. */ /* Build the email message. */
foreach($mailFormat as $key => $desc) {
if(!array_key_exists($key, $_POST)) {
/* Not included in the POST message, skip. */
continue;
}
$data = $_POST[$key]; $message = '<h1>SimpleSAMLphp Error Report</h1>
addMessageSection($desc, $data); <p>Message from user:</p>
} <div class="box" style="background: yellow; color: #888; border: 1px solid #999900; padding: .4em; margin: .5em">' . getPValue('text') . '</div>
/* Add any unknown fields to the message. */ <p>Exception: <strong>' . getPValue('exceptionmsg') . '</strong></p>
foreach($_POST as $key => $data) { <pre>' . getPValue('exceptiontrace') . '</pre>
/* Skip known fields. */ <p>URL:</p>
if(array_key_exists($key, $mailFormat)) { <pre><a href="' . getPValue('url') . '">' . getPValue('url') . '</a></pre>
continue;
}
/* Skip ignored fields. */ <p>Directory:</p>
if(in_array($key, $ignoredFields, TRUE)) { <pre>' . dirname(dirname(__FILE__)) . '</pre>
continue;
}
$title = 'Unknown field: ' . $key; <p>Track ID:</p>
addMessageSection($title, $data); <pre>' . getPValue('trackid') . '</pre>
}
<p>Version: <tt>' . getPValue('version') . '</tt></p>
/* Add footer to message. */ <p>Report ID: <tt>' . $reportId . '</tt></p>
$message .= 'Error report id: ' . $reportId . "\n";
$message .= "You may search the logs for this id to find the location\n"; <hr />
$message .= "where this report was sent.\n"; <div class="footer">This message was sent using simpleSAMLphp. Visit <a href="http://rnd.feide.no/simplesamlphp">simpleSAMLphp homepage</a>.</div>
';
/* We want to use UTF-8 encoding of the email message. */
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/plain; charset="UTF-8"' . "\r\n";
/* Add the email address of the submitter as the Reply-To address. */ /* Add the email address of the submitter as the Reply-To address. */
$replyto = NULL;
$from = 'no-reply@simplesamlphp.org';
if(array_key_exists('email', $_POST)) { if(array_key_exists('email', $_POST)) {
$email = $_POST['email']; $email = $_POST['email'];
$email = trim($email); $email = trim($email);
/* Check that it looks like a valid email address. */ /* Check that it looks like a valid email address. */
if(!preg_match('/\s/', $email) && strpos($email, '@') !== FALSE) { if(!preg_match('/\s/', $email) && strpos($email, '@') !== FALSE) {
$headers .= 'Reply-To: ' . $email . "\r\n"; $replyto = $email;
$from = $email;
} }
} }
/* Send the email. */ /* Send the email. */
$email = $config->getValue('technicalcontact_email', 'na@example.org'); $toaddress = $config->getValue('technicalcontact_email', 'na@example.org');
if($email !== 'na@example.org') { if($email !== 'na@example.org') {
/* This should always be TRUE, as the error report button should not appear unless
* the email is set. $email = new SimpleSAML_XHTML_EMail($email, 'simpleSAMLphp error report', $from);
*/ $email->setBody($message);
mail($email, 'simpleSAMLphp error report', $message, $headers); $email->send();
} }
/* Redirect the user back to this page to clear the POST request. */ /* Redirect the user back to this page to clear the POST request. */
SimpleSAML_Utilities::redirect(SimpleSAML_Utilities::selfURLNoQuery()); SimpleSAML_Utilities::redirect(SimpleSAML_Utilities::selfURLNoQuery());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment