0

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: "[email protected]",
  from: "[email protected]",
  subject: "Hello Test",
  html: `<p style="font-weight: 600; background-color: #ff0000">Hello This is test email</p>`,
});

1
  • I misspelled the Question. It should be "How to send email from Angular v15 using @sendgrid/mail?"
    – jamcaan
    Commented May 10, 2023 at 21:22

1 Answer 1

2

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.