0

I'm making a Angular 6 webapp. i want to send a mail with the information from the form. I made a php function that does this, but I don't know how to link it in my Angular code, i need to access my php code in my Angular.

This is my form

<form class="example-form">
      <h1>Contacteer ons</h1>
      <p>Tel:02/411.74.95<br>
        info@atlemo.be

       </p>
       <p>Of vul de onderstaarde form in.</p>
        <table class="example-full-width" cellspacing="0"><tr>
          <td><mat-form-field class="example-full-width">
            <input matInput placeholder="Naam" id="naamContact" name="naamContact"[(ngModel)]="model.naamContact"  [formControl]="naamFormControl"
            [errorStateMatcher]="matcher"  required >
            <mat-error *ngIf="naamFormControl.hasError('required')">
              Naam is <strong>Verplicht</strong>
            </mat-error>
            <mat-error *ngIf="naamFormControl.hasError('whitespace')">Opgepast!! "SPATIES"
            </mat-error>
          </mat-form-field></td>
          <td><mat-form-field class="example-full-width">
            <input matInput placeholder="Voornaam"id="voornaamContact" name="voornaamContact" [(ngModel)]="model.voornaamContact"   [formControl]="voorNaamFormControl"
            [errorStateMatcher]="matcher"    required>
            <mat-error *ngIf="voorNaamFormControl.hasError('required')">
              voornaam is <strong>Verplicht</strong>
            </mat-error>
            <mat-error *ngIf="voorNaamFormControl.hasError('whitespace')">Opgepast!! "SPATIES"
            </mat-error>
          </mat-form-field></td>
        </tr></table>
        <mat-form-field class="example-full-width" floatLabel="never">
            <span matPrefix>+32 &nbsp;</span>
            <input type="text"  matInput placeholder="Tel" id="telContact" name="telContact"[(ngModel)]="model.telContact"  maxlength=13 [formControl]="telFormControl"
            [errorStateMatcher]="matcher"    numbersOnly required>
            <mat-icon matSuffix>phone</mat-icon>
            <mat-error *ngIf="telFormControl.hasError('required')">
              Telefoonnr is <strong>Verplicht</strong>
            </mat-error>
            <mat-error *ngIf="telFormControl.hasError('whitespace')">Opgepast!! "SPATIES"
            </mat-error>
            <mat-error *ngIf="telFormControl.hasError('smallerdanTen')">10 Cijfers ingeven!!
            </mat-error>
          </mat-form-field>

          <mat-form-field class="example-full-width" floatLabel="never">
                <input type="email"  matInput placeholder="email" id="emailContact" [(ngModel)]="model.emailContact"  name="emailContact" [formControl]="emailFormControl"
                [errorStateMatcher]="matcher"     required>
                <mat-icon matSuffix>alternate_email</mat-icon>
                <mat-error *ngIf="emailFormControl.hasError('required')">
                  email is <strong>Verplicht</strong>
                </mat-error>
                <mat-error *ngIf="emailFormControl.hasError('whitespace')">Opgepast!! "SPATIES"
                </mat-error>
                <mat-error *ngIf="emailFormControl.hasError('email')">ongeldig email structuur (abc@def.ghi) 
                </mat-error>
              </mat-form-field>
              <div class="row">

</div>
 <mat-form-field floatLabel="never">
        <mat-label>Onderwerp</mat-label>
        <mat-select [(value)]="selectedOnderwerp" name="onderwerpContact" [(ngModel)]="model.onderwerpContact" id="onderwerpContact" [formControle]="onderwerpFormControl"   [errorStateMatcher]="matcher"  required>

          <mat-options value="Inschrijving" >Inschrijving</mat-options>
          <mat-options value="Bestelling">Bestelling</mat-options>
          <mat-options value="Wedstrijd">Wedstrijd</mat-options>
          <mat-options value="Verzekeing/Ongeval">Verzekeing/Ongeval</mat-options>
          <mat-options value="Andere">Andere</mat-options>
        </mat-select>
        <mat-error *ngIf="onderwerpFormControl.hasError('required')">
          Kies een <strong>onderwerp</strong>
        </mat-error>
        <mat-error *ngIf="onderwerpFormControl.hasError('whitespace')">
        </mat-error>
      </mat-form-field>


  <mat-form-field appearance="fill" class="example-full-width">
    <textarea matInput placeholder="vraag of commentaar " name="commentaarContact" [(ngModel)]="model.commentaarContact"  id="commentaarContact" [formControle]="comentaarFormControl"   [errorStateMatcher]="matcher"  required></textarea>
    <mat-error *ngIf="comentaarFormControl.hasError('required')">
     Er is geen commentaar of vraag ingegeven.
    </mat-error>
    <mat-error *ngIf="comentaarFormControl.hasError('whitespace')">
    </mat-error>
  </mat-form-field>

      <button mat-raised-button class="example-full-width" (click)="checkform()" type="submit" color="primary">verstuur bericht</button>

      </form>

