Skip to content

Commit

Permalink
Added verbosity to SMTP send
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Oct 31, 2024
1 parent e0f6983 commit 7a4e5ab
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions email/1.3.0/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ def send_email_shuffle(self, apikey, recipients, subject, body):
headers = {"Authorization": "Bearer %s" % apikey}
return requests.post(url, headers=headers, json=data).text

def send_email_smtp(
self, smtp_host, recipient, subject, body, smtp_port, attachments="", username="", password="", ssl_verify="True", body_type="html", cc_emails=""
):
def send_email_smtp(self, smtp_host, recipient, subject, body, smtp_port, attachments="", username="", password="", ssl_verify="True", body_type="html", cc_emails=""):
self.logger.info("Sending email to %s with subject %s" % (recipient, subject))
if type(smtp_port) == str:
try:
smtp_port = int(smtp_port)
except ValueError:
return "SMTP port needs to be a number (Current: %s)" % smtp_port

self.logger.info("Pre SMTP setup")
try:
s = smtplib.SMTP(host=smtp_host, port=smtp_port)
except socket.gaierror as e:
Expand All @@ -95,6 +95,7 @@ def send_email_smtp(
else:
s.starttls()

self.logger.info("Pre SMTP auth")
if len(username) > 1 or len(password) > 1:
try:
s.login(username, password)
Expand All @@ -108,6 +109,7 @@ def send_email_smtp(
body_type = "html"

# setup the parameters of the message
self.logger.info("Pre mime multipart")
msg = MIMEMultipart()
msg["From"] = username
msg["To"] = recipient
Expand All @@ -116,10 +118,12 @@ def send_email_smtp(
if cc_emails != None and len(cc_emails) > 0:
msg["Cc"] = cc_emails

self.logger.info("Pre mime check")
msg.attach(MIMEText(body, body_type))

# Read the attachments
attachment_count = 0
self.logger.info("Pre attachments")
try:
if attachments != None and len(attachments) > 0:
print("Got attachments: %s" % attachments)
Expand Down Expand Up @@ -153,7 +157,7 @@ def send_email_smtp(
except Exception as e:
self.logger.info(f"Error in attachment parsing for email: {e}")


self.logger.info("Pre send msg")
try:
s.send_message(msg)
except smtplib.SMTPDataError as e:
Expand Down

0 comments on commit 7a4e5ab

Please sign in to comment.