1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00
Files
go-chasquid-smtp/test/util/smtpc.py
Alberto Bertogli 4ecc5461d3 Add driusan/dkim integration example and tests
This patch adds DKIM signing using https://github.com/driusan/dkim tools
to the example hook.

It also adds an optional integration test to exercise signing and
verification, and corresponding documentation.
2018-11-30 10:03:48 +00:00

44 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Simple SMTP client for testing purposes.
import argparse
import email.parser
import email.policy
import re
import smtplib
import sys
ap = argparse.ArgumentParser()
ap.add_argument("--server", help="SMTP server to connect to")
ap.add_argument("--user", help="Username to use in SMTP AUTH")
ap.add_argument("--password", help="Password to use in SMTP AUTH")
args = ap.parse_args()
# Parse the email using the "default" policy, which is not really the default.
# If unspecified, compat32 is used, which does not support UTF8.
rawmsg = sys.stdin.buffer.read()
msg = email.parser.Parser(policy=email.policy.default).parsestr(
rawmsg.decode('utf8'))
s = smtplib.SMTP(args.server)
s.starttls()
if args.user:
s.login(args.user, args.password)
# Send the raw message, not parsed, because the parser does not handle some
# corner cases that well (for example, DKIM-Signature headers get mime-encoded
# incorrectly).
# Replace \n with \r\n, which is normally done by the library, but will not do
# it in this case because we are giving it bytes and not a string (which we
# cannot do because it tries to incorrectly escape the headers).
crlfmsg = re.sub(br'(?:\r\n|\n|\r(?!\n))', b"\r\n", rawmsg)
s.sendmail(
from_addr=msg['from'], to_addrs=msg.get_all('to'),
msg=crlfmsg,
mail_options=['SMTPUTF8'])
s.quit()