Difference between revisions of "Smtp client"
Jump to navigation
Jump to search
(Created page with "# Server ``` docker run --rm -p 1587:587 --name postfix -e SMTPD_USERS="valid:password,user1:password1,user2:password2" -e MAILNAME=mail.example.com panubo/postfix:latest ```...") |
(No difference)
|
Revision as of 03:07, 10 May 2022
Server
docker run --rm -p 1587:587 --name postfix -e SMTPD_USERS="valid:password,user1:password1,user2:password2" -e MAILNAME=mail.example.com 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_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
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')
def send_email():
msg = MIMEText(SMTP_MSG_BODY)
msg['Subject'] = SMTP_MSG_SUBJECT
msg['From'] = SMTP_FROM_EMAIL
msg['To'] = SMTP_TO_EMAILS
msg = msg.as_string()
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as session: # 587
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)
session.quit()
send_email()
Run
. .env ./smpt-client.py