Send a contact form by email with angular 6 and PHP

0

I have problems when sending the data of a contact form made in Angular 6 by email using PHPmailer.

Versions:

  • Node 8.11.1
  • Angular 6.0.1
  • rxjs 6.1.0
  • typescript 2.7.2
  • webpack 4.6.0
  • OS darwin x64 Macbook pro high sierra

I can not find a solution to the error:

  

FormComponent.html: 3 ERROR TypeError: this.sendServices.sendEmail (...). subscribe is not a function at FormComponent.push ../ src / app / form / form.component.ts.FormularioComponent.onSubmit ( formulario.component.ts: 60)

The formulario.component.ts component is as follows:

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Observable } from 'rxjs';
import { of } from 'rxjs';

import { SendEmailService, FormInterface } from '../services/form.service';

@Component({
  selector: 'likuid-formulario',
  templateUrl: './formulario.component.html',
  styles: ['./formulario.component.scss']
})

export class FormularioComponent implements OnInit {

  submitted = false;

  nCorporacion: FormInterface = {
    nombre: '',
    telefono: '',
    telefonoAlt: '',
    email: '',
    whatsapp: '',
    corporacion: '',
    corporacionAlt: '',
    sufijo: 'Corp',
    ein: 'si',
    dirVirtual: true,
    dirCorporacion: '',
    ciudadCorporacion: '',
    estadoCorporacion: '',
    cpCorporacion: '',
    presidente: '',
    tipoDocPre: 'Pasaporte',
    docPresidente: '',
    vicepresidente: '',
    tipoDocVice: 'Pasaporte',
    docVicepresidente: '',
    secretario: '',
    tipoDocSecr: 'Pasaporte',
    docSecretario: '',
    tesorero: '',
    tipoDocTeso: 'Pasaporte',
    docTesorero: '',
    observaciones: '',
    terminos: '',
  };

  constructor(private sendServices: SendEmailService) {
  }

  ngOnInit() {
  }

  onSubmit(nCorp: FormInterface) {

    this.submitted = true;
    console.log(this.nCorporacion);

    this.sendServices.sendEmail(nCorp).subscribe(res => {
      console.log('AppComponent Success', res);
    }, error => {
      console.log('AppComponent Error', error);
    });
  }

}

the form.service.ts service is:

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Observable } from 'rxjs';
import { of } from 'rxjs';

import { SendEmailService, FormInterface } from '../services/form.service';

@Component({
  selector: 'likuid-formulario',
  templateUrl: './formulario.component.html',
  styles: ['./formulario.component.scss']
})

export class FormularioComponent implements OnInit {

  submitted = false;

  nCorporacion: FormInterface = {
    nombre: '',
    telefono: '',
    telefonoAlt: '',
    email: '',
    whatsapp: '',
    corporacion: '',
    corporacionAlt: '',
    sufijo: 'Corp',
    ein: 'si',
    dirVirtual: true,
    dirCorporacion: '',
    ciudadCorporacion: '',
    estadoCorporacion: '',
    cpCorporacion: '',
    presidente: '',
    tipoDocPre: 'Pasaporte',
    docPresidente: '',
    vicepresidente: '',
    tipoDocVice: 'Pasaporte',
    docVicepresidente: '',
    secretario: '',
    tipoDocSecr: 'Pasaporte',
    docSecretario: '',
    tesorero: '',
    tipoDocTeso: 'Pasaporte',
    docTesorero: '',
    observaciones: '',
    terminos: '',
  };

  constructor(private sendServices: SendEmailService) {
  }

  ngOnInit() {
  }

  onSubmit(nCorp: FormInterface) {

    this.submitted = true;
    console.log(this.nCorporacion);

    this.sendServices.sendEmail(nCorp).subscribe(res => {
      console.log('AppComponent Success', res);
    }, error => {
      console.log('AppComponent Error', error);
    });
  }

}

line 3 of formulario.component.html is:

<form class="container-fluid" #nCorp="ngForm" (ngSubmit)="onSubmit(nCorporation)" novalidate>

the php to send the data is:

<?php

function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}

