Difference between revisions of "Smtp client"
Jump to navigation
Jump to search
Line 34: | Line 34: | ||
import smtplib, ssl | import smtplib, ssl | ||
from email.mime.text import MIMEText | from email.mime.text import MIMEText | ||
+ | from email import utils | ||
SMTP_HOST = os.environ.get('SMTP_HOST') | SMTP_HOST = os.environ.get('SMTP_HOST') | ||
Line 52: | Line 53: | ||
msg['From'] = SMTP_FROM_EMAIL | msg['From'] = SMTP_FROM_EMAIL | ||
msg['To'] = SMTP_TO_EMAILS | msg['To'] = SMTP_TO_EMAILS | ||
− | msg = msg.as_string() | + | msg['Date'] = utils.formatdate(localtime = 1) |
+ | msg['Message-ID'] = utils.make_msgid() | ||
+ | msg_as_string = msg.as_string() | ||
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as session: # 587 | with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as session: # 587 | ||
Line 63: | Line 66: | ||
if SMTP_AUTH == "true": | if SMTP_AUTH == "true": | ||
session.login(SMTP_AUTH_USERNAME, SMTP_AUTH_USERPASS) | session.login(SMTP_AUTH_USERNAME, SMTP_AUTH_USERPASS) | ||
− | session.sendmail(SMTP_FROM_EMAIL, SMTP_TO_EMAILS, | + | session.sendmail(SMTP_FROM_EMAIL, SMTP_TO_EMAILS, msg_as_string) |
− | session.quit() | + | session.close() |
+ | # session.quit() | ||
Revision as of 04:00, 10 May 2022
Server
Gen cert
openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout server.key -out server.crt -subj "/C=US/ST=Utah/L=SLC/O=Example Corp/OU=Testing/CN=relay.uvoo.io"
Run docker
docker run --rm -p 1587:587 --name postfix -e MYNETWORKS="127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16" -e SMTPD_USERS="valid:password,user1:password1,user2:password2" -e MAILNAME=relay.uvoo.com -v ${PWD}/server.crt:/etc/ssl/certs/server.crt:ro -v ${PWD}/server.key:/etc/ssl/private/server.key:ro -e TLS_KEY="/etc/ssl/private/server.key" -e TLS_CRT="/etc/ssl/certs/server.crt" panubo/postfix:latest
Client
.env
export SMTP_HOST=localhost export SMTP_PORT=1587 export SMTP_FROM_EMAIL=no-reply@uvoo.io export SMTP_TO_EMAILS=test@uvoo.io export SMTP_MSG_SUBJECT="test subject" export SMTP_MSG_BODY="test body" export SMTP_DEBUG=true export SMTP_AUTH=true export SMTP_AUTH_USERNAME=valid export SMTP_AUTH_USERPASS=password
./smtp-client.py
#!/usr/bin/env python3 import os import smtplib, ssl from email.mime.text import MIMEText from email import utils SMTP_HOST = os.environ.get('SMTP_HOST') SMTP_PORT = os.environ.get('SMTP_PORT') SMTP_FROM_EMAIL = os.environ.get('SMTP_FROM_EMAIL') SMTP_TO_EMAILS = os.environ.get('SMTP_TO_EMAILS') SMTP_MSG_SUBJECT = os.environ.get('SMTP_MSG_SUBJECT') SMTP_MSG_BODY = os.environ.get('SMTP_MSG_BODY') SMTP_AUTH = os.environ.get('SMTP_AUTH') SMTP_AUTH_USERNAME = os.environ.get('SMTP_AUTH_USERNAME') SMTP_AUTH_USERPASS = os.environ.get('SMTP_AUTH_USERPASS') SMTP_DEBUG = os.environ.get('SMTP_DEBUG') def send_email(): msg = MIMEText(SMTP_MSG_BODY) msg['Subject'] = SMTP_MSG_SUBJECT msg['From'] = SMTP_FROM_EMAIL msg['To'] = SMTP_TO_EMAILS msg['Date'] = utils.formatdate(localtime = 1) msg['Message-ID'] = utils.make_msgid() msg_as_string = msg.as_string() with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as session: # 587 if SMTP_DEBUG == "true": session.set_debuglevel(True) session.ehlo() # context = ssl.create_default_context() context = ssl._create_unverified_context() session.starttls(context=context) if SMTP_AUTH == "true": session.login(SMTP_AUTH_USERNAME, SMTP_AUTH_USERPASS) session.sendmail(SMTP_FROM_EMAIL, SMTP_TO_EMAILS, msg_as_string) session.close() # session.quit() send_email()
Run
. .env ./smtp-client.py