almost ready

This commit is contained in:
Chris 2023-11-11 16:41:14 +01:00
parent 2bfe9f297c
commit d79091faac
12 changed files with 131 additions and 35 deletions

View file

@ -4,27 +4,14 @@ server {
set $base /var/www/opentrashmail;
root /var/www/opentrashmail/web/;
index index.html;
index index.php;
client_max_body_size 10M;
location / {
try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php;
}
location /rss {
index rss.php;
if (!-e $request_filename){
rewrite ^(.*)$ /rss.php?url=$1 last;
}
}
location /api {
index api.php;
if (!-e $request_filename){
rewrite ^(.*)$ /api.php?url=$1 last;
}
}
# logging
access_log /var/log/nginx/opentrashmail/web.access.log;

View file

@ -28,6 +28,13 @@ else
echo "DOMAINS=localhost" >> /var/www/opentrashmail/config.ini
fi
if [ "$URL" != "" ]; then
echo "URL=$URL" >> /var/www/opentrashmail/config.ini
echo " [i] URL of GUI is set to: $URL"
else
echo "URL=http://localhost:8080" >> /var/www/opentrashmail/config.ini
fi
if [ "$SHOW_ACCOUNT_LIST" != "" ]; then
echo "SHOW_ACCOUNT_LIST=$SHOW_ACCOUNT_LIST" >> /var/www/opentrashmail/config.ini
echo " [i] Set show account list to: $SHOW_ACCOUNT_LIST"

19
docs/Dev.md Normal file
View file

@ -0,0 +1,19 @@
# Quick testing
From the main directory run
```bash
docker build -f docker/Dockerfile -t opentrashmail . && docker run --rm -it --name trashmail -p 3000:80 -p 2525:25 opentrashmail
```
And check if it works on http://localhost:3000
## Sending debug emails from the command line
Using the text file `tools/testmail.txt` and the following line of bash you can send emails to your server and test if it's acceping emails like you want.
Note that if you change cour config.ini, the mail server needs to be restarted before it takes effect.
```bash
cat "tools/testmail.txt" | while read L; do sleep "0.2"; echo "$L"; done | "nc" -C -v "localhost" "2525"
```

View file

@ -1,9 +0,0 @@
# Quick testing
From the `docker` directory run
```bash
docker build -t opentrashmail . && docker run --rm -it --name trashmail -p 3000:80 -p 2525:25 opentrashmail
```
And check if it works on http://localhost:3000

2
python/mailserver.py Normal file → Executable file
View file

@ -171,6 +171,8 @@ if __name__ == '__main__':
DELETE_OLDER_THAN_DAYS = (Config.get("CLEANUP","DELETE_OLDER_THAN_DAYS").lower() == "true")
print "[i] Starting Mailserver on port",port
print "[i] Discard unknown domains:",DISCARD_UNKNOWN
print "[i] Listening for domains:",DOMAINS
server = CustomSMTPServer(('0.0.0.0', port), None) # use your public IP here
print "[i] Ready to receive Emails"

14
tools/testmail.txt Normal file
View file

@ -0,0 +1,14 @@
HELO localhost
MAIL FROM:<system@example.com>
RCPT TO:<random.user@rand.domain.tld>
DATA
Subject: Test Message
Hi there! This is supposed to be an email...
Have a good day!
-- System
.
QUIT

View file

@ -57,6 +57,41 @@ class OpenTrashmailBackend{
]);
}
//json api
else if($this->url[0]=='json')
{
header("Content-Type: application/json; charset=UTF8");
if($this->url[1]=='listaccounts')
{
if($this->settings['SHOW_ACCOUNT_LIST'])
return json_encode(listEmailAdresses());
else exit(json_encode(['error'=>'403 Forbidden']));
}
$email = $this->url[1];
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(404);
exit(json_encode(['error'=>'Email not found']));
}
$id = $this->url[2];
if($id) //user wants a specific email ID
{
if(!emailIDExists($email,$id))
{
http_response_code(404);
exit(json_encode(['error'=>'Email ID not found']));
}
else if(!ctype_digit($id))
{
http_response_code(400);
exit(json_encode(['error'=>'Invalid ID']));
}
else
return json_encode(getEmail($email,$id));
}
else
return json_encode(getEmailsOfEmail($email,true,true));
}
else return false;
}

View file