if(is_ajax() ) {

    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);


    $nombre = $request->nombre;
    $telefono = $request->telefono;
    $telefonoAlt = $request->telefonoAlt;
    $email = $request->email;
    $whatsapp = $request->whatsapp;
    $corporacion = $request->corporacion;
    $corporacionAlt = $request->corporacionAlt;
    $sufijo = $request->sufijo;
    $ein = $request->ein;
    $dirVirtual = $request->dirVirtual;
    $dirCorporacion = $request->dirCorporacion;
    $ciudadCorporacion = $request->ciudadCorporacion;
    $estadoCorporacion = $request->estadoCorporacion;
    $cpCorporacion = $request->cpCorporacion;
    $presidente = $request->presidente;
    $tipoDocPre = $request->tipoDocPre;
    $docPresidente = $request->docPresidente;
    $vicepresidente = $request->vicepresidente;
    $tipoDocVice = $request->tipoDocVice;
    $docVicepresidente = $request->docVicepresidente;
    $secretario = $request->secretario;
    $tipoDocSecr = $request->tipoDocSecr;
    $docSecretario = $request->docSecretario;
    $tesorero = $request->tesorero;
    $tipoDocTeso = $request->tipoDocTeso;
    $docTesorero = $request->docTesorero;
    $observaciones = $request->observaciones;

    $titulo = 'Datos de la solicitud: ';
    $para = '[email protected]';
    $asunto = 'Solicitud de Creacion de Corporacion';

    $formulario =   "<p>$titulo</p>
                    <hr>
                    <p><strong>Nombre:            </strong>$nombre</p>
                    <p><strong>Telefono:          </strong>$telefono</p>
                    <p><strong>Telefono Alt:      </strong>$telefonoAlt</p>
                    <p><strong>E-mail:            </strong>$email</p>
                    <p><strong>Whatsapp:          </strong>$whatsapp</p>
                    <hr>
                    <p><strong>Corporacion:       </strong>$corporacion</p>
                    <p><strong>Nombre Alt:        </strong>$corporacionAlt</p>
                    <p><strong>Sufijo:            </strong>$sufijo</p>
                    <p><strong>EIN:               </strong>$ein</p>
                    <hr>
                    <p><strong>Direccion Virtual: </strong>$dirVirtual</p>
                    <p><strong>Direccion:         </strong>$dirCorporacion</p>
                    <p><strong>Ciudad:            </strong>$ciudadCorporacion</p>
                    <p><strong>Estado:            </strong>$estadoCorporacion</p>
                    <p><strong>CP:                </strong>$cpCorporacion</p>
                    <hr>
                    <p><strong>Presidente:        </strong>$presidente</p>
                    <p><strong>Tipo Documento:    </strong>$tipoDocPre</p>
                    <p><strong>Numero:            </strong>$docPresidente</p>
                    <p><strong>Vicepresidente:    </strong>$vicepresidente</p>
                    <p><strong>Tipo Documento:    </strong>$tipoDocVice</p>
                    <p><strong>Numero:            </strong>$docVicepresidente</p>
                    <p><strong>Scretario:         </strong>$secretario</p>
                    <p><strong>Tipo Documento:    </strong>$tipoDocSecr</p>
                    <p><strong>Numero:            </strong>$docSecretario</p>
                    <p><strong>Tesorero:          </strong>$tesorero</p>
                    <p><strong>Tipo Documento:    </strong>$tipoDocTeso</p>
                    <p><strong>Numero:            </strong>$docTesorero</p>
                    <hr>
                    <p><strong>Observaciones:     </strong>$observaciones</p>";

    $body = "<html><body>";
    $body .= "$formulario";
    $body .= "</body></html>";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    $headers .= "From: $email\n";

    mail($para, $asunto, $body, $headers);

    $response_array['status'] = 'success';
    $response_array['from'] = $para;
    echo json_encode($response_array);
    echo json_encode($para);
    header($response_array);
    return $para;

} else {
    die("Sin Permisos");
}

It has been a few days trying to solve this, if someone knows an alternative will also be welcome.

    
asked by LikuidKu 12.05.2018 в 20:29
source

0 answers