Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Send input data in HTML Body with Python

brunosa
7 - Meteor

Hi,

 

I've been trying to include a basic table I create to be pushed in the body of the email when I'm using the Python tool. I'm able to send the email through Python with no problem but cannot find the proper line code to include my table into it. Below the table and the code in Python. Any tips?

 

brunosa_0-1666530100526.png

 

brunosa_1-1666530289262.png

 

3 REPLIES 3
acarter881
12 - Quasar

Hey, @brunosa. Have you tried using the Email tool instead of Python? You're currently getting a TypeError in Python when reading the DataFrame. Perhaps validating the DataFrame's type then working accordingly may solve the issue. I believe the body should be text and you are reading in the entire DataFrame and assigning it to the body. That may need to be edited, perhaps by assigning the data in the cell to df. Just thinking out loud, as I haven't tried this one myself.

apathetichell
19 - Altair

I'm not much of a Python in Alteryx person - but I'd recommend using the Python cheat sheet here: https://community.alteryx.com/t5/Data-Science/Python-Tool-Cheat-Sheet-and-Resources/ba-p/554223 - the Alteryx package needs to know what it's reading in - and in this case it should be '#1' - not your message. The Alteryx package identifies your datastream coming into the Python tool via assignments like #1,#2, etc - these are designations for a safearray and critical to using the Alteryx package.

aduval
6 - Meteoroid

Hi, @brunosa.  You need to read input one as your data frame. Like @apathetichell mentioned above.

df = Alteryx.read('#1)

 

You will then need to convert this to html using pandas.

 

import pandas as pd

html = df.to_html()

This will return the html as a string. 

 

I use the python email module to send emails.

 

# Import smtplib for the actual sending function
import smtplib

# Import the email modules 
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')
msg['Subject'] = 'subject'
msg['From'] = 'me'
msg['To'] = 'you'

# Add table to email
table = MIMEText(html, 'html')
msg.attach(table)

# Send the message via SMTP server.
s = smtplib.SMTP('server')
s.send_message(msg)
s.quit()

 

Hopefully this helps.

 

Let me know if you need any more help.

Labels
Top Solution Authors