@ -38,7 +38,7 @@ function emailIDExists($email,$id)
return file_exists(getDirForEmail($email).DS.$id.'.json');
}
function getEmailsOfEmail($email)
function getEmailsOfEmail($email,$includebody=false,$includeattachments=false)
{
$o = [];
$settings = loadSettings();
@ -62,6 +62,15 @@ function getEmailsOfEmail($email)
'md5'=>md5($time.$json['raw']),
'maillen'=>strlen($json['raw'])
);
if($includebody==true)
$o[$time]['body'] = $json['parsed']['body'];
if($includeattachments==true)
{
$o[$time]['attachments'] = $json['parsed']['attachments'];
//add url to attachments
foreach($o[$time]['attachments'] as $k=>$v)
$o[$time]['attachments'][$k] = $settings['URL'].'/api/attachment/'.$email.'/'. $v;
}
}
}
closedir($handle);
@ -76,8 +85,23 @@ function getEmailsOfEmail($email)
if (endsWith($entry,'.json')) {
$time = substr($entry,0,-5);
$json = json_decode(file_get_contents(getDirForEmail($email).DS.$entry),true);
$o[$time] = array('email'=>$email,'id'=>$time,'from'=>$json['parsed']['from'],'subject'=>$json['parsed']['subject'],'md5'=>md5($time.$json['raw']),'maillen'=>strlen($json['raw']));
}
$o[$time] = array(
'email'=>$email,
'id'=>$time,
'from'=>$json['parsed']['from'],
'subject'=>$json['parsed']['subject'],
'md5'=>md5($time.$json['raw']),'maillen'=>strlen($json['raw'])
);
if($includebody==true)
$o[$time]['body'] = $json['parsed']['body'];
if($includeattachments==true)
{
$o[$time]['attachments'] = $json['parsed']['attachments'];
//add url to attachments
foreach($o[$time]['attachments'] as $k=>$v)
$o[$time]['attachments'][$k] = $settings['URL'].'/api/attachment/'.$email.'/'. $v;
}
}
}
closedir($handle);
}

View file

@ -12,7 +12,7 @@ $backend = new OpenTrashmailBackend($url);
if($_SERVER['HTTP_HX_REQUEST']!='true')
{
if(count($url)==0 || !file_exists(ROOT.DS.implode('/', $url)))
if($url[0]!='api' && $url[0]!='rss')
if($url[0]!='api' && $url[0]!='rss' && $url[0]!='json')
exit($backend->renderTemplate('index.html',[
'url'=>implode('/', $url),
'settings'=>loadSettings(),

View file

@ -1,3 +1,7 @@
<div>
<a role="button" class="outline" href="/json/listaccounts" target="_blank"><i class="fas fa-file-code"></i> JSON API</a>
</div>
<table>
<thead>
<tr>
@ -16,7 +20,8 @@
</td>
<td><?= countEmailsOfAddress($email); ?></td>
<td>
<input type="submit" value="Delete" hx-get="/api/deleteaccount/<?= $email ?>" hx-confirm="Are you sure to delete this account and all its emails?" hx-target="closest tr" hx-swap="outerHTML swap:1s">
<a href="/address/<?= $email; ?>" hx-get="/api/address/<?= $email; ?>" hx-push-url="/address/<?= $email; ?>" hx-target="#main" role="button" >Show</a>
<a href="#" role="button" hx-get="/api/deleteaccount/<?= $email ?>" hx-confirm="Are you sure to delete this account and all its emails?" hx-target="closest tr" hx-swap="outerHTML swap:1s">Delete</a>
</td>
</tr>
<?php endforeach; ?>

View file

@ -1,10 +1,17 @@
<nav aria-label="breadcrumb">
<ul>
<li><?= escape($email) ?></li>
<li><a href="/rss/<?= $email ?>">RSS Feed</a></li>
<li></li>
<li></li>
</ul>
</nav>
<div>
<a role="button" class="outline" href="#" id="copyemailbtn" onclick="copyEmailToClipboard();return false;"><i class="far fa-clipboard"></i> Copy address to clipboard</a>
<a role="button" class="outline" href="/rss/<?= $email ?>" target="_blank"><i class="fas fa-rss"></i> RSS Feed</a>
<a role="button" class="outline" href="/json/<?= $email ?>" target="_blank"><i class="fas fa-file-code"></i> JSON API</a>
</div>
<table role="grid">
<thead>
<tr>
@ -30,13 +37,17 @@
<td><?= escape($ed['from']) ?></td>
<td><?= escape($ed['subject']) ?></td>
<td>
<div class="grid">
<div><input type="submit" value="Read" hx-get="/api/read/<?= $email ?>/<?= $ed['id'] ?>" hx-push-url="/read/<?= $email ?>/<?= $ed['id'] ?>" hx-target="#main"></div>
<div><input type="submit" value="Delete" hx-get="/api/delete/<?= $email ?>/<?= $ed['id'] ?>" hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML swap:1s"></div>
</div>
<a href="/read/<?= $email ?>/<?= $ed['id'] ?>" hx-get="/api/read/<?= $email ?>/<?= $ed['id'] ?>" hx-push-url="/read/<?= $email ?>/<?= $ed['id'] ?>" hx-target="#main" role="button">Open</a>
<a href="#" hx-get="/api/delete/<?= $email ?>/<?= $ed['id'] ?>" hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML swap:1s" role="button">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<script>history.pushState({urlpath:"/address/<?= $email ?>"}, "", "/address/<?= $email ?>");</script>
<script>history.pushState({urlpath:"/address/<?= $email ?>"}, "", "/address/<?= $email ?>");</script>
<script>
function copyEmailToClipboard(){
navigator.clipboard.writeText("<?= $email ?>");
document.getElementById('copyemailbtn').innerHTML = '<i class="fas fa-check-circle" style="color: green;"></i> Copied!';
}
</script>

View file

@ -14,6 +14,7 @@
<?php foreach ($emaildata as $id => $d):
$data = getEmail($email, $id);
$time = substr($id, 0, -3);
$att_text = [];
if (is_array($data['parsed']['attachments']))
foreach ($data['parsed']['attachments'] as $filename) {
$filepath = ROOT . DS . '..' . DS . 'data' . DS . $email . DS . 'attachments' . DS . $filename;