Angular应用程序,express.js服务器,在前端通过表单发送电子邮件。
创始人
2024-10-30 15:00:47
0

下面是一个使用Angular和Express.js的示例应用程序,通过前端表单发送电子邮件:

  1. 前端Angular应用程序代码:
// app.component.ts

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  template: `
    

Send Email

`, }) export class AppComponent { email: string; message: string; constructor(private http: HttpClient) {} sendEmail() { const data = { email: this.email, message: this.message }; this.http.post('/send-email', data).subscribe(() => { console.log('Email sent'); }); } }
  1. Express.js服务器端代码:
// server.js

const express = require('express');
const nodemailer = require('nodemailer');

const app = express();
app.use(express.json());

app.post('/send-email', (req, res) => {
  const { email, message } = req.body;

  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: 'your-email@gmail.com',
      pass: 'your-password'
    }
  });

  const mailOptions = {
    from: 'your-email@gmail.com',
    to: email,
    subject: 'Test Email',
    text: message
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.error(error);
    } else {
      console.log('Email sent: ' + info.response);
      res.sendStatus(200);
    }
  });
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

请确保已安装以下依赖项:

  • Angular依赖项:@angular/core, @angular/common, @angular/forms, @angular/common/http
  • Express.js依赖项:express, nodemailer

要运行应用程序,您需要在终端中分别运行Angular应用程序和Express.js服务器。

相关内容

热门资讯

黑科技系统(wepoke透明黑... 黑科技系统(wepoke透明黑科技)wepoke有吗(WepoKe)本然真的有挂(有挂细节)-哔哩哔...
黑科技辅助(wepoke辅助插... 黑科技辅助(wepoke辅助插件)wepokeai代打技巧(wepoke)最初存在有挂(确实有挂)-...
黑科技计算(wepoke辅助插... 黑科技计算(wepoke辅助插件)wepower透视辅助效果(WepOke)一向存在有挂(有挂方略)...
黑科技数据(wepoke辅助插... 黑科技数据(wepoke辅助插件)wepower提高中牌率(wEpOke)最初是有挂(有挂方略)-哔...
黑科技工具(wepokeai代... 黑科技工具(wepokeai代打)wepoke智能ai什么意思(wepoke)真是是有挂(有挂规律)...
黑科技系统(wepoke智能a... 黑科技系统(wepoke智能ai)wepower有机器人吗(WEPOKE)素来是真的有挂(有挂存在)...
黑科技安装(wepoke透明黑... 黑科技安装(wepoke透明黑科技)wepoke机制(WEPoke)竟然真的有挂(有挂解密)-哔哩哔...
黑科技神器(wepoke真的有... 黑科技神器(wepoke真的有挂)wepoke软件规律(wEPoke)从前是真的有挂(有挂猫腻)-哔...
辅助黑科技(wepoke智能a... 大家肯定在之前或者中玩过辅助黑科技(wepoke智能ai)we辅助poker德之星(WepoKe)本...
黑科技科技(wepoke真的有... 黑科技科技(wepoke真的有挂)wepkoe有外挂么(WEPoke)往昔真的是有挂(有挂透明挂)-...