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服务器。

相关内容

热门资讯

微扑克的辅助工具苹果!微扑克a... 微扑克的辅助工具苹果!微扑克ai分析,(微扑克)其实真的有挂,教你教程(黑科技辅助器)小薇(透视辅助...
wepokeai插件!wepo... wepokeai插件!wepoke智能ai什么意思,(wepOKE)一贯是真的有挂,必备教程(黑科技...
德扑之星ai代打可靠!德扑之星... 德扑之星ai代打可靠!德扑之星用ai会封号嘛,(德扑)本来是真的有挂,wpk教程(黑科技攻略)德扑之...
德扑之星安卓模拟器!德扑之星a... 德扑之星安卓模拟器!德扑之星ai代打怎么弄,(德扑)真是真的是有挂,微扑克教程(黑科技黑科技)1、德...
aapoker俱乐部后台可以操... aapoker俱乐部后台可以操作吗!aa扑克辅助,(aapoker)切实是有挂,第三方教程(黑科技黑...
aapoker这个软件靠谱吗!... aapoker这个软件靠谱吗!aapoker辅助工具下载,(AAPoker)切实真的是有挂,总结教程...
aapoker俱乐部后台可以操... aapoker俱乐部后台可以操作吗!aapoker辅助,(aapoker)竟然有挂,微扑克教程(黑科...
aapoker外挂实测结果!a... aapoker外挂实测结果!aapoker记牌器,(AAPoker)都是真的是有挂,可靠教程(黑科技...
德扑ai软件!德扑之星ai代打... 德扑ai软件!德扑之星ai代打怎么弄,(德扑)切实有挂,必赢教程(黑科技方法)1、德扑之星ai代打怎...
aapoker透视辅助!aap... aapoker透视辅助!aapoker透视辅助器,(aApoker)切实是真的有挂,微扑克教程(黑科...