smtlib not send email to email address from column list of pandas dataframe

28 views Asked by At

Dears

I have a Code Connect to Oracle DB, get query as Daraframe and send email to receiver that exist in a column

As I Test When I set test_reciver argument to an email address test_reciver Email received but when I set (email Receiver) to the column of data frame, Email not received

#send mail function
def send_email(subject,to,cc,body):
    sender_email = '[email protected]'
    receivers = to
    host = "bulkmail.test.com"
    server = smtplib.SMTP(host)
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['to'] = to
    msg['cc'] = cc
    server.sendmail(sender_email,to,msg.as_string())
    server.quit()


query = "select * from sometable"
test_reciver = "[email protected]"

con = cx_Oracle.connect(user_db,password_db,dsn_tns,encoding="UTF-8", nencoding="UTF-8")
cc = '[email protected]'
df = pd.read_sql_query(query,con, index_col=None).head(1)
df = df.reset_index()
for index, row in df.iterrows():
    print(row['EMAIL'])
    subject = 'please clarify your Workgroup in SDM '
    to = row['EMAIL']
    body = 'sample body'
    send_email(subject, to, cc, body)

con.close()
1

There are 1 answers

3
MohammadReza moeini On

it is solved by sending email as an object with send_message instead of sendmail for smtplib

#send mail function
def send_email(subject,to,cc,body):
    host = "bulkmail.test.com"
    server = smtplib.SMTP(host)
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = '[email protected]'
    msg['to'] = to
    msg['cc'] = email_cc
    serve.send_message(msg)
    server.quit()