Smtlib不向Pandas Dataframe 列列表中的电子邮件地址发送电子邮件

7bsow1i6  于 2022-09-21  发布在  其他
关注(0)|答案(1)|浏览(143)

亲爱的们

我有一个代码连接到Oracle数据库,以Daraframe的身份获取查询,并将电子邮件发送给列中存在的接收者

当我将TEST_Reciver参数设置为电子邮件地址TEST_Reciver时进行测试,但当我将(Email Receiver)设置为数据框列时,未收到电子邮件


# send mail function

def send_email(subject,to,cc,body):
    sender_email = 'test@test.com'
    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 = "myemail@test.com"

con = cx_Oracle.connect(user_db,password_db,dsn_tns,encoding="UTF-8", nencoding="UTF-8")
cc = 'emailcctest@test.com'
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()
oknwwptz

oknwwptz1#

通过使用Send_Message而不是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'] = 'test@test.com'
    msg['to'] = to
    msg['cc'] = email_cc
    serve.send_message(msg)
    server.quit()

相关问题