Created
March 7, 2018 12:27
-
-
Save felixivance/03cb7e922a6470fbe5e0d658d502a99a to your computer and use it in GitHub Desktop.
Angular Service send email confirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; | |
import 'rxjs/add/operator/catch'; | |
import 'rxjs/add/operator/retry'; | |
import 'rxjs/add/operator/toPromise'; | |
import { environment } from '../../../environments/environment'; | |
export const CLOUD_FUNCTION_URL = environment.firebase['cloudFunctionUrl']; | |
@Injectable() | |
export class SendEmailConfirmationService { | |
constructor(private httpClient: HttpClient) { | |
} | |
sendEmailConfirmation(emailAddress) { | |
let url = `${CLOUD_FUNCTION_URL}`; | |
let emailHeaders = new HttpHeaders(); | |
emailHeaders.append('Content-Type', 'application/json'); | |
emailHeaders.append('Access-Control-Allow-Origin', '*'); | |
let emailParams = new HttpParams(); | |
emailParams.append('to', emailAddress); | |
emailParams.append('from', 'example@gmail.com'); /** replace 'example@gmail.com' with a working email address, I simply sent an email to myself. */ | |
emailParams.append('subject', 'Test 1 Subject'); | |
emailParams.append('text', 'test 1 text'); | |
emailParams.append('html', '<html><p>Test 1!</p></html>'); | |
let body = { | |
to: emailAddress, | |
from: 'example@gmail.com', /** replace 'example@gmail.com' with a working email address, I simply sent an email to myself. */ | |
subject: 'Test 1 Subject', | |
text: 'test 1 text', | |
html: '<html><p>Test 1!</p></html>' | |
}; | |
return this.httpClient.post(url, body, { | |
headers: emailHeaders, | |
params: emailParams | |
}) | |
.toPromise() | |
.then(res => { | |
console.log(res); | |
}) | |
.catch(err => { | |
console.log(err); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment