18

How do I create an email service in Angular 7 that contains a method for sending out emails?

For example:

// email.service.ts
import { Injectable } from ‘@angular/core’;

@Injectable()

export class EmailService {

    constructor() { }

    // method to fetch data from server
    public sendEmail(): void {
       // logic to send email
      ...
    }
}

I'm new to developing with Angular so any examples, use cases, and/or code snippets are greatly appreciated.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mokka soru
  • 503
  • 2
  • 4
  • 12
  • 2
    As far as i know it's impossible to do right away from js at all, but you can write a **web server** with **http api** or use already existing **third party server** that would do that, and you just need to **call it** from your **angular application**. – Amir Arbabian Feb 24 '19 at 08:12
  • I Thought it's not possible with angular –  Mar 06 '19 at 10:41
  • 2
    Angular is a Front-End View Framework and is not Server Transaction Smart but rather browser smart. For that matter, Browsers aren't capable to send emails so angular unfortunately cannot send an email with it's own built in API's. You need a back-end service that is server smart to implement email transactions. This will depend on what you want to use. Implement API's to take care of those transactions. – Mosia Thabo May 26 '19 at 16:27

4 Answers4

22

You could use some BaaS (Backend as a service) provider, like Formspree. It's pretty simple to use and enables you to send emails without have to write or setup a backend. All you have to do is post an Http request from your angular application and this service takes care of the rest.

  1. create an account at formspree.io
  2. click the "+New Form" button
  3. enter a email that you want to receive the emails

Then you'll get an unique endpoint for this newly create form wich you can use to send (post) emails to from your angular application. The endpoint will look something like this : https://formspree.io/asdlf7asdf

Here is some example code using Template driven forms:

The html form

<form (ngSubmit)="onSubmit(contactForm)" #contactForm="ngForm">
    <input type="text" placeholder="Name" name="name" ngModel required #name="ngModel">
    <input type="text" placeholder="Email" email name="email" ngModel required #email="ngModel">
    <textarea placeholder="Messages" name="messages" ngModel required #messages="ngModel"></textarea>
    <input type="submit" value="Send">
</form>

The code behind

 onSubmit(contactForm: NgForm) {
    if (contactForm.valid) {
      const email = contactForm.value;
      const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
      this.http.post('https://formspree.io/asdlf7asdf',
        { name: email.name, replyto: email.email, message: email.messages },
        { 'headers': headers }).subscribe(
          response => {
            console.log(response);
          }
        );
    }
  }

You can do much more with this (or other) services but this would be the basic gist of it.

Community
  • 1
  • 1
8

Angular working at client side and for sending email you need a server, so I think it is impossible for now to send email using only angular you need to used server side application for sending email.

Dhaval
  • 868
  • 12
  • 22
3

This is impossible to send Emails via Angular. how ever,you can create Node Js express sever and from there you can send the emails. i have used the following project to implement that : https://github.com/niftylettuce/email-templates

Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
  • Hi do you know how to use this in typescript? i am not sure how to import EmailTemplate... thanks. – Rachel Mar 04 '19 at 21:10
2

You need to understand the nature of angular in the first place. But assuming that you're new to angular - you must understand that Angular is just a view front-end framework. So it has no capabilities to send emails on its own. To make angular transaction-capable, you need a backend system that angular can tell to send emails on its behalf.

This is achieved through Web API's. So you must create an API where angular can inform the API to send emails. As far as my knowledge is concerned, this is the method I am aware is most widely used.

Mosia Thabo
  • 4,009
  • 1
  • 14
  • 24