unfolding-io / nebulix

Nebulix, a Fast & Green Theme Based on Astro + Static CMS + Snipcart

Home Page:https://nebulix.unfolding.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Postmark configuration

getofmeland opened this issue · comments

Discussed in #30

Originally posted by getofmeland January 16, 2024
Hi All,

I am trying to get Postmark configured on my version of the site, when I try to submit an email I get the following message:

We could not send your message. Please try again.

I have checked my configuration and server configuration in the env and that all seems to be ok. When I monitor the edge function log, I am getting an alert that : Jan 16, 03:51:55 PM: info SLACK_TOKEN undefined

So I am not even sure if its attempting to go via Postmark.

Has anyone else managed to get this working

Hi @getofmeland,
can you conform your contact settings.
so run npm run dev and npm run cms-proxy-server
and go to:
http://localhost:4321/admin#/collections/config/entries/contact

what is the provider you see?
image

I see all 3 but Postmark is selected
image

I also have my postmark server api key listed in my .env file in the root

I am using netlify for my deployment if that helps

Postmark should work,
the SLACK_TOKEN undefined was just a console log on the slack endpoint.

Take a look at the response to see why postmark is not sending your email you can check your console if you are running netlify dev or if it is on netlify check the edge functions log

[contact-postmark] {
  ErrorCode: 412,
  Message: "While your account is pending approval, all recipient addresses must share the same domain as the 'F"... 143 more characters

Have tried Mailgun as well and I am getting literally the same message, and nothing in the edge functions logs either.

can you check the network response of the form submit?

So saying Mailgun configuration is missing from the .env file, I have checked my .env file and the configuration is there:

image

Not sure what I am missing...

Where are you hosting the site, netlify?

the error comes from an edge / deno function:


let {
  MAILGUN_API_KEY,
  MAILGUN_API_URL,
  MAILGUN_DOMAIN,
  FROM_EMAIL_ADDRESS,
  TO_EMAIL_ADDRESS,
} = Deno.env.toObject();

export default async (request, context) => {
  if (
    !MAILGUN_API_KEY ||
    !FROM_EMAIL_ADDRESS ||
    !TO_EMAIL_ADDRESS ||
    !MAILGUN_API_URL
  )
    return Response.json({
      error: "Missing MailGun configuration, please check your .env file.",
    });

Can you console.log the Deno.env.toObject() to see if they are available?

Having made a change it appears the script for mailgun isn't picking up the Environmental Variables:

Jan 20, 09:57:43 PM: info [Object: null prototype] {Jan 20, 09:57:43 PM: info DENO_REGION: "aws-eu-central-1",Jan 20, 09:57:43 PM: info UNITS: "metric",Jan 20, 09:57:43 PM: info SHOP_SLUG: "shop",Jan 20, 09:57:43 PM: info CURRENCY: "GBP",Jan 20, 09:57:43 PM: info WEBSITE_LANGUAGE: "en",Jan 20, 09:57:43 PM: info BLOG_SLUG: "blog",Jan 20, 09:57:43 PM: info MENU_SLUG: "menu",Jan 20, 09:57:43 PM: info DENO_DEPLOYMENT_ID: "CD_vAvvpuOZU_j2brJ96HFhCd5idqZku2-Y1XvGkVbw=",Jan 20, 09:57:43 PM: info NODE_VERSION: "18",Jan 20, 09:57:43 PM: info PORTFOLIO_SLUG: "work"Jan 20, 09:57:43 PM: info }

It appears that they are not even being pulled in via the script, as this returns undefined

So in addition to this, I added these into the Netlify Environmental Variables and they are pulled in...

When I submit the form I now get a new error:

uncaught exception during edge function invocation

It appeared to be linked to the encode function, I have updated the script to as follows:

import { encodeBase64 } from "https://deno.land/std/encoding/base64.ts";

let {
  MAILGUN_API_KEY,
  MAILGUN_API_URL,
  MAILGUN_DOMAIN,
  FROM_EMAIL_ADDRESS,
  TO_EMAIL_ADDRESS,
} = Deno.env.toObject();

// Get the environment variables as an object
const envObject = Deno.env.toObject();

// Log the object to the console
console.log(envObject);


export default async (request, context) => {

  if (
    !MAILGUN_API_KEY ||
    !FROM_EMAIL_ADDRESS ||
    !TO_EMAIL_ADDRESS ||
    !MAILGUN_API_URL
  )
    return Response.json({
      error: "Missing MailGun configuration, please check your .env file.",
    });

  const { email, name, message, topicEmail } = await request.json();

  if (!email || email === "") return Response.json({ error: "Missing email" });
  const authHeader = "Basic " + encodeBase64(`api:${MAILGUN_API_KEY}`);

  const headers = {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "application/x-www-form-urlencoded",
    "Access-Control-Allow-Headers": "Content-Type",
    "Access-Control-Allow-Methods": "OPTIONS,POST,GET",
    Authorization: authHeader,
  };

  let payload = new URLSearchParams();

  payload.append("from", FROM_EMAIL_ADDRESS);
  payload.append("to", topicEmail ? topicEmail : TO_EMAIL_ADDRESS);
  payload.append("h:Reply-To", email);
  payload.append("subject", `Contact Form: ${name} ${email}`);
  payload.append("text", message); 
  try {
    const resp = await fetch(
      `${MAILGUN_API_URL}/v3/${MAILGUN_DOMAIN}/messages`,
      {
        method: "POST",
        headers: headers,
        body: payload,
      },
    );

    let response = await resp.json();

    return Response.json({
      statusCode: 200,
      status: resp?.ok ? "ok" : "error",
      body: "Your message was sent successfully! We'll be in touch.",
    });
  } catch (e) {
    console.log("ERROR:", e);
    return Response.json({
      statusCode: 400,
      status: "error",
      error: "Mailgun  error",
    });
  }
};

Thanks!
strange that encode works local but not on netlify.
I have updated the function.