Skip to content

Commit

Permalink
Add Message-ID header and do ehlo only when needed
Browse files Browse the repository at this point in the history
1. Why is this change necessary?
  - Message-ID is used by antispam engines to identify hosts, its absense might
    incur penalties
  - the second ehlo call is only necessary if we've started TLS
  • Loading branch information
PauloPhagula committed Dec 5, 2017
1 parent d172885 commit fd69228
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import os
import re
import smtplib
from email.utils import COMMASPACE, formatdate, formataddr
from email.utils import COMMASPACE, formatdate, formataddr, make_msgid

import six
import html2text
Expand Down Expand Up @@ -210,6 +210,7 @@ def send_mail(subject,
mail['Reply-To'] = formataddr(reply_to)

mail['Date'] = formatdate(localtime=True)
mail['Message-ID'] = make_msgid()
mail['Subject'] = subject
mail.preamble = subject

Expand All @@ -229,7 +230,7 @@ def send_mail(subject,
charset.add_charset('utf-8', charset.QP, charset.QP)

if message:
body.attach(MIMEText(message, 'plain', 'utf-8'))
body.attach(MIMEText(message, 'plain', 'utf-8'))

if html_message:
body.attach(MIMEText(html_message, 'html', 'utf-8'))
Expand Down Expand Up @@ -293,12 +294,12 @@ def send_mail(subject,
if debug:
mail_server.set_debuglevel(1)

mail_server.ehlo()
mail_server.ehlo() # identify ourselves, prompting server for supported features

if use_tls:
mail_server.starttls()
mail_server.ehlo() # re-identify ourselves over TLS connection

mail_server.ehlo()
mail_server.login(username, password)
mail_server.sendmail(subject, list(map(lambda x: x[1], all_destinations)), mail.as_string())
# Should be mailServer.quit(), but that crashes...
Expand Down

0 comments on commit fd69228

Please sign in to comment.