3

I was trying to create an appointment system where appointment system will send mail to the user where mail data will be dynamic but the problem is how cloud I pass data inside html_content like the date is a variable how can I pass date value inside Meeting Date + symbol is not working because mine is not string its an HTML code

Here is my Code

subject, from_email, to = 'You Have A Meeting Request', 'send@gmail.com', 'text@gmail.com'
text_content = 'Dear Sir,'
date = 12-12-2018
html_content = '<p>This is an ' \
               '<strong>Meeting Request</strong>' \
               ' <ul>' \
               '<li><b>Meeting Date:</b>  {{ date }}</li>' \
               '<li><strong>Meeting Place:</strong> DRL , Meeting Room 1</li>' \
               '<li><strong>Time:</strong> 10-30 To 13-30</li>' \
               '<li><strong>Meeting Purpose:</strong> For Opening Appointment</li>' \
               '<li><strong>No Of Person:</strong> 5</li>' \
               '<li><strong>Priority:</strong> High</li>' \
               '<li><strong>Request By:</strong> Mr X</li>' \
               '</ul>.</p>'

msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
Robot
  • 135
  • 1
  • 1
  • 6
  • Possible duplicate of [how to use concatenate a fixed string and a variable in Python](https://stackoverflow.com/questions/18348717/how-to-use-concatenate-a-fixed-string-and-a-variable-in-python) – Johan Oct 30 '18 at 06:33
  • 1
    No, that not help me cuz those are string but mine is HTML + not work for me giving an error – Robot Oct 30 '18 at 06:39
  • 1
    There is no datatype in Python that is HTML in the way you describe it. The data in `html_content` is going to be a string that **will form HTML**, but it's still a string. The answer you accepted is exactly what the duplicate describes, just in another way of concatenating the variables into a string. – Johan Oct 30 '18 at 08:59

1 Answers1

3

Why not just do something like this:

html_content = '''
<p>This is an 
<strong>Meeting Request</strong>
<ul>
<li><b>Meeting Date:</b>  %s</li>
<li><strong>Meeting Place:</strong> DRL , Meeting Room 1</li>
<li><strong>Time:</strong> 10-30 To 13-30</li>
<li><strong>Meeting Purpose:</strong> For Opening Appointment</li>
<li><strong>No Of Person:</strong> 5</li>
<li><strong>Priority:</strong> High</li>
<li><strong>Request By:</strong> Mr X</li>
</ul>.</p>
''' % (date)
Red Cricket
  • 9,762
  • 21
  • 81
  • 166