Skip to content

Commit

Permalink
Create hyperlinks for URLs in plaintext emails
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Jan 31, 2016
1 parent fc52cb6 commit 22a6426
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion assets/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ mailhogApp.controller('MailCtrl', function ($scope, $http, $sce, $timeout) {

return content;
}

$scope.formatMessagePlain = function(message) {
var body = $scope.getMessagePlain(message);
var escaped = $scope.escapeHtml(body);
var formatted = escaped.replace(/(https?:\/\/)([-[\]A-Za-z0-9._~:/?#@!$()*+,;=%]|&amp;|&#39;)+/g, '<a href="$&" target="_blank">$&</a>');
return $sce.trustAsHtml(formatted);
}

$scope.escapeHtml = function(html) {
var entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
};
return html.replace(/[&<>"']/g, function (s) {
return entityMap[s];
});
}

$scope.getMessagePlain = function(message) {
if (message.Content.Headers && message.Content.Headers["Content-Type"] && message.Content.Headers["Content-Type"][0].match("text/plain")) {
Expand All @@ -414,7 +434,7 @@ mailhogApp.controller('MailCtrl', function ($scope, $http, $sce, $timeout) {
return $scope.tryDecode(l);
}
return message.Content.Body;
}
}

$scope.findMatchingMIME = function(part, mime) {
// TODO cache results
Expand Down
2 changes: 1 addition & 1 deletion assets/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@

<div class="tab-content">
<iframe target-blank="" ng-if="hasHTML(preview)" ng-class="{ active: hasHTML(preview) }" class="tab-pane" id="preview-html" srcdoc="{{preview.previewHTML}}" seamless frameborder="0" style="width: 100%"></iframe>
<div class="tab-pane" ng-class="{ active: !hasHTML(preview) }" id="preview-plain">{{ getMessagePlain(preview) }}</div>
<div class="tab-pane" ng-class="{ active: !hasHTML(preview) }" id="preview-plain" ng-bind-html="formatMessagePlain(preview)"></div>
<div class="tab-pane" id="preview-source">{{ getSource(preview) }}</div>
<div class="tab-pane" id="preview-mime">
<div ng-repeat="part in preview.MIME.Parts" class="mime-part">
Expand Down

0 comments on commit 22a6426

Please sign in to comment.