我正在为我的小型培训项目制作联系表单。正在接收电子邮件,但似乎不正确。似乎我是从同一个电子邮件帐户发送给自己的
我正在使用nodemailer和Angular 。
这是sendEmail函数:
async function sendEmail(user,callback){
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port:587,
service: `gmail`,
auth:{
async function sendEmail(data,callback){
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port:587,
service: `gmail`,
auth:{
user : `youremailwithoutprovider`,
pass: `your 3rd party app Google password`
}
})
const mailOptions = {
sender: data.email,
to: `AUTH USER SHOULD BE HERE`,
subject: data.subject,
query: data.query,
html: `
<h2>From:</h2>
<div>${data.email}</div>
<h2>Subject:</h2>
<div>${ data.subject}</div>
<h2>Query:</h2>
<div>${ data.query}</div>
`
}
let info = await transporter.sendMail(mailOptions)
callback(info)
}
module.exports = {
sendEmail
}
这是服务器中的控制器:
emailController.post(`/contact-us`, (req,res) => {
console.log(`request came`)
let user = req.body
try {
sendEmail(user,callback => {
})
} catch (error) {
res.status(400).json({error:error.message})
}
res.end()
})
这是整个联系我们组件:
import { Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ContactUsService } from './contact-us.service';
@Component({
selector: 'app-contact-us',
templateUrl: './contact-us.component.html',
styleUrls: ['./contact-us.component.css']
})
export class ContactUsComponent {
errors: any;
constructor(private fb: FormBuilder,private emailService: ContactUsService, private router: Router ){}
contactUs = this.fb.group({
name: [``,[Validators.required, Validators.minLength(5)]],
subject: [``, [Validators.required, Validators.maxLength(20)]],
email: [``,[Validators.email, Validators.required]],
query: [``, [Validators.required, Validators.minLength(15)]]
})
sendEmail(){
let user = {
name : this.contactUs.get(`name`)?.value,
email : this.contactUs.get(`email`)?.value,
subject : this.contactUs.get(`subject`)?.value,
query: this.contactUs.get(`query`)?.value,
}
this.emailService.sendEmail(user).subscribe({
next: () => this.router.navigate([`/`]),
error:(err)=> {
this.errors = err.error?.error
}
})
this.contactUs.reset()
}
}
这是Angular 的服务:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { IQuery } from '../interfaces/query';
const API_CONTACT_US_EMAIL = `http://localhost:3000/contact-us`
@Injectable({
providedIn: 'root'
})
export class ContactUsService {
query : null | IQuery | undefined
constructor(private http: HttpClient, private router: Router) { }
sendEmail(data: {}){
return this.http.post<IQuery>(`${API_CONTACT_US_EMAIL}`, data)
}
}
这一切都是工作,表格正在填写正确,电子邮件收到,只是发件人是错误的gmail -这是相同的电子邮件地址。
您可以看到,我在服务器上使用过几次console.log,从它们那里似乎提取了适当的数据,但我认为在nodemailer中发生了一些事情。
request came
Email sent
我错过了什么?
谢谢
1条答案
按热度按时间dm7nw8vv1#
这就是我如何更新sendEmail函数来展示我对电子邮件的良好体验。不是最漂亮的,但你可以改变css,无论你觉得合适:
这也是我在gmail和nodemailer上发现的最新情况,现在您需要启用2FA并创建一个单独的应用程序密码以放入节点身份验证/传输中。