ont / slacker

Simple smtp email server which redirects emails to slack.
MIT License
34 stars 12 forks source link

How to use? #8

Open Sparticuz opened 4 years ago

Sparticuz commented 4 years ago

I've got the image up and running on docker, but do you have instructions on how to use it? What's the best program to install to take advantage of this smtp server? Thanks!

ont commented 4 years ago

You can use it as usual SMTP server if you program can send emails directly to SMTP server.

For sending mails from command line and for old programs that calls /usr/bin/sendmail you must install one of the MTA (mail transfer agent).

Any of sendmail, postfix or exim will provide /usr/bin/sendmail command. but if you only need to redirect all emails to the slacker, then just install ssmtp.

Then you can use config /etc/ssmtp/ssmtp.conf that looks like this:

root=postmaster
mailhub=localhost:8025
hostname=localhost.localdomain

And test configuration with sendmail -s "Test subject" test@test.com < test.txt

Sparticuz commented 4 years ago

Awesome, got it working with ssmtp! The two snags I had were figuring out the rules array in the yaml was required, so I created a catch all rule. And the second was figuring out slacks confusing interface for creating an API key. (And scopes)

On Tue, Jun 2, 2020, 12:35 PM ont notifications@github.com wrote:

You can use it as usual SMTP server if you program can send emails directly to SMTP server.

For sending mails from command line and for old programs that calls /usr/bin/sendmail you must install one of the MTA (mail transfer agent).

Any of sendmail, postfix or exim will provide /usr/bin/sendmail command. but if you only need to redirect all emails to the slacker, then just install ssmtp.

Then you can use config /etc/ssmtp/ssmtp.conf that looks like this:

root=postmaster mailhub=localhost:8025 hostname=localhost.localdomain

And test configuration with sendmail -s "Test subject" test@test.com < test.txt

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ont/slacker/issues/8#issuecomment-637668862, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABHQ6OVE3VSB6GPYJZS6UTRUUS37ANCNFSM4NQ23KOQ .

Sparticuz commented 4 years ago

Actually, a little help on the rule. How do I create a catch all rule? I want every single message sent through the server, regardless of where it comes from, to be pushed to slack. I tried changing the from to *@.* but it crashes

ont commented 4 years ago

@Sparticuz can you post error message of the crash and config?

Sparticuz commented 4 years ago

/usr/src/app/handler.py:19: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.,
/etc/slacker/config.yml,
  self.config = yaml.load(open(config)),
Traceback (most recent call last):,
  File "/usr/local/bin/aiosmtpd", line 11, in <module>,
    load_entry_point('aiosmtpd', 'console_scripts', 'aiosmtpd')(),
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/main.py", line 107, in main,
    parser, args = parseargs(args=args),
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/main.py", line 86, in parseargs,
    args.handler = handler_class(),
    self.config = yaml.load(open(config)),
  File "/usr/src/app/handler.py", line 19, in __init__,
  File "/usr/local/lib/python3.6/site-packages/yaml/__init__.py", line 114, in load,
    return loader.get_single_data(),
  File "/usr/local/lib/python3.6/site-packages/yaml/constructor.py", line 41, in get_single_data,
    node = self.get_single_node(),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 36, in get_single_node,
    document = self.compose_document(),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 55, in compose_document,
    node = self.compose_node(None, None),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 84, in compose_node,
    node = self.compose_mapping_node(anchor),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 133, in compose_mapping_node,
    item_value = self.compose_node(node, item_key),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 82, in compose_node,
    node = self.compose_sequence_node(anchor),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 111, in compose_sequence_node,
    node.value.append(self.compose_node(node, index)),
  File "/usr/local/lib/python3.6/site-packages/yaml/composer.py", line 64, in compose_node,
    if self.check_event(AliasEvent):,
  File "/usr/local/lib/python3.6/site-packages/yaml/parser.py", line 98, in check_event,
    self.current_event = self.state(),
  File "/usr/local/lib/python3.6/site-packages/yaml/parser.py", line 449, in parse_block_mapping_value,
    if not self.check_token(KeyToken, ValueToken, BlockEndToken):,
  File "/usr/local/lib/python3.6/site-packages/yaml/scanner.py", line 116, in check_token,
    self.fetch_more_tokens(),
  File "/usr/local/lib/python3.6/site-packages/yaml/scanner.py", line 227, in fetch_more_tokens,
    return self.fetch_alias(),
  File "/usr/local/lib/python3.6/site-packages/yaml/scanner.py", line 610, in fetch_alias,
    self.tokens.append(self.scan_anchor(AliasToken)),
  File "/usr/local/lib/python3.6/site-packages/yaml/scanner.py", line 924, in scan_anchor,
    % ch, self.get_mark()),
