Python email via sendmail with pdf attachment fails on any pdf file greater than 47k. Works correctly on files 47k and under. Why? Here is the code I am using. Also, is there any way to use a newer version of python in Ignition? It is really hard to research when most posts are for a newer version of python.
def SendPDFEmail(PDFPathAndFileName, PDFFileName, Subject, Message, EmailList):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
debug = 5
# prepare semicolon delimited email list for sendmail command
recipients = [email.strip() for email in EmailList.split(";") if email.strip()]
if debug >= 5: print "SendPDFEmail - Recipients:", recipients
if not recipients:
if debug >= 0: print("SendPDFEmail - No valid recipients found.")
WriteToQCAuditFile("N/A", PDFFilename, "No email recipients - email not sent")
return 'Error'
msg = MIMEMultipart()
msg.attach(MIMEText(Message, "plain"))
msg['Subject'] = Subject
msg['From'] = "Info@pf-pv.com"
msg['To'] = ", ".join(recipients)
try:
with open(PDFPathAndFileName, "rb") as f:
pdfAttachment = MIMEApplication(f.read(), _subtype="pdf")
pdfAttachment.add_header('Content-Disposition', 'attachment', filename=PDFFileName)
msg.attach(pdfAttachment)
except IOError as e:
if debug >= 0: print("SendPDFEmail - Error reading PDF file:", e)
WriteToQCAuditFile("N/A", PDFFilename, "Error Reading PDF file - email not sent")
return 'Error'
try:
server = smtplib.SMTP('10.10.101.2', 25, timeout=10)
server.ehlo()
server.sendmail("Info@pf-pv.com", recipients, msg.as_string())
server.quit()
if debug>= 1: print("SendPDFEmail - Email sent successfully.")
return 'Success'
except smtplib.SMTPException as e:
if debug >= 0: print("SendPDFEmail - SMTP error occurred:", str(e))
WriteToQCAuditFile("N/A", PDFFilename, "SMTP error occured - email not sent")
return 'Error'
except Exception as e:
if debug >= 0: print("SendPDFEmail - Unexpected error:", str(e))
WriteToQCAuditFile("N/A", PDFFilename, "Unexpected error occured - email not sent")
return 'Error'