3

I'm looking to send an email as a string to an API via a service using Angular 6.

I would like to have a separate HTML file that has the code for this email template. I would then like to pass in this HTML template to the service and then post it to the API. (Please note - I do not want to build the email template as a string. i want to build it in HTML and then convert it to string).

So say i have the following files:

  1. email-template.html - holds the html email template code

  2. email.service.ts - posts the email template code as string

How would I implement this?

Here is my service:

constructor(private httpClient: HttpClient) { }

  send(recipient, email) { <=== need that 'email' perameter to be the email template.html as string

    const object: Email = {
      FromAddress: environment.noreplyEmailAddress,
      ToAddress: recipient,
      MessageBodyText: null,
      MessageBodyHtml: email.template,
      Subject: email.subject
    };

    return this.httpClient.post(`${this.url}/api/storeemail`, object, httpOptions);
  }
2
  • are you aware that by generating the mail on client side you are exposing an endpoint that anybody could use to send a random string as a mail? Commented Jan 21, 2019 at 12:47
  • yes, the api will have secuity around it to ensure only authorised parties can post to it - thanks for pointing that out though Commented Jan 21, 2019 at 12:50

1 Answer 1

1

You can simply import it within your component. Here is a working solution When you click on Click me button, it logs a html content into the console.

This is how you do it. First create a template in a file called email-template.html and put it somewhere (I put it in the same folder) and load it as follows.

import returns a Promise. You can do whatever you want within then

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  send() {
    import('./email-template.html').then(res => {
      console.log(res.default);
    });
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

thanks, getting the error 'Dynamic import cannot be used when targeting ECMAScript 2015 modules.'
I changed my tsconfig.json file to "module": "commonjs" and this fixed that issue but now i'm getting : 'Cannot find module '../email-template.html'
Make sure that the relative path is correct. It seems that you put your template one level above or you are trying to access that level
It's still erroring with cannot find module. Do i have to state that it is a static html file? i can see its working in your working example but the error is occuring in my project. I've tried toString() but that doesnt work
Do you get this errors after you build the application or before? @DBoi
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.