Sending data dynamically does not work in Angular forms

I am facing an issue with dynamically capturing text and sending it to my email. While manually typing into TS onSendEmail, it works fine. I am using a free email hosting server, and all the required files are available in the assets folder.

HTML code

<form (ngSubmit)="onSendEmail(addEmailForm)" #addEmailForm ="ngForm">
  <fieldset>
    <legend>Contact us</legend>
    <label>Your name</label>  <br>
    <input ngModel  name="name" type="text" placeholder="Your name" size="20" required ><br> 
    <label>E-mail address</label>  <br>
    <input ngModel  name="email" type="text" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f0edf4f8e5f9f0d5f0edf4f8e5f9f0bbf6faf8">[email protected]</a>" size="20" required><br>
    <label>Subject</label>  <br>
    <input ngModel  name="subject" type="text" size="20"><br>
    <label>Body</label>  <br>
    <textarea ngModel  name="body" type="text" cols="40" rows="6" required></textarea><br>
    <button [disabled]="addEmailForm.invalid" >Send</button>
    </fieldset>
</form> 

Typescript code

import { HttpClient } from '@angular/common/http';
import { Component, OnInit, AfterViewInit } from '@angular/core';
import { NgForm } from '@angular/forms';


declare let Email: any;
import 'src/assets/smtp.js';  // available in assets folder


@Component({
  selector: 'app-map',
  templateUrl: './shops.component.html',
  styleUrls: ['./shops.component.css']
})
export class ShopsComponent implements OnInit, AfterViewInit {

  constructor(private http: HttpClient) { }

  ngOnInit(): void { }  

  body: any
  subject: any;
  email: any;
  name: any;
 

  onSendEmail(addEmailForm: NgForm) {
    Email.send({
      Host : "smtp.elasticemail.com",
      Username : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd6c2ded6dad2d7fbd3d4cfd6dad2d795d8d4d6">[email protected]</a>",
      Password : "mypassword",
      To : '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92ffebe1f7f1fdfcf6f7fff3fbfed2fafde6fff3fbfebcf1fdff">[email protected]</a>',
      Name: this.name,    
      From : this.email,     // when I write these manually the email gets sent
      Subject : this.subject,
      Body : this.body,
        }).then( 
    (message: any) => alert("E-mail sent, thank you.")
      );
  }

}


Answer №1

If you want to retrieve data from your form, you can accomplish this by using the following code:

  onSubmitForm(submittedForm: NgForm) {
let formData = submittedForm.value;
//...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to trigger the selection options in a dropdown menu with several buttons?

Is it possible to display the options from a select-option tag using a different button? I have either a div or another button. I want to be able to show the list of options from my select tag by clicking this button. Here is my select tag: <select&g ...

Enable the click functionality for a disabled MI TextField

I'm utilizing a disabled TextField in my project and updating it using React Hooks useState to modify the value property of the TextField. const [employee , setEmployee] = React.useState('') <TextField fullWidth ...

Exploring VueJS templating: Loading external templates

Being new to Vue.js, I have some experience with AngularJS where we used to load templates like this: template: '/sometemplate.html', controller: 'someCtrl' My question is how can we achieve something similar in Vue? Instead of embeddi ...

Refresh the jQuery Raty choice when clicked (jQuery)

Currently, I am incorporating the jQuery Raty plugin into a jQuery UI dialog to create a questionnaire-style format. Through custom jQuery scripting, I have devised an interactive interface where users are presented with a new question upon each selection ...

Load Angular modules dynamically

Is there a way to dynamically load a module (js, css and html) using a single directive at any point during the app's lifecycle? <my-module id="contacts"></my-module> The template for this directive looks like this <my-module id="con ...

Enhance Your NestJS Experience: Using Interceptors for Mapping and Error Handling

I'm looking for a NestJS interceptor that can log requests in all scenarios, including both successful executions and errors. Below is an example implementation: public intercept(context: ExecutionContext, next: CallHandler): Observable<any> { ...

Using infoWindows with multiple markers in Google Maps

i have developed a custom Google Maps application that pulls data from a CSV file. The functionality works well, but I am facing an issue with the infoWindow when looping through all the objects. It seems like the problem stems from the marker variable bei ...

Guide to implementing conditional inline styling within React

I am attempting to incorporate a condition for inline styling, but I keep receiving this error message: Error: The `style` prop requires a mapping from style properties to values, not a string Here is the code in question: <div className="todo&quo ...

Accessing website login - <div> and validating user entry

I am currently working on developing a basic login webpage, but I am facing issues with the rendering of the page. Below is the code I am using: function logIn(username, password){ var username = document.getElementById("username").value; var p ...

Tips for accurately extracting values from a decoded JSON

Hello, I am posting this query because I recently encountered an issue with json encoding in PHP. When using the json_encode() function, my original JSON data gets converted to strings instead of maintaining its original variable type. For instance, let&a ...

Showing button based on a particular value

I am trying to dynamically display a button based on the value of the sendSMS property for the logged-in user. I have added this property in the viewer model, which is connected to the user's base model. However, I am encountering difficulties with us ...

Transferring data between various stages of the user interface

As a newcomer to angularJs, I find myself facing an issue with two forms existing in different UI states (URLs) labeled as Step 1 and Step 2. The process requires filling up Step 1 and moving to the next step by clicking the NEXT button, which then leads t ...

The BooleanField component in V4 no longer supports the use of Mui Icons

In my React-Admin v3 project, I had a functional component that looked like this: import ClearIcon from '@mui/icons-material/Clear' import DoneIcon from '@mui/icons-material/Done' import get from 'lodash/get' import { BooleanF ...

The issue of Three js object turning dark after implementing Phong Shading

I have a project where I am displaying 3 cubes and toggling between MeshLambertMaterial and MeshPhongMaterial when pressing the A key. However, I'm facing an issue where the object turns black when applying the phong shading, even though I've cal ...

Issue with ChromeDriver in Selenium where button cannot be clicked

Currently, I am using Selenium with a selection of NuGet packages (DotNetSeleniumExtras.WaitHelpers.3.11.0 NUnit.3.12.0 NUnit3TestAdapter.3.16.1 Selenium.Support.3.141.0 Selenium.WebDriver.3.141.0) on Windows running Chrome (Version 80.0.3987.132 (Offi ...

JavaScript implementation of Content-disposition feature

Is it possible to use client-side JavaScript to make the browser behave the same way as when it encounters "Content-disposition: attachment; filename=..."? This would mean that the data for the file to be saved is only available on the client side. For ex ...

Encountering issues with Google authentication functionality while using AngularFire2

Currently in the process of setting up Google Authentication with Firebase and AngularFire2. The setup seems to be partially functional, however, there are some unusual behaviors that I am encountering. Upon the initial loading of the app, the Login button ...

Is there a way to access Validators directly from the formControl?

https://i.sstatic.net/Zj100.png https://i.sstatic.net/vtgxc.png https://i.sstatic.net/ak3tM.png When handling an email input field with validators such as required and email, the goal is to trigger validation on the input event to call an API only when ...

Introduce a specialized hierarchical data structure known as a nested Record type, which progressively ref

In my system, the permissions are defined as an array of strings: const stringVals = [ 'create:user', 'update:user', 'delete:user', 'create:document', 'update:document', 'delete:document&ap ...

HTML dynamic voting system

I'm new to coding in HTML and I have a specific challenge that I need help with. I want to display a value on my webpage that increases every time a user clicks a button. Below is the code I have written so far: <script type="text/javascript& ...