python - How to properly create dynamic table email? -
assume have defined dataframe. make sense? there way can define create_email()
saved 1 string instead of writing message_body = create_email()
.. have feeling not proper way it.
import smtplib import pandas pd def create_email(): print('<html><body>') print('<table><tr><th>security</th><th>ticker</th><th>yesterday</th><th>currency</th></tr>') index, row in df.iterrows(): print('<tr><td>' + row['a'] + '</td>') print('<td>' + row['b'] + '</td>') print('<td>' + str(row['c']) + '</td>') print('<td>' + row['d'] + '</td></tr>') print('</table>') print('</html></body>') class gmail(object): def __init__(self, email, password, recepient): self.email = email self.password = password self.recepient = recepient self.server = 'smtp.gmail.com' self.port = 465 session = smtplib.smtp_ssl(self.server, self.port) session.ehlo session.login(self.email, self.password) self.session = session print('connected gmail account successfully.') def send_message(self, subject, body): headers = [ "from: " + self.email, "subject: " + subject, "to: " + self.recepient, "mime-version: 1.0", "content-type: text/html"] headers = "\r\n".join(headers) self.session.sendmail( self.email, self.recepient, headers + "\r\n\r\n" + body) print('- message has been sent.') print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') rec = 'email@mail.com' print('email to: ' + rec) message_body = create_email() gm = gmail('test@gmail.com', 'test_password', rec) gm.send_message('daily update: ' + date_time, message_body) print('-- message ' + rec + 'is completed.')
i trying dynamically create table using html shoots off email of table made based on dataframe.
Comments
Post a Comment