Error sending message: Object { headers: {…}, status: 500, statusText: "Internal Server Error", url: "http://localhost:3000/invia-email", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:3000/send-email: 500 Internal Server Error", error: "Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https: //support.google.com/mail/?p=BadCredentials cm42-20020a170906f5aa00b009ff8f199f21sm1832983ejd.19 - gsmtp" } error in console. chatgpt tells me "Less secure login: If you are using a Gmail account, you may need to enable "Less secure login" in your Gmail account. Go to this page and make sure "Less secure app login" is enabled." Do you know a trick?
server.js const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
const port = 3000; // Puoi scegliere la porta che preferisci
// Middleware per consentire la comunicazione con il front-end
app.use(express.json());
// Configura il trasportatore Nodemailer (sostituisci con le tue informazioni)
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: '123456',
},
});
// Definisci la rotta per l'invio delle email
app.post('/invia-email', (req, res) => {
const { destinatario, oggetto, testo } = req.body;
const mailOptions = {
from: '[email protected]',
to: destinatario,
subject: oggetto,
text: testo,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return res.status(500).send(error.toString());
}
res.status(200).send('Email inviata con successo: ' + info.response);
});
});
// Avvia il server sulla porta specificata
app.listen(port, () => {
console.log(`Server in ascolto sulla porta ${port}`);
});
component angular import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-contatti',
templateUrl: './contatti.component.html',
styleUrls: ['./contatti.component.css']
})
export class ContattiComponent {
constructor(private http: HttpClient) {}
inviaMessaggio(formData: FormData): void {
this.http.post('http://localhost:3000/send-email', {
nome: formData.get('name'),
email: formData.get('email'),
messaggio: formData.get('message')
}).subscribe(
data => {
// Gestisci la risposta dal server (potrebbe essere un messaggio di conferma, ad esempio)
console.log(data);
},
error => {
console.error('Errore durante l\'invio del messaggio:', error);
}
);`your text`
}
onSubmit(event: Event): void {
event.preventDefault();
const formData = new FormData(event.target as HTMLFormElement);
this.inviaMessaggio(formData);
}
}