Skip to content

Commit

Permalink
Change default setting for server to support all incoming mail.
Browse files Browse the repository at this point in the history
Close #2
  • Loading branch information
denself committed Sep 8, 2017
1 parent 94e09f7 commit 8e0eb03
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ pip install -r requirements.txt

## Running server

Server support two optional settings, that cen be changed using environment variables:
Server support two optional settings, that cen be changed using environment
variables:

`SMTP_HOST` defines host, on which server accepts connections. `*` for all.

`SMTP_PORT` defines servers port.
Keep in mind, that if you setting `SMTP_HOST=::1`, that you should setup DNS
record that supports IPv6.


`SMTP_PORT` defines servers port. Default ports for SMTP are 25, 2525, 587 and
465, 25, 587, 2526 for Secure SMTP

To start server just run:
```bash
./main.py
```


## DNS settings

Expand All @@ -46,7 +58,7 @@ mx.example.com A 1.2.3.4
- Another on `MX` record, that points to previous `A` record.
```
example.com MX mx.example.com
```
```

TODO: SPF/DKIM/DMARC records
https://www.namecheap.com/support/knowledgebase/article.aspx/317/2237/how-do-i-add-txtspfdkimdmarc-records-for-my-domain
Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
stream=sys.stdout
)

log = logging.getLogger()


if __name__ == '__main__':
host = os.getenv('SMTP_HOST', '::1')
port = os.getenv('SMTP_PORT', '8025')
host = os.getenv('SMTP_HOST', '*')
port = os.getenv('SMTP_PORT', '25')

loop = asyncio.get_event_loop()
controller = Controller(ExampleHandler(), hostname=host, port=port)
controller.start()

log.info("SMTP server started on %s:%s", host, port)

try:
loop.run_forever()
except KeyboardInterrupt:
Expand Down
20 changes: 13 additions & 7 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import logging

from aiosmtpd.smtp import Envelope, Session, SMTP


log = logging.getLogger("smtphandler")


class ExampleHandler:
async def handle_RCPT(self,
server: SMTP,
session: Session,
envelope: Envelope,
address: str,
rcpt_options: list):
if not address.endswith('@example.com'):
return '550 not relaying to that domain'
# Accept all incoming mail for now
# if not address.endswith('@example.com'):
# return '550 not relaying to that domain'
log.info("Handle RCPT for %s", address)
envelope.rcpt_tos.append(address)
return '250 OK'

async def handle_DATA(self,
server: SMTP,
session: Session,
envelop: Envelope):
content = envelop.content.decode("utf8", errors="replace")
print(f'Message from {envelop.mail_from}')
print(f'Message for {envelop.rcpt_tos}')
print(f'Message data:\n{content}')
print('End of message')
content = envelop.content
log.debug('Message from %s', envelop.mail_from)
log.debug('Message for %s', envelop.rcpt_tos)
log.debug('Message data:\n%s\n%s', content, '*'*20)
return '250 Message accepted for delivery'

0 comments on commit 8e0eb03

Please sign in to comment.