I am able to run the same syntax in one server it is working fine. whereas in some other server it is not working.
import smtplib
import os
import re
import sys
SERVER1 = "localhost"
localdomain="worldwide.com"
TO=["test@wide.com"]
CC=["test@wide.com"]
FROM="test@wide.com"
SUBJECT = "extra "
TEXT=""
email_file='test.txt'
fp = open(email_file,"r")
TEXT_LIST = fp.readlines()
fp.close()
for line in TEXT_LIST:
TEXT=TEXT+"\n"+line
footer_msg=" "
header= """
Below list "
"""
footer = """
Note: This is an automated mail please do not reply to this.In case of any queries please contact
"""
TEXT = header + TEXT + footer + footer_msg
# Prepare actual message
message = """\
From: %s
To: %s
Cc: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO),", ".join(CC), SUBJECT, TEXT)
print(message)
# Send the mail
'''
server = smtplib.SMTP(SERVER1)
server.sendmail(FROM, TO, message)
server.set_debuglevel(2)
server.quit()
'''
try:
smtpObj = smtplib.SMTP(SERVER1)
smtpObj.sendmail(FROM, TO, message)
smtpObj.set_debuglevel(2)
print("Successfully sent email")
except smtplib.SMTPException:
print("Error: unable to send email")
Traceback (most recent call last):
File "send_mail.py", line 59, in <module>
except SMTPException:
NameError: name 'SMTPException' is not defined
Getting the above error. Any idea why it is causing in one server. it is working fine in other server.
Is that error really coming from the code you posted? Please double-check. Take the exact code you’re posting, run it, and copy and paste the exact error.
import smtplib
import os
import re
import sys
SERVER1 = "localhost"
localdomain="worldwide.com"
TO=["test@mail.com"]
CC=["test@mail.com"]
FROM="test@mail.com"
SUBJECT = "extra"
TEXT=""
email_file='output1.txt'
fp = open(email_file,"r")
TEXT_LIST = fp.readlines()
fp.close()
for line in TEXT_LIST:
TEXT=TEXT+"\n"+line
footer_msg=" "
header= """
Below list "
"""
footer = """
Note: This is an automated mail please do not reply to this.In case of any queries please contact.
"""
TEXT = header + TEXT + footer + footer_msg
# Prepare actual message
message = """\
From: %s
To: %s
Cc: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO),", ".join(CC), SUBJECT, TEXT)
print(message)
# Send the mail
'''
server = smtplib.SMTP(SERVER1)
server.sendmail(FROM, TO, message)
server.set_debuglevel(2)
server.quit()
'''
try:
smtpObj = smtplib.SMTP(SERVER1)
smtpObj.sendmail(FROM, TO, message)
smtpObj.set_debuglevel(2)
print("Successfully sent email")
except SMTPException:
print("Error: unable to send email")
Traceback (most recent call last):
File "send_mail.py", line 59, in <module>
except SMTPException:
NameError: name 'SMTPException' is not defined