yaml.scanner.ScannerError: while scanning an alias,
  in "/etc/slacker/config.yml", line 10, column 11,
expected alphabetic or numeric character, but found '@',
  in "/etc/slacker/config.yml", line 10, column 12

I tried from: as well as `@*`

ont commented 4 years ago

Oh, you probably use this form in config:

rules:
    - name: Catch them all
      from: *@*

Yes, this will not work. Values in yaml must start with alphanumeric values. You must surround value with quotes: from: "*@*" or from: '*@*'. But this also will not work because rules must be correct regular expressions.

Correct "catch them all" rule must be:

rules:
    - name: "Catch them all"
      from: ".*"
Sparticuz commented 4 years ago

Great, It looks like I'm getting some messages. (Sorry it's taken so long, working on this on the side). Some of my messages are coming through odd though. What's this? [<email.message.Message object at 0x7f60d0a0da58>, <email.message.Message object at 0x7f60d0a0db00>]

ont commented 4 years ago

It is probably multipart email message with additional content. For example some email has two variants: text/plain and text/html. Currently slacker doesn't try to figure out which part of message to take nor try to convert html to plaintext.

hycday commented 2 years ago

Can you please provide a bit more details on how to make it work ?

I have a running container, and it is open and working

# docker port slacker
8025/tcp -> 0.0.0.0:8025
8025/tcp -> :::8025

I installed ssmtp, and configured it exactly as per above (which is : )

/etc/ssmtp/ssmtp.conf looks like this:

root=postmaster
mailhub=localhost:8025
hostname=localhost.localdomain

when I run sendmail -s "Test subject" test@test.com < test.txt, I get: sendmail: Server didn't like our AUTH LOGIN (500 Error: command "AUTH" not recognized)

Is there anything else I need to do ,

ont commented 2 years ago

@hycday you probably have AuthUser directive in your config: https://linux.die.net/man/5/ssmtp.conf

AuthUser The user name to use for SMTP AUTH. The default is blank, in which case SMTP AUTH is not used.

hycday commented 2 years ago

I indeed forgot to comment out that line, so now I only have the 3 lines from my previous post when I do :

sendmail user@example.com < test.txt

and test.txt has the following:


Subject: Test subject

Email Content line 1

I get

sendmail: 500 Error: (InvalidAuthError)

ont commented 2 years ago

Very strange. Do you have any logs from server in docker container?

hycday commented 2 years ago

this is what i have with docker logs slacker

/usr/src/app/handler.py:19: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  self.config = yaml.load(open(config))
ERROR:mail.log:SMTP session exception
Traceback (most recent call last):
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/smtp.py", line 315, in _handle_client
    await method(arg)
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/smtp.py", line 688, in smtp_DATA
    status = await self._call_handler_hook('DATA')
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/smtp.py", line 122, in _call_handler_hook
    status = await hook(self, self.session, self.envelope, *args)
  File "/usr/src/app/src/aiosmtpd/aiosmtpd/handlers.py", line 156, in handle_DATA
    self.handle_message(envelope)
  File "/usr/src/app/handler.py", line 28, in handle_message
    self.send_to_slack(self.extract_text(message), **options)
  File "/usr/src/app/handler.py", line 75, in send_to_slack
    icon_url=options['icon_url']
  File "/usr/local/lib/python3.6/site-packages/slack/chat.py", line 49, in post_message
    return slack.http_client.post('chat.postMessage', data)
  File "/usr/local/lib/python3.6/site-packages/slack/http_client.py", line 42, in post
    return _parse_response(response)
  File "/usr/local/lib/python3.6/site-packages/slack/http_client.py", line 49, in _parse_response
    _raise_error_if_not_ok(response)
  File "/usr/local/lib/python3.6/site-packages/slack/http_client.py", line 59, in _raise_error_if_not_ok
    raise InvalidAuthError()
slack.exception.InvalidAuthError

so i guess it comes from my config.yml... I am not sure i got the slack_token correctly can you provide a very easy guide to be sure i did it correctly ? (might be useful for others as well), and also, the username can be anything ?

hycday commented 2 years ago

Nevermind...

just did it all works perfectly well.

Once you create your slack workspace go to https://api.slack.com/ Create an app Add right OAuth permissions as needed (example: Bot Token Scopes should be chat:write and chat:write.public I guess) Once done with rest of config (if needed), go to "Install app" and add it to your slack workspace to generate the slack_token (it should start with xoxb followed by numbers and then by letters, like in the readme of this docker). And thats all :)