{"id":49,"date":"2024-08-20T19:04:44","date_gmt":"2024-08-20T19:04:44","guid":{"rendered":"https:\/\/imcodinggenius.com\/?p=49"},"modified":"2024-08-20T19:04:44","modified_gmt":"2024-08-20T19:04:44","slug":"building-custom-email-templates-with-html-and-css-in-python","status":"publish","type":"post","link":"https:\/\/imcodinggenius.com\/?p=49","title":{"rendered":"Building Custom Email Templates with HTML and CSS in Python"},"content":{"rendered":"<p>An HTML email utilizes HTML code for presentation. Its design is heavy and looks like a modern web page, rich with visual elements like images, videos, etc., to emphasize different parts of an email&#8217;s content.<\/p>\n<p>Building email templates tailored to your brand is useful for various email marketing purposes such as welcoming new customers, order confirmation, and so on. Email template customization allows you to save time by not having to create emails from scratch each time. You can also include an <a href=\"https:\/\/mailtrap.io\/blog\/mailto-links-explained\/\" target=\"_blank\" rel=\"noopener\">email link in HTML<\/a> to automatically compose emails in your email client.<\/p>\n<p>In this step-by-step guide, you&#8217;ll learn how to build an HTML email template, add a CSS email design to it, and send it to your target audience.<\/p>\n<h2>Setting Up Your Template Directory and Jinja2<\/h2>\n<p>Follow the steps below to set up your HTML email template directory and Jinja2 for Python email automation:<\/p>\n<p><strong>Create a Template Directory<\/strong>: To hold your HTML email templates, you will need to set up a template directory inside your project module. Let&#8217;s name this directory &#8212; html_emailtemp.<\/p>\n<p><strong>Install Jinja2<\/strong>: Jinja is a popular templating engine for Python that developers use to create configuration files, HTML documents, etc. Jinja2 is its latest version. It lets you create dynamic content via loops, blocks, variables, etc. It&#8217;s used in various Python projects, like building websites and microservices, automating emails with Python, and more.<\/p>\n<p>Use this command to install Jinja2 on your computer:<\/p>\n<p>pip install jinja2<\/p>\n<h2>Creating an HTML Email Template<\/h2>\n<p>To create an <a href=\"https:\/\/mailtrap.io\/blog\/html-email\/\" target=\"_blank\" rel=\"noopener\">HTML email<\/a> template, let&#8217;s understand how to code your email step by step. If you want to modify your templates, you can do it easily by following the steps below:<\/p>\n<h3>Step 1: Structure HTML<\/h3>\n<p>A basic email will have a proper structure &#8212; a header, a body, and a footer.<\/p>\n<p><strong>Header<\/strong>: Used for branding purposes (in emails, at least)<br \/>\n<strong>Body<\/strong>: It will house the main text or content of the email<br \/>\n<strong>Footer<\/strong>: It&#8217;s at the end of the email if you want to add more links, information, or call-to-actions (CTA)<\/p>\n<p>Begin by creating your HTML structure, keeping it simple since email clients are less compatible than web browsers. For example, using tables is preferable for custom email layouts.<\/p>\n<p>Here&#8217;s how you can create a basic HTML mail with a defined structure:<\/p>\n<p><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">&#171;en&#187;<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">&#171;UTF-8&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">&#171;viewport&#187;<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">&#171;width=device-width, initial-scale=1&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>HTML Email Template<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">&#171;text\/css&#187;<\/span>&gt;<\/span><span class=\"css\"><br \/>\n        <span class=\"hljs-comment\">\/* Add your CSS here *\/<\/span><br \/>\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;100%&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;600&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-comment\">&lt;!&#8212; Header &#8212;&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;background-color: #1c3f60; color: #ffffff; text-align: center; padding: 20px;&#187;<\/span>&gt;<\/span><br \/>\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Your order is confirmed<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-comment\">&lt;!&#8212; Body &#8212;&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;padding: 20px; font-size: 16px; line-height: 1.6; color:#ffffff;&#187;<\/span>&gt;<\/span><br \/>\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>The estimated delivery date is 22nd August 2024.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-comment\">&lt;!&#8212; Footer &#8212;&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;background-color: #ff6100; color: #000000; text-align: center; padding: 20px;&#187;<\/span>&gt;<\/span><br \/>\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>For additional help, contact us at support@domain.com<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>&lt;!DOCTYPE html&gt;: This declares HTML as your document type.<br \/>\n&lt;html&gt;: This is an HTML page&#8217;s root element.<br \/>\n&lt;head&gt;: This stores the document&#8217;s metadata, like CSS styles.<br \/>\n&lt;style&gt;: CSS styles are defined here.<br \/>\n&lt;body&gt;: This stores your email&#8217;s main content.<br \/>\n&lt;table&gt;: This tag defines the email layout, giving it a tabular structure with cells and rows, which makes rendering easier for email clients.<br \/>\n&lt;tr&gt;: This tag defines the table&#8217;s row, allowing vertical content stacking.<br \/>\n&lt;td&gt;: This tag is used to define a cell inside a row. It contains content like images, text, buttons, etc.<\/p>\n<h3>Step 2: Structure Your Email<\/h3>\n<p>Now, let&#8217;s create the structure of your HTML email. To ensure it&#8217;s compatible with different email clients, use tables to generate a custom email layout, instead of CSS.<\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;100%&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;600&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;border: 1px solid #1c3f60; padding: 20px;&#187;<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;color: #7ed957;&#187;<\/span>&gt;<\/span>Hi, Jon!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span><br \/>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;font-size: 16px; color: #ffde59;&#187;<\/span>&gt;<\/span>Thank you for being our valuable customer!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><\/p>\n<h2>Styling the Email with CSS<\/h2>\n<p>Once you&#8217;ve defined your email structure, let&#8217;s start designing emails with HTML and CSS:<\/p>\n<h3>Inline CSS<\/h3>\n<p>Use inline CSS to ensure different email clients render CSS accurately and preserve the intended aesthetics of your email style.<\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;font-size: 16px; color: blue;&#187;<\/span>&gt;<\/span>Styled paragraph.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><\/p>\n<h3>Adjusting Style<\/h3>\n<p>Users might use different devices and screen sizes to view your email. Therefore, it&#8217;s necessary to adapt the style to suit various screen sizes. In this case, we&#8217;ll use media queries to achieve this goal and facilitate responsive email design.<\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">&#171;text\/css&#187;<\/span>&gt;<\/span><span class=\"css\"><br \/>\n    <span class=\"hljs-keyword\">@media<\/span> screen <span class=\"hljs-keyword\">and<\/span> (<span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">600px<\/span>) {<br \/>\n        <span class=\"hljs-selector-class\">.container<\/span> {<br \/>\n            <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span> <span class=\"hljs-meta\">!important<\/span>;<br \/>\n            <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-meta\">!important<\/span>;<br \/>\n        }<br \/>\n    }<br \/>\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">&#171;container&#187;<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;600&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-comment\">&lt;!&#8212; Content &#8212;&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>@media screen and (max-width: 600px) {&#8230;.}: This is a media query that targets device screens of up to 600 pixels, ensuring the style applies only to these devices, such as tablets and smartphones.<br \/>\nwidth: 100% !important;: This style changes the width of the table &#8212; .container. The code instructs that the table width be set to full screen, not 600px.<br \/>\n!important: This rule overrides other styles that may conflict with it.<br \/>\npadding: 10px !important;: Inside the .container table, a padding of 10px is added to the table.<\/p>\n<h3>Adding CTA Button and Links<\/h3>\n<p>Here, we are adding a call to action (CTA) link at the button &#8212; &#171;Get a 30-day free trial&#187; that points to this page &#8212; https:\/\/www.mydomain.com.<\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;margin: auto;&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;background-color: #8c52ff; padding: 10px 20px; border-radius: 5px;&#187;<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">&#171;https:\/\/www.mydomain.com&#187;<\/span> <span class=\"hljs-attr\">target<\/span>=<span class=\"hljs-string\">&#171;_blank&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;color: #ffffff; text-decoration: none; font-weight: bold;&#187;<\/span>&gt;<\/span>Get a 30-day free trial<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><\/p>\n<p><strong>Let&#8217;s Now Look at the Complete HTML Email Template:<\/strong><\/p>\n<p><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span><\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">&#171;en&#187;<\/span>&gt;<\/span><\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span><\/p>\n<p>  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">&#171;UTF-8&#187;<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">&#171;viewport&#187;<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">&#171;width=device-width, initial-scale=1&#187;<\/span>&gt;<\/span><\/p>\n<p>  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>HTML Email Template<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span><\/p>\n<p>  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">&#171;text\/css&#187;<\/span>&gt;<\/span><span class=\"css\"><br \/>\n    <span class=\"hljs-comment\">\/* Adding the CSS *\/<\/span><\/span><\/p>\n<p>    <span class=\"hljs-selector-tag\">body<\/span> {<br \/>\n      <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;<br \/>\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#f4f4f4<\/span>;<br \/>\n      <span class=\"hljs-attribute\">font-family<\/span>: Arial, sans-serif;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-tag\">table<\/span> {<br \/>\n      <span class=\"hljs-attribute\">border-collapse<\/span>: collapse;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-class\">.mailcontainer<\/span> {<br \/>\n      <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;<br \/>\n      <span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">600px<\/span>;<br \/>\n      <span class=\"hljs-attribute\">margin<\/span>: auto;<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ffffff<\/span>;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-class\">.header<\/span> {<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#1c3f60<\/span>;<br \/>\n      <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#ffffff<\/span>;<br \/>\n      <span class=\"hljs-attribute\">text-align<\/span>: center;<br \/>\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-class\">.body<\/span> {<br \/>\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;<br \/>\n      <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">16px<\/span>;<br \/>\n      <span class=\"hljs-attribute\">line-height<\/span>: <span class=\"hljs-number\">1.6<\/span>;<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#1c3f60<\/span>;<br \/>\n      <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#7ed957<\/span>;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-class\">.footer<\/span> {<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ff6100<\/span>;<br \/>\n      <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#000000<\/span>;<br \/>\n      <span class=\"hljs-attribute\">text-align<\/span>: center;<br \/>\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-selector-class\">.cta<\/span> {<br \/>\n      <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#8c52ff<\/span>;<br \/>\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-number\">20px<\/span>;<br \/>\n      <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">5px<\/span>;<br \/>\n      <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#ffffff<\/span>;<br \/>\n      <span class=\"hljs-attribute\">text-decoration<\/span>: none;<br \/>\n      <span class=\"hljs-attribute\">font-weight<\/span>: bold;<br \/>\n    }<\/p>\n<p>    <span class=\"hljs-keyword\">@media<\/span> screen <span class=\"hljs-keyword\">and<\/span> (<span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">600px<\/span>) {<br \/>\n      <span class=\"hljs-selector-class\">.container<\/span> {<br \/>\n        <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span> <span class=\"hljs-meta\">!important<\/span>;<br \/>\n        <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">10px<\/span> <span class=\"hljs-meta\">!important<\/span>;<br \/>\n      }<br \/>\n    }<br \/>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span><\/p>\n<p><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;100%&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span>&gt;<\/span><br \/>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">&#171;container&#187;<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;600&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span>&gt;<\/span><\/p>\n<p>          <span class=\"hljs-comment\">&lt;!&#8212; Header &#8212;&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">&#171;header&#187;<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Your order is confirmed<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><\/p>\n<p>          <span class=\"hljs-comment\">&lt;!&#8212; Body &#8212;&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">&#171;body&#187;<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>The estimated delivery date is 22nd August 2024.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;font-size: 16px; color: blue;&#187;<\/span>&gt;<\/span>Styled paragraph.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">&#171;100%&#187;<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;border: 1px solid #1c3f60; padding: 20px;&#187;<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;color: #7ed957;&#187;<\/span>&gt;<\/span>Hi, Jon!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;font-size: 16px; color: #ffde59;&#187;<\/span>&gt;<\/span>Thank you for being our valuable customer!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n                  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">table<\/span> <span class=\"hljs-attr\">cellpadding<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">cellspacing<\/span>=<span class=\"hljs-string\">&#171;0&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;margin: auto;&#187;<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n                  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">align<\/span>=<span class=\"hljs-string\">&#171;center&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;background-color: #8c52ff; padding: 10px 20px; border-radius: 5px;&#187;<\/span>&gt;<\/span><br \/>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">&#171;https:\/\/www.mydomain.com&#187;<\/span> <span class=\"hljs-attr\">target<\/span>=<span class=\"hljs-string\">&#171;_blank&#187;<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">&#171;noopener noreferrer&#187;<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;color: #ffffff; text-decoration: none; font-weight: bold;&#187;<\/span>&gt;<\/span>Get a 30-day free trial<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><br \/>\n                  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><\/p>\n<p>          <span class=\"hljs-comment\">&lt;!&#8212; Footer &#8212;&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">td<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">&#171;background-color: #ff6100; color: #000000; text-align: center; padding: 20px;&#187;<\/span>&gt;<\/span><br \/>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>For additional help, contact us at support@domain.com<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><br \/>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><\/p>\n<p>        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">td<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">tr<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">table<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span><\/p>\n<p><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>.mailcontainer: This is a class that you can use to style your email content&#8217;s main section. It&#8217;s given a set width, margin, border, and color.<br \/>\n.header, .footer, .body: These are classes used to style your email&#8217;s header, footer, and body, respectively.<br \/>\n.cta: This class allows you to style your buttons, such as CTA buttons, with a specified color, border design, padding, etc.<\/p>\n<h2>Bringing Everything Together With Jinja2<\/h2>\n<p>Having created our HTML template, it&#8217;s now time to bring everything together using the Jinja2 templating engine.<\/p>\n<h3>Import Project Modules<\/h3>\n<p>You&#8217;ve already set up your template directory &#8212; html_emailtemp. Now you can find and render templates using code. But before you do that, import the relevant project modules using the code below:<\/p>\n<p><span class=\"hljs-keyword\">from<\/span> jinja2 <span class=\"hljs-keyword\">import<\/span> Environment, PackageLoader, select_autoescape<\/p>\n<p>env = Environment(loader=PackageLoader(<span class=\"hljs-string\">&#8217;email_project&#8217;<\/span>, <span class=\"hljs-string\">&#8216;html_emailtemp&#8217;<\/span>), autoescape=select_autoescape([<span class=\"hljs-string\">&#8216;html&#8217;<\/span>, <span class=\"hljs-string\">&#8216;xml&#8217;<\/span>]))<\/p>\n<p><strong>Explanation<\/strong>:<\/p>\n<p>Environment: Jinja2 utilizes a central object, the template Environment. Its instances store global objects and configurations, and load your email templates from a file.<\/p>\n<p>PackageLoader: This configures Jinja2 to load email templates.<\/p>\n<p>autoescape: To mitigate security threats such as cross-site scripting (XSS) attacks and protect your code, you can escape values (that are passed to the email template) while rendering HTML using the command autoescape. Or, you can validate user inputs to reject malicious code.<\/p>\n<p>For security, autoescape is set to True to enable escaping values. If you turn it to False, Jinja2 won&#8217;t be able to escape values, and XSS attacks may occur. To enable autoescape, set autoescape to True:<\/p>\n<p>env = Environment(loader=PackageLoader(&#171;myapp&#187;), autoescape=True)<\/p>\n<h3>Load Your Template<\/h3>\n<p>Once done, a template environment will be created with a template loader to find email templates created inside your project module&#8217;s template folder.<\/p>\n<p>Next, load your HTML email template using the method &#8212; get_template(). This function will return your loaded template. It also offers several benefits such as enabling email template inheritance, so you can reuse the template in multiple scenarios.<\/p>\n<p>template1 = env.get_template(&#171;myemailtemplate.html&#187;)<\/p>\n<h3>Render the Template<\/h3>\n<p>To render your email template, use the method &#8212; render()<\/p>\n<p>html1 = template1.render()<\/p>\n<p>As these HTML email templates are dynamic, you can pass keyworded arguments (kwargs) with Jinja2 to the render function. The kwargs will then be passed to your email template. Here&#8217;s how you can render your templates using the destined user&#8217;s name &#8212; &#171;Jon Doe&#187; &#8212; in your email.<\/p>\n<p>html1 = template1.render(name=&#187;Jon Doe&#187;)<\/p>\n<p><strong>Let&#8217;s look at the complete code for this section:<\/strong><\/p>\n<p><span class=\"hljs-keyword\">from<\/span> jinja2 <span class=\"hljs-keyword\">import<\/span> Environment, PackageLoader, select_autoescape<\/p>\n<p>env = Environment(loader=PackageLoader(<span class=\"hljs-string\">&#171;email_project&#187;<\/span>, <span class=\"hljs-string\">&#171;html_emailtemp&#187;<\/span>),<br \/>\n                  autoescape=select_autoescape([<span class=\"hljs-string\">&#171;html&#187;<\/span>, <span class=\"hljs-string\">&#171;xml&#187;<\/span>]))<\/p>\n<p>template1 = env.get_template(<span class=\"hljs-string\">&#171;myemailtemplate.html&#187;<\/span>)<br \/>\nhtml1 = template1.render()<\/p>\n<h2>Sending the Email<\/h2>\n<p>To send an email, you can use the application-level, straightforward protocol &#8212; Simple Mail Transfer Protocol (SMTP). This protocol streamlines the email sending process and determines how to format, send, and encrypt your emails between the source and destination mail servers.<\/p>\n<p>In this instance, we&#8217;ll send emails in Python via SMTP since Python offers a built-in module for email sending. To send emails, Python provides a library, &#8216;smtplib&#8217;, to facilitate effortless interaction with the SMTP protocol.<\/p>\n<p>To get started:<\/p>\n<p><strong>Install &#8216;smtplib&#8217;<\/strong>: Ensure you have installed Python on your system. Now, import &#8216;smtplib&#8217; to set up connectivity with the mail server.<\/p>\n<p>import smtplib<\/p>\n<p><strong>Define your HTML parameter<\/strong>: Define your HTML parameter for the mail object where you&#8217;ll keep your HTML template. It will instruct email clients to render the template.<\/p>\n<p>Here&#8217;s the full code for this section:<\/p>\n<p><span class=\"hljs-keyword\">import<\/span> smtplib<\/p>\n<p><span class=\"hljs-keyword\">from<\/span> email.mime.text <span class=\"hljs-keyword\">import<\/span> MIMEText    <span class=\"hljs-comment\"># MIMEText is a class from the email package <\/span><\/p>\n<p><span class=\"hljs-keyword\">from<\/span> jinja2 <span class=\"hljs-keyword\">import<\/span> Template   <span class=\"hljs-comment\"># Let&#8217;s use Template class for our HTML template <\/span><\/p>\n<p>sender = <span class=\"hljs-string\">&#171;&lt;a href=&#8217;mailto:sender1@gmail.com&#8217; target=&#8217;_blank&#8217; rel=&#8217;noopener noreferrer&#8217;&gt;sender1@gmail.com&lt;\/a&gt;&#187;<\/span><\/p>\n<p>recipient = <span class=\"hljs-string\">&#171;&lt;a href=&#8217;mailto:recipient1@gmail.com&#8217; target=&#8217;_blank&#8217; rel=&#8217;noopener noreferrer&#8217;&gt;recipient1@gmail.com&lt;\/a&gt;&#187;<\/span><\/p>\n<p>subject = <span class=\"hljs-string\">&#171;Your order is confirmed!&#187;<\/span><\/p>\n<p><span class=\"hljs-keyword\">with<\/span> <span class=\"hljs-built_in\">open<\/span>(<span class=\"hljs-string\">&#8216;myemailtemplate.html&#8217;<\/span>, <span class=\"hljs-string\">&#8216;r&#8217;<\/span>) <span class=\"hljs-keyword\">as<\/span> f:<br \/>\n    template1 = Template(f.read())<\/p>\n<p><span class=\"hljs-comment\"># Enter the HTML template<\/span><\/p>\n<p>html_emailtemp = <span class=\"hljs-string\">&#171;&#187;&#187;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang=&#8217;en&#8217;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset=&#8217;UTF-8&#8242;&gt;<br \/>\n    &lt;meta name=&#8217;viewport&#8217; content=&#8217;width=device-width, initial-scale=1&#8242;&gt;<\/span><\/p>\n<p>    &lt;title&gt;HTML Email Template&lt;\/title&gt;<\/p>\n<p>    &lt;style type=&#8217;text\/css&#8217;&gt;   # Adding the CSS<br \/>\n        body { margin: 0; padding: 0; background-color: #f4f4f4; font-family: Arial, sans-serif; }<br \/>\n        table { border-collapse: collapse; }<br \/>\n        .mailcontainer { width: 100%; max-width: 600px; margin: auto; background-color: #ffffff; }<br \/>\n        .header { background-color: #1c3f60; color: #ffffff; text-align: center; padding: 20px; }<br \/>\n        .body { padding: 20px; font-size: 16px; line-height: 1.6; background-color: #1c3f60; color: #7ed957; }<br \/>\n        .footer { background-color: #ff6100; color: #000000; text-align: center; padding: 20px; }<br \/>\n        .cta { background-color: #8c52ff; padding: 10px 20px; border-radius: 5px; color: #ffffff; text-decoration: none; font-weight: bold; }<\/p>\n<p>        @media screen and (max-width: 600px) {<br \/>\n            .container {<br \/>\n                width: 100% !important;<br \/>\n                padding: 10px !important;<br \/>\n            }<br \/>\n        }<br \/>\n    &lt;\/style&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;table width=&#8217;100%&#8217; cellpadding=&#8217;0&#8242; cellspacing=&#8217;0&#8242;&gt;<br \/>\n        &lt;tr&gt;<br \/>\n            &lt;td align=&#8217;center&#8217;&gt;<br \/>\n                &lt;table class=&#8217;container&#8217; width=&#8217;600&#8242; cellpadding=&#8217;0&#8242; cellspacing=&#8217;0&#8242;&gt;<br \/>\n                    &lt;!&#8212; Header &#8212;&gt;<br \/>\n                    &lt;tr&gt;<br \/>\n                        &lt;td class=&#8217;header&#8217;&gt;<br \/>\n                            &lt;h1&gt;Your order is confirmed&lt;\/h1&gt;<br \/>\n                        &lt;\/td&gt;<br \/>\n                    &lt;\/tr&gt;<br \/>\n                    &lt;!&#8212; Body &#8212;&gt;<br \/>\n                    &lt;tr&gt;<br \/>\n                        &lt;td class=&#8217;body&#8217;&gt;<br \/>\n                            &lt;p&gt;The estimated delivery date is 22nd August 2024.&lt;\/p&gt;<br \/>\n                            &lt;p style=&#8217;font-size: 16px; color: blue;&#8217;&gt;Styled paragraph.&lt;\/p&gt;<br \/>\n                            &lt;table width=&#8217;100%&#8217; cellpadding=&#8217;0&#8242; cellspacing=&#8217;0&#8242; style=&#8217;border: 1px solid #1c3f60; padding: 20px;&#8217;&gt;<br \/>\n                                &lt;tr&gt;<br \/>\n                                    &lt;td align=&#8217;center&#8217;&gt;<br \/>\n                                        &lt;h1 style=&#8217;color: #7ed957;&#8217;&gt;Hi, Jane!&lt;\/h1&gt;<br \/>\n                                        &lt;p style=&#8217;font-size: 16px; color: #ffde59;&#8217;&gt;<br \/>\n                                            Thank you for being our valuable customer!<br \/>\n                                        &lt;\/p&gt;<br \/>\n                                    &lt;\/td&gt;<br \/>\n                                &lt;\/tr&gt;<br \/>\n                            &lt;\/table&gt;<br \/>\n                            &lt;table cellpadding=&#8217;0&#8242; cellspacing=&#8217;0&#8242; style=&#8217;margin: auto;&#8217;&gt;<br \/>\n                                &lt;tr&gt;<br \/>\n                                    &lt;td align=&#8217;center&#8217; style=&#8217;background-color: #8c52ff; padding: 10px 20px; border-radius: 5px;&#8217;&gt;<br \/>\n                                        &lt;a href=&#8217;https:\/\/www.mydomain.com&#8217; target=&#8217;_blank&#8217; rel=&#8217;noopener noreferrer&#8217; style=&#8217;color: #ffffff; text-decoration: none; font-weight: bold;&#8217;&gt;Get a 30-day free trial&lt;\/a&gt;<br \/>\n                                    &lt;\/td&gt;<br \/>\n                                &lt;\/tr&gt;<br \/>\n                            &lt;\/table&gt;<br \/>\n                        &lt;\/td&gt;<br \/>\n                    &lt;\/tr&gt;<br \/>\n                    &lt;!&#8212; Footer &#8212;&gt;<br \/>\n                    &lt;tr&gt;<br \/>\n                        &lt;td style=&#8217;background-color: #ff6100; color: #000000; text-align: center; padding: 20px;&#8217;&gt;<br \/>\n                            &lt;p&gt;For additional help, contact us at support@domain.com&lt;\/p&gt;<br \/>\n                        &lt;\/td&gt;<br \/>\n                    &lt;\/tr&gt;<br \/>\n                &lt;\/table&gt;<br \/>\n            &lt;\/td&gt;<br \/>\n        &lt;\/tr&gt;<br \/>\n    &lt;\/table&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<br \/>\n&#171;&#187;&#187;<\/p>\n<p>template1 = Template(html_emailtemp)<br \/>\nhtml1 = template1.render(name=<span class=\"hljs-string\">&#171;Jon Doe&#187;<\/span>)<\/p>\n<p><span class=\"hljs-comment\"># Attach your MIMEText objects for HTML<\/span><\/p>\n<p>message = MIMEText(html1, <span class=\"hljs-string\">&#8216;html&#8217;<\/span>)<br \/>\nmessage[<span class=\"hljs-string\">&#8216;Subject&#8217;<\/span>] = subject<br \/>\nmessage[<span class=\"hljs-string\">&#8216;From&#8217;<\/span>] = sender<br \/>\nmessage[<span class=\"hljs-string\">&#8216;To&#8217;<\/span>] = recipient<\/p>\n<p><span class=\"hljs-comment\"># Send the HTML email<\/span><\/p>\n<p><span class=\"hljs-keyword\">with<\/span> smtplib.SMTP_SSL(<span class=\"hljs-string\">&#8216;smtp.gmail.com&#8217;<\/span>, <span class=\"hljs-number\">465<\/span>) <span class=\"hljs-keyword\">as<\/span> server:<br \/>\n    server.login(username, password)<br \/>\n    server.sendmail(sender, recipient, message.as_string())<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>sender: The sender&#8217;s email address<br \/>\nrecipient: The recipient&#8217;s email address<br \/>\nfrom email.mime.text import MIMEText: This is used to import the class MIMEText, enabling you to attach your HTML template in the email.<br \/>\nsmtplib.SMTP_SSL(&#8216;smtp.gmail.com&#8217;, 465) as server:: This establishes a connection with your email provider&#8217;s (Gmail&#8217;s) SMTP server using port 465. If you are using another SMTP provider, use their domain name, such as smtp.domain.com, with an appropriate port number. The connection is secured with SSL.<br \/>\nserver.login(username, password): This function allows you to log in to the email server using your username and password.<br \/>\nserver.sendemail(sender, recipient, message.as_string()): This command sends the HTML email.<\/p>\n<h3>Testing<\/h3>\n<p>Before sending your HTML email, test it to understand how different email clients render CSS and HTML. Testing tools like Email on Acid, Litmus, etc. can assist you.<\/p>\n<h2>Conclusion<\/h2>\n<p>To build custom email templates with HTML and CSS in Python, follow the above instructions. First, begin structuring your HTML email template, style emails with CSS, and then send them to your recipients. Always check your email template&#8217;s compatibility with different email clients and ensure to keep your HTML simple using tables. Adding an <a href=\"https:\/\/stackabuse.com\/python-specific-design-patterns\/\" target=\"_blank\" rel=\"noopener\">email link in HTML<\/a> will also allow you to compose an email automatically in your email client and send it to a specific email address.<\/p>","protected":false},"excerpt":{"rendered":"<p>An HTML email utilizes HTML code for presentation. Its design is heavy and looks like a modern web page, rich with visual elements like images, videos, etc., to emphasize different parts of an email&#8217;s content. Building email templates tailored to your brand is useful for various email marketing purposes such &#8230; <\/p>\n<div><a class=\"more-link bs-book_btn\" href=\"https:\/\/imcodinggenius.com\/?p=49\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/49"}],"collection":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":0,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}