如何使用 @sendgrid/mail 从 Angular v15 发送电子邮件?

huangapple go评论72阅读模式
英文:

How to send email form Angular v15 using @sendgrid/mail?

问题

I am trying to send an email from Angular v15 using @sendgrid; However, I am getting the errors below on importing the dependencies like : import { MailService } from "@sendgrid/mail";

我正在尝试使用@sendgrid在Angular v15中发送电子邮件;但是,当我导入依赖项时,出现以下错误:import { MailService } from "@sendgrid/mail";

I don't know if @sendgrid supports later versions of angular or not.

我不知道@sendgrid是否支持较新版本的Angular。


1 import * as https from 'https';

找不到模块'https'或其相应的类型声明。

1 import * as https from 'https';

Anyone can help will be appreciated.

任何人都可以帮助将不胜感激。

I installed the required dependencies like:

我已安装所需的依赖项,如下:

npm install @sendgrid/mail,
npm i @types/node

I was expecting to be able to send email template from Angular v15. I don't know why it's not working through or with angular. I even tried to get help from ChatGPT but no success.

我本来希望能够从Angular v15中发送电子邮件模板。我不知道为什么它在Angular中或与Angular一起使用时不起作用。我甚至尝试过寻求ChatGPT的帮助,但没有成功。

On the other hand, I tested to use Node.js without angular to send the email and surprisingly it works and I was able to receive the email.

另一方面,我测试了在没有Angular的情况下使用Node.js发送电子邮件,令人惊讶的是它可以工作,我成功收到了电子邮件。

英文:

I am trying to send an email from Angular v15 using @sendgrid; However, I am getting the errors below on importing the dependencies like : import { MailService } from "@sendgrid/mail";

I don't know if @sendgrid supports later versions of angular or not.

Cannot find module 'https' or its corresponding type declarations.

1 import * as https from 'https';

Anyone can help will be appreciated.

I installed the required dependencies like:

npm install @sendgrid/mail,
npm i @types/node

I was expecting to be able to send email template from Angular v15.
I don't know why its not working through or with angular. I even tried to get help from ChatGPT but no success.

On the other hand, I tested to use Node.js without angular to send the email and surprisingly it works and I was able to receive the email.

require("dotenv").config();

const sgMail = require("@sendgrid/mail");

sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const sendMail = async (msg) => {
  try {
    await sgMail.send(msg);
    console.log("Message sent successfully");
  } catch (error) {
    console.error(error);

    if (error.response) {
      console.error(error.response.body);
    }
  }
};

sendMail({
  to: "xyz@gmail.com",
  from: "ptj@hotmail.com",
  subject: "Hello Test",
  html: `<p style="font-weight: 600; background-color: #ff0000">Hello This is test email</p>`,
});

答案1

得分: 2

我不建议这样使用Angular,请记住Angular是一个具有Angular Universal的服务器端渲染的前端框架

仅在服务器端建议生成渲染内容,不要执行其他任务,并在前端发送电子邮件,这会将您的机密信息暴露给所有人并将您的机密方法暴露给所有人

对于这种类型的任务,建议构建后端应用程序/ API,并从前端调用这些API

因此,在前端,您应该进行HTTP POST调用,例如,在API应用程序的服务器端,执行使用sendgrid的方法

有很多用于执行此操作的JavaScript / TypeScript后端框架,如NestJS,Loopback,Express...

祝好运!

英文:

I would not recommend to use Angular like this, remember that Angular is a frontend framework with Server Side Rendering with Angular Universal

On the server side only is recommended to generate rendered content, not to do another tasks, and send a email on the frontend, is expose your secrets to everyone and expose your secret methods to everyone

For these kind of tasks is recommended to build a backend application / API and call these APIs from the front end

So on the front side you should make a http post call, for example, and on the server side in the API application, execute the methods with sendgrid

There are a lot of Javascript/Typescript backend frameworks to do this, like NestJS, Loopback, Express...

Good luck!

huangapple
  • 本文由 发表于 2023年5月11日 05:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定