Tips for inserting an element with a background color into an email using variables

I have encountered an issue while trying to send an email that includes an element from my component.

The element is passed from Angular to C# and then sent to the customer.

Here is the element:

https://i.sstatic.net/2ky1b.png

Everything looks good in print, however, when I try to send an email, I encounter the following problem: https://i.sstatic.net/WUrWD.png

The email is being sent using the following code:

The content:

html

<div id="print-section" >
<div>
    <h1 >your makeup:</h1>
    <label>id: </label><span>{{product.id}}</span><br/>
    <label>codeInCompany: </label><span>{{product.codeInCompany}}</span><br/>
    <span id="color"></span><br>
    <span style="display:none" id='r'>{{product.r}}</span>
    <span style="display:none" id='g'>{{product.g}}</span>
    <span  style="display:none" id='b'>{{product.b}}</span>
    <label>price: </label><span>{{product.price}}</span><br/>
    <label>description: </label><span>{{product.description}}</span><br/>
    <label >company name: </label><span>{{product.company.name}}</span><br/>
    <h2 >thank you and have a good day!</h2>
    <br/>
</div>

ts

let printContents;
//The element to be printed:
printContents = document.getElementById('print-section').innerHTML;

let message:string=`
    <html>
      <head>
        <title>Print tab</title>
        <style> </style>
        <script>
         function color(){
           if(document.getElementById("r")!=null && document.getElementById("g")!=null && document.getElementById("b")!=null)
           {
             var r=document.getElementById("r").innerHTML;
             var g=document.getElementById("g").innerHTML;
             var b =document.getElementById("b").innerHTML;
             let style1="background-color: rgb"+"("+r+" "+ g+" "+b+")";
             var d=document.querySelector("[id*=color]");
             d.setAttribute("style",style1);
           }
         }
         window.addEventListener('load', (event) => {
           color();
         });
      </script>
       
     </head>
     <body onload="color()">${printContents}</body>
  </html>`;

Sending to server:

 this.customerService.sendMail(this.address,message).subscribe(
    data=>{
       this.alert="Email sent successfully";
    },
    fail=> {
       this.alert="Sorry, There is a problem with send email";
    });

Could anyone provide assistance with this issue?

Answer №1

I made a modification to the way the style is applied in the element: instead of using d.setAttribute in ts, I decided to utilize [style] in html.

Here is the updated code:

html

<div id="print-section" >
<div>
    <h1>your makeup</h1>
    <label>id: </label><span>{{product.id}}</span><br/>
    <label>codeInCompany: </label><span>{{product.codeInCompany}}</span><br/>
    <span name="color" id="color" [style]="myStyle">color</span><br>
    <span style="display:none" id='r'>{{product.r}}</span>
    <span style="display:none" id='g'>{{product.g}}</span>
    <span style="display:none" id='b'>{{product.b}}</span>
    <label>price: </label><span>{{product.price}}</span><br/>
    <label>description: </label><span>{{product.description}}</span><br/>
    <label>company name: </label><span>{{product.company.name}}</span><br/>
    <h2>thank you and have a good day</h2>
</div>

ts

//color element using variables

 myStyle="";

 ngOnInit() {
    this.colorlabel();
  }
  colorlabel(){
        var r=this.product.r;
        var g=this.product.g;
        var b=this.product.b;
        let style1="background-color: rgb"+"("+r+" "+ g+" "+b+");padding-top:20px";
        style1+=';width: 100px;height: 100px;border-radius: 50%;font-size:20px;font-family: Cambria, Cochin, Georgia, Times,Times New Roman, serif;font-weight: 600;display: inline-block;text-align:center;';
        this.myStyle=style1;
  }

//send to server

