Cloud function SendGrid email [solved]

Hi There!

I tried to send emails via Moralis cloud function, But function execution is working but emails are not sending.

Followed this guide https://docs.moralis.io/moralis-server/tools/sending-email#email-template

Also created email template.

Please kindy advice us what is going on

1 Like

Hi @neil,

Could you provide your Cloud function code so that we know how you’re calling the email provider.

Also, make sure you added the template IDs in the Moralis configuration.

Lastly, Make sure the cloud function is correctly being called in the first place.

More information will be appreciated. :smiley:

Hi Malik!

Thank for the reply.

// Send email verification to user
Moralis.Cloud.define("sendVerificationEmailToUser", function (request) {
  Moralis.Cloud.sendEmail({
    to: "[email protected]",
    templateId: "d-2d253da8sd48ac42edaab2f9fa405ed6839",
    dynamic_template_data: {
      url: request.params.url
    }
  });
});

// Testing
Moralis.Cloud.define("sendEmailToUser", function (request) {
  Moralis.Cloud.sendEmail({
    to: request.user.get("email"),
    subject: "Fundamentals",
    html: "Pampamentally it does make sense https://youtu.be/xXrkgWDcd7c"
  });
});

Also this is how we call

const params = {
 url: "http://localhost:3000/profile/HQhGzba5nmPHCZOTKNUz20Zo/verify_email",
 };
 const sendVerificationEmail = await Moralis.Cloud.run(
 "sendEmailToUser",
 params
 )

Hi,
It looks like sendEmailToUser function doesn’t use that url param.

You may be able to find more info in Moralis Dashboard in logs:

@cryptokid I checked there is no error, the function is executed

The code works as expected for me right now. Please make sure your configuration is correct.

Let me know what you can find.

@malik yea the code is working but the email is not sending, I checked to send grid configurations all good, but emails are not sending.

Is your sendgrid account verified already?

Also, is the user initialised from Moralis side? Try using a test account and see if it works -

Moralis.Cloud.define("sendEmailToUser", function (request) {
  Moralis.Cloud.sendEmail({
    to: "[email protected]",
    subject: "Fundamentals",
    html: "Pampamentally it does make sense https://youtu.be/xXrkgWDcd7c"
  });
});

You can get test email ids over here - https://temp-mail.org/en/

Refresh your test email inbox on the website to see if you have the emails.

Let me know how it goes.

Hi All,

Finally, I make it works, Below are the step that I followed to make it works.

  1. Verify SendGrid with your domain name (add all the CNAME that SendGrid provide)
  2. Create New API access KEY on Sendgrid.
  3. Add SendGrid API key to Moralis Email Configuration section

Then it should work.

2 Likes

Hi @neil

Great job! Thanks for the detailed instructions. We really appreciate it :raised_hands:

You are Real BUIDLer :man_mechanic: :muscle:

1 Like

This message is not intended to reopen this thread, I am just confirming that neil’s solution worked for me. Without the domain verification I was not able to send messages.

In my case, I hosted my app as a static site, on a live domain (rather than localhost) so I had access to edit the DNS records for my domain name.
I created three CNAME records and copy/pasted the details that SendGrid provide on the authentication page. I then generated a new API and replaced the old one.

I found using the cloud function to send the verification email was not working, so after signing up the user in the front-end code, I called a new function

        sendEmailVerification = async () => {
        let user = Moralis.User.current(); 
        let userEmail = user.attributes.email;
        await Moralis.User.requestEmailVerification(userEmail)
          .then(() => {
            //user will get an email with a link. If the user clicks on the link his user get authenticated.
            console.log("Successfully sent email verification email");
          })
          .catch((error) => {
            // Show the error message somewhere
            alert("Error: " + error.code + " " + error.message);
          });
      };
2 Likes