this is my php

<?php
  public function contact()
  {
      header("Access-Control-Allow-Origin: *");
      header("Access-Control-Request-Headers: GET,POST,OPTIONS,DELETE,PUT");
      header('Access-Control-Allow-Headers: Accept,Accept-Language,Content-Language,Content-Type');

      $formdata = json_decode(file_get_contents('php://input'), true);

      if( ! empty($formdata)) {

          $naam = $formdata['naamContact'];
          $voornaam = $formdata['voornaamContact'];
          $email = $formdata['emailContact'];
          $telefoonnr = $formdata['telContact'];
          $onderwerp = $formdata['onderwerpContact'];
          $commentaar = $formdata['commentaarContact'];

          $contactData = array(
              'naamContact' => $naam,
              'voornaamContact' => $voornaam,
              'emailContact' => $email,
              'telContact' => $telefoonnr,
              'onderwerpContact' => $onderwerp,
              'commentaarContact' => $commentaar,
              'created_at' => date('Y-m-d H:i:s', time())
          );

          $id = $this->api_model->insert_contact($contactData);

          $this->sendemail($contactData);

          $response = array('id' => $id);
      }
      else {
          $response = array('id' => '');
      }

      $this->output
          ->set_content_type('application/json')
          ->set_output(json_encode($response));
  }

  public function sendemail($contactData)
  {
      $message = '<p>Hi, <br />Some one has submitted contact form.</p>';
      $message .= '<p><strong>Naam: </strong>'.$contactData['naamContact'].'</p>';
      $message .= '<p><strong>Voornaam: </strong>'.$contactData['voornaamContact'].'</p>';
      $message .= '<p><strong>Email: </strong>'.$contactData['emailContact'].'</p>';
      $message .= '<p><strong>Tel nr: </strong>'.$contactData['telContact'].'</p>';
      $message .= '<p><strong>Opmerking of vraag: </strong>'.$contactData['commentaarContact'].'</p>';
      $message .= '<br />Thanks';

      $this->load->library('email');

      $config['protocol'] = 'sendmail';
      $config['mailpath'] = '/usr/sbin/sendmail';
      $config['charset'] = 'iso-8859-1';
      $config['wordwrap'] = TRUE;
      $config['mailtype'] = 'html';

      $this->email->initialize($config);

      $this->email->from($contactData['emailContact'], 'RSGiTECH');
      $this->email->to('lerys91@hotmail.com');
      $this->email->cc('info@atlemo.be');
      //$this->email->bcc('them@rsgitech.com');

      $this->email->subject('Contact Form');
      $this->email->message($message);

      $this->email->send();
  }
?>

Need to access my php code in contact.ts or html

Zendie
  • 1,176
  • 1
  • 13
  • 30
  • 1
    What are you getting? What are you expecting? Also, this is a huge code dump. Can you reduce your problem to under 10 lines of code that recreates the problem? – David Hempy Aug 28 '19 at 03:50
  • now it validates my form and tells me the form is valid. but the mail is not send. i need to be able to call or access the php file from my ts file so that i can run the function. after the falidation the send function should run, but that is not the case now. – Lerys Lodonou Aug 28 '19 at 04:01
  • Do you get some sort of error? Or are you saying the email just never shows up? If the latter, it is likely that your local machine is not configured to send mail correctly, which can sometimes be quite difficult. There's a great, easy solution in https://mailcatcher.me/ , which runs a local server to receive and show email with almost no configuration. Their docs show hot to set it up for pmp (and other languages) with just a few config settings. – David Hempy Aug 28 '19 at 04:12

2 Answers2

0

Maybe this StackOverflow answer can help you .
link : Angular 2 How to send mail PHP?

Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
0

you need to create an API between the 2 files , submit the form from angular , handle the request in php and fill in the variables . Read About https://angular.io/guide/http HttpClient in Angular

Ammar M Mashfj
  • 104
  • 1
  • 1
  • 10