let printContents;
printContents = document.getElementById('print-section').innerHTML;
let message:string=`
<html>
  <head>
    <title>Email tab</title>
  </head>
  <body>${printContents}</body>
</html>`;
this.customerService.sendMail(this.address,message).subscribe(
  data=>{
    this.alert="Email sent successfully";
  },
  fail=> {
    this.alert="Sorry, There is a problem with send email";
  }
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switch the class of the Parent Component to that of the Child Component

Dashboard.html <nav #nav> <div class="nav nav-tabs" id="nav-tab" role="tablist"> <button class="nav-link active" id="nav-categories-tab" data-bs-toggle="tab" data-bs-targ ...

Setting the height of CKEditor 5: A comprehensive guide

How can I adjust the height of the CKeditor angular component? The documentation suggests we can set the editor style to: min-height: 500px !important; However, this solution does not seem to be effective for me. Any other suggestions? ...

I'm struggling to make my div display inline

My div elements are not displaying inline, I'm thinking maybe I shouldn't use the nav and div structure like this. But starting over again seems like a lot of work. How can I fix this issue? Just so you know, I'm only on my 4th day of learn ...

Creating a dynamic d3 force layout with interactive features in an Angular environment

I am currently working on a website using Angular and D3, although I don't have much experience with either of them. My goal is to create a force layout that is interactive, allowing users to select nodes and display related information in a sidebar. ...

Can someone please explain how I can extract and display information from a database in separate text boxes using Angular?

Working with two textboxes named AuthorizeRep1Fname and AuthorizeRep1Lname, I am combining them in typescript before storing them as AuthorizeRep1Name in the database. Refer to the image below for the result. This process is used to register and merge the ...

Drop-down menu in a table cell overflowing with lengthy text, causing the table to collapse

I'm facing an issue with my table where I have inputs and selects inside <td>. The problem arises when the text in the options of the <select> element is too long, causing the <td> to expand and disrupt the overall layout of the tabl ...

Clearing cookies in Express.js can be a challenging task

I've tried multiple options, but I can't seem to delete the cookie. Can anyone help me understand this? I have attempted using set maxage, expires, setheaders, but nothing seems to delete the cookie. It's impossible. res.cookie("refres ...

When the browser is resized, the fadeIn() function restarts from the

My plan was to create a dynamic effect where a browser-sized div would rotate through 3 different backgrounds using jQuery's fading effect. However, I encountered an issue - whenever I resize the browser window, the effect restarts from the beginning ...

How can one set relative paths for links when including a file from a different directory that contains links?

My project directory is structured as follows: root css img src login login.php dashboard basic header.php footer.php profile.php manage.php department ...

Scully.io prerenders the ROOT route as a static file, but it does not prerender any other routes in its process

Our production Angular 14.2.12 application is running smoothly. I decided to generate static pages using Scully, so I followed these steps: Run the command: ng add @scullyio/init This added Scully to my project. Next, I executed "ng build" and "npx sc ...

Scale first, then drag and drop to fix a faulty registration

I'm currently working on a project that involves dragging and dropping the correct items onto a technical drawing, such as a motherboard. The user is presented with pictures of components like CPUs or DDR memory and must drag these items to their corr ...

Challenge with validating a custom form control using Angular's FormArray

Could you please review the following issue? I have developed a custom component that functions as a checklist. I utilized FormArray to represent the rows, making each row a form array item and thus a FormGroup. Below is the form definition for the main ...

Encountering Compilation Error When Using RxJS Observable with Angular 6 and Swagger Codegen

Encountering TypeScript compiler errors related to rxjs while working with Angular 6 and Swagger Codegen: Cannot find module 'rxjs-compat/Observable' Referenced the following link for assistance: https://github.com/ReactiveX/rxjs/blob/master/M ...

Is it possible to stop the manipulation of HTML and CSS elements on a webpage using tools similar to Firebug?

How can we prevent unauthorized editing of HTML and CSS content on a webpage using tools like Firebug? I have noticed that some users are altering values in hidden fields and manipulating content within div or span tags to their advantage. They seem to be ...

Refresh the database records by clicking the submit button

Is there a way to update a database by entering text into a text box and submitting it, with the data being sent to the database with a specific id? The code snippet below outlines what needs to be done, but it seems to trigger a 403 error: Access forbid ...

The NextJS API is throwing an error due to a mysterious column being referenced in

I am currently in the process of developing an API that is designed to extract data from a MySQL table based on a specific category. The code snippet below represents my current implementation: import { sql_query } from "../../../lib/db" export ...

What is causing the #reset button to trigger the Flow.reset() function when the #gameboard does not contain any child elements?

Whenever I click on the resetBtn, it triggers the Flow.reset function regardless of whether the gameboard has child elements. Am I using the hasChildNodes() method incorrectly? const resetBtn = document.querySelector('#reset'); resetBtn.addEventL ...

How to utilize a PHP array within a Vue.js template

I have been exploring the realms of Laravel and vue.js recently and have encountered a challenge. Within my Laravel model, I have a PHP method that retrieves data from a database and organizes it into objects stored in an array. Now, my goal is to access t ...

Learn how to implement pagination in AngularJS using the $http service

I need assistance in implementing pagination using Angularjs within the Ionic Framework. Can someone provide guidance on how to code pagination for fetching data from a JSON URL? controller.js angular.module('starter.controllers', []) .control ...

Is there a way to make my modal appear only when the "New" option is clicked?

Is there a way to make my modal in VueJS open only when I click on the "New" option? <select v-model="input.des" @change="$refs.modalName.openModal()"> <option value="A">A</opt ...