Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Last active April 19, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codediodeio/61904b1d2d9b47c898d83046c701f33a to your computer and use it in GitHub Desktop.
Save codediodeio/61904b1d2d9b47c898d83046c701f33a to your computer and use it in GitHub Desktop.
Trigger HTTP Cloud Function via Angular Component (for transactional email)
<button (click)="sendEmail()">Send Email via Cloud Function</button>
/// SEE https://gist.github.com/codediodeio/f50c2d2f3cc0243814a40f71f221d2ab
/// To setup transactional email with sendgrid and firebase google cloud functions
import { Component } from '@angular/core';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Component({
selector: 'send-email',
templateUrl: './send-email.component.html',
styleUrls: ['./send-email.component.scss']
})
export class SendEmailComponent implements OnInit {
constructor(private http: Http) { }
sendEmail() {
let url = `https://your-cloud-function-url/function`
let params: URLSearchParams = new URLSearchParams();
let headers = new Headers({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });
params.set('to', 'user@example.com');
params.set('from', 'you@yoursupercoolapp.com');
params.set('subject', 'test-email');
params.set('content', 'Hello World');
return this.http.post(url, params, headers)
.toPromise()
.then( res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}
}
@anurag-aadi
Copy link

the header is showing error in that case but if we use like

return this.http.post(url, params, headers)
to
return this.http.post(url, params, {headers})

then its not showing error but also not working as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment