Send_mail syntax

Curently, i need to cat the file and print it in the body of the email.

Not sure. I have tried the below one. is this correct?

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    from email.utils import formatdate
    from_addr= 'test@gmail.com'
    to_addr= 'test@gmail.com'
    sub= 'test'
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subj

    msg_text = '\n'.join(body)
    msg.attach(MIMEText(msg_text))

    smtp = smtplib.SMTP('localhost')
    smtp.sendmail(from_addr,  to_addr.split(","), msg.as_string())
    smtp.close()



Did it work or is there an error? Please tell us.

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.

Exact code below

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

Cool. Compare your two posts and see if you can find the problem :slight_smile:

both the codes are throwing error.

not able to find it out.

Double-check, just like I said. Run the exact code you’re posting, and post the exact error. Do NOT shortcut.