'Read data from incoming kafka message or database and put it into Email Template using python then publish Email template on kafka topic

email_template = """

<b>Opportunity Summary <a href="{{ opptyURL }}">{{ OpptyNumber }}</a></b><br>
Client: {{ clientName }}<br>
Opportunity Desc: {{ opptyDesc }}<br>
Total TCV (USD): {{ TCV }}<br>
Country: {{ country }}<br>
Geo: {{ geo }}<br>
Market: {{ market }}<br>
Sector: {{ sectorName }}<br>
Industry: {{ sicName }}<br>
OO: {{ ooEmail }}<br>
<br>

enter image description here

In the above code snippet i want to read values from kafka message and insert into email template for example OpportunityNumber read from kafka message and insert into template after replacing all values .send that email template to kafka topic.



Solution 1:[1]

First, create a Template object, then consume your data and update the template string to create the email string.

Then send emails.

from string import Template

t = Template(...)

consumer = ... # refer your Kafka client documentation
producer = ...

# example from kafka-python
for message in consumer:
  # records can be JSON strings, deserialzied would make data a dict()
  data = consumer.value()  
  # apply values to template   
  email_body = t.substitute(data)
  # send email
  smtp...

  # or send to kafka
  producer.send('email-topic', email_body) 

The same could be done with Jinja2 or Mako templates instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneCricketeer