SendGrid Setup - Send Email To ETH Adress

Ok, unfortunately, this is not as easy as described in the tutorial, as SendGrid setup is somewhat more complex. I set up a single sender verification, and there are no more errors from the dapp side of life. Also the moralis database works as intended. But how to implement and verify sendgrid correctly?


as you can see here, SendGrid asks for the language that we want to integrate the service to, I just went with node.js, generated and API key, inserted it into moralis cloud function, but the email never arrives.

Did someone integrate this successfully and can point into the right direction?

Thanks a lot :pray:

old

UPDATE:
I redid everything, spin up a new server, spinned up ganache gain. now the errors do not throw. still waiting for the e-mail though… will keep you updated.

Hi everyone,

I am following @filip’s tutorial on how to send emails to users via moralis.
I keep getting this error:
AccountExistForMailAddress
no matter which email I input or with which account I log in.

any clues?

main.js
Moralis.initialize("VyXM3keXqHlBnaH5FUv8rT6Y9DDMCBBreVc2kyJK"); // Application id from moralis.io
Moralis.serverURL = "https://m0e24kwy0shp.moralis.io:2053/server"; //Server url from moralis.io

async function login() {
    try {
        user = await Moralis.Web3.authenticate();
        let email = document.getElementById("email_input").value;
        user.set("email", email);
        await user.save();
        Moralis.Cloud.run("sendEmail", {});
    } catch (error) {
        console.log(error);
    }
}


document.getElementById("login_button").onclick = login;

When you execute the function, do you see any email in your email log in sendgrid? (https://app.sendgrid.com/email_activity)

If so, what?

If not, could you share your moralis cloud code?

1 Like

Hello Filip,
thanks for replying.
No activity is logged in SendGrid.
Cloudfunction:

Moralis.Cloud.define("sendEmail", (request) => {
Moralis.Cloud.sendEmail({
      to: request.user.get('email'),
      subject: "Test Mail via Moralis and SendGrid",
      html: "<h1>Welcome to the Future of DApps!</h1> <br/> This is my first mail send via moralis and sendgrid!"
	})
})

Thomas, so not an expert with this but I’m fairly certain what the issue is. I have attempted to address this issue in the post I shared here of basically looking for guidance/improvement within the email section.

So it does say HTML and you would think that you can add things like
<h1> Welcome! </h1>
but it won’t work/send if you do that.

I have tried with double quotes, single quotes, back ticks, tried adding names, images, etc. whenever I do any of that it does not work/send.

So, if you try just basic words within the quotes (remove the code) it will work/send. For example:
"Welcome to the Future of DApps! This is my first email..."

Try vanilla and if sendGrid is set up correctly it will work.

If others have configured how to use HTML within emails, I’m all ears as I would like to make improvements for those.

Your cloud code works for me @Thomas. I received the email. Even with the HTML code @nev.

Can you try updating your server to the latest version? After that, please run your email function again and then check your moralis log to see if you find any error there. It should say something like

“Ran cloud function sendEmail for user …”

2 Likes

Thanks @filip

I have tried all that with older servers and haven’t tried since. Let me try it now with updated server.

Much appreciated!!!

BOOM! It worked!! How amazing is that! Thank you so much @filip
You guys are incredible!! :partying_face:

Hi @filip @Thomas I tried the SendGrid approach several times, but no email were sent and no error after calling the Moralis cloud function.

I generated the SendGrid API key with email sending permission as in the video updated to the latest server 0.0235.

Here are my cloud functions, but neither of them were working:

Moralis.Cloud.define("sendWelcomeEmail", (request) => {
  Moralis.Cloud.sendEmail({
    // Hardcoded from https://temp-mail.org/
    to: '[email protected]',
    templateId: "d-84e646524eb54d64944182a456f4c0e0",
    dynamic_template_data: {
      name: request.params.name,
    },
  });
});

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

and the cloud function call:

  async sendWelcome() {
    try {
      let cloudResponse = await Moralis.Cloud.run('sendWelcomeEmail', {
        email: '[email protected]',
        name: this.testUserName,
      });

      cloudResponse = await Moralis.Cloud.run('sendEmailToUser', {
        email: '[email protected]',
        name: this.testUserName,
      });

      console.info('User welcome email success:', cloudResponse);
    } catch (error) {
      console.error(
        'User welcome email error:' + error.code + ' ' + error.message
      );
    }
  }

The console output was: User welcome email success: undefined and no email were sent.

Thanks for all your hints and help in this in advance.

We will look into this and get back to you.

Thank you for your patience.

1 Like

Thanks @malik, I also keep you updated if I managed to make it working. Thanks in advance. :slightly_smiling_face:

Any chance to look into it so far? I have the same issue. simple sum cloud function is working, but SendGrid call seems do nothing even if I save the API Key. Thanks in advance.

1 Like

Have you tried to restart the server? Some other users had the same issue and they solve it by restarting the server. :face_with_monocle:

Carlos Z

Dear @thecil thanks for the tip, but these simple resolutions I have already tried like, restarting and generating new SendGrid keys.

It seems somehow the SendGrid call within Moralis code fail silently or we need something extra configuration in SendGrid which was not mentioned in the video. Stuck at this point :frowning: Thanks anyway.

Not an expert by any means, but you seem to call the email twice which is why I think the code is getting stuck. You are defining specific email in cloud function and then when you call it you yet again define specifics. Try changing the code in cloud function to say something like:

to: request.params.email

Basically what you did with the name and then define both the name and the email when you call it. I hope that’s helpful

Thanks @nev, my example above tried to eliminate all api knowledge and use hardcoded email properties (to, from, subject, body) and that call still not working. No error, but Moralis somehow not send out the message. Any other idea? @thecil @malik, thanks.

Dear @thecil @malik finally the documentation mentioned a key section:

From Email: this will appear as the “from” address on emails received by users (must be authorized by Sendgrid as a single sender or domain).

I used custom from email address than my registered one in sendgrid :slight_smile:

1 Like

I tried to figure out and ask several times how/if we’re able to also set a From Name in the emails headers, like:

From: MyApp <[email protected]>

but didn’t figure it out yet!

Dear @matiyin not tried, but my understanding is that the from is set via email settings on the server, and cannot be overridden. Will try it soon…

cool, let me know if you find out, would make my emails look more profi :slight_smile:

It seems Moralis sendEmail use from address authorized by Sendgrid as a single sender or domain (so your provided custom from parameter is overridden).

So to change that you need to configure your SendGrid Identity, and set your profi email address there.

I hope this helps :slight_smile: