#1 Cost Savings Hosting with Cloudflare Pages + Sending Form Data with Workers + SendGrid

Cloudflare Pages

Cloudflare Pages

Cloudflare Pages is a powerful and easy-to-use platform that has transformed the way developers create and deploy websites. Cloudflare Pages is a website creation and hosting service that offers an easy interface and seamless connection with major version control systems such as GitHub. Pages’ fast and dependable global network offers lightning-quick page load speeds and excellent performance for visitors all around the world. Additionally, the platform offers automated SSL encryption, providing safe communications for your website visitors. Cloudflare Pages distinguishes itself by its seamless interaction with other Cloudflare services like as CDN caching, DDoS protection, and intelligent routing. Cloudflare Pages is a wonderful alternative for bloggers and developers searching for a hassle-free and high-performance solution to exhibit their material because to its extensive feature set.

While it offers an intuitive interface and simplifies the website creation and deployment process, adapting to the platform may require some adjustment for those accustomed to traditional HTML workflows. However, the effort invested in learning the platform is well worth it, as Cloudflare Pages offers unparalleled performance, global scalability, automatic SSL encryption, and seamless integration with other Cloudflare services. By embracing the learning curve, traditional web designers can unlock the full potential of Cloudflare Pages and leverage its powerful features to enhance their websites’ speed, security, and overall user experience.

Cloudflare Pages

We went with Cloudflare Workers + SendGrid Because

With the combination of Cloudflare Pages and Cloudflare Workers, developers can leverage the simplicity and scalability of serverless computing to seamlessly integrate SendGrid’s email delivery capabilities into their applications. By utilizing the Worker’s event-driven architecture, developers can trigger email sending based on specific events or actions, such as user registrations or password resets. With the ability to customize email content dynamically and personalize messages for individual recipients, Cloudflare Workers enable developers to create highly tailored and engaging email experiences. Additionally, the global network infrastructure of Cloudflare ensures fast and reliable email delivery across the globe. Combining the flexibility of Cloudflare Workers and the robust features of SendGrid, developers can efficiently incorporate email functionality into their applications while maintaining high deliverability rates and ensuring a seamless user experience.

This is how you do it with Cloudflare Pages

To use Cloudflare Workers to send emails with SendGrid, you can follow these general steps:

  1. Set up a Cloudflare Workers environment: First, create a Cloudflare account if you don’t have one already. Set up a Workers environment by navigating to the Cloudflare Workers dashboard. This will provide you with the necessary tools and resources to create and manage your Worker scripts.
  2. Create a Worker script: Within the Workers dashboard, create a new script or open an existing one. This script will contain the code logic for sending emails using SendGrid. You can use JavaScript or any supported programming language for writing your Worker script.
  3. Integrate SendGrid API: To interact with the SendGrid API, you’ll need to obtain an API key from SendGrid. Sign up for a SendGrid account if you don’t have one, generate an API key, and keep it secure. Add the API key to your Worker script as a secure environment variable or use a secret management system provided by Cloudflare.
  4. Set up email parameters: Define the necessary email parameters, such as the sender’s email address, recipient’s email address, subject, and email content. You can use variables to customize the email content dynamically, such as adding user-specific information or generating personalized messages.
  5. Make an HTTP request to SendGrid: In your Worker script, make an HTTP request to the SendGrid API endpoint, passing the required email parameters as part of the request payload. Use the appropriate endpoint and HTTP method (e.g., POST) to send the email data to SendGrid.
  6. Handle the response: Once the request is sent to SendGrid, handle the response returned by the API. You can check for a successful response code (e.g., 200) to ensure the email was sent successfully. Handle any errors or exceptions that may occur during the process and provide appropriate feedback or error handling within your script.
  7. Deploy and test the Worker: After writing and saving your Worker script, deploy it to the Cloudflare Workers environment. Test the email functionality by triggering the Worker script through a relevant event or action in your application. Monitor the logs and verify that emails are being sent as expected.

HTML Form

Go ahead and create your HTML form – for the action url – enter your Cloudflare worker endpoint URL

Copy to Clipboard

Javascript (right before the closing </body> tag)

We are using jQuery 3.6.4 with our function to provide the slideUp and fadeIn and Out animations.

Copy to Clipboard

Your Worker should look like this

With the next code, pay close attention to some of the variables, make sure you enter in the recipient email, where you want the worker to send the contact form data.  When creating this worker -make sure to create an “Environmental Variable” called “SENDGRID_API_KEY”, this variable is being used in the post request. By doing this, your API key is kept from the end user being about to view it, which makes it nice and secure for sending form requests.

Copy to Clipboard

Utilizing Cloudflare Pages

Now comes for the fun part. You can go ahead and create your page, upload your HTML and test out your form. You can even test your form straight from your computer before deploying to production. The hurdle we came across was to ensure the following code is in your worker code. Otherwise you will get a CORS error, which basically means you are not allowing yourself to submit the form request. Otherwise, it only allows the local origin to the code to submit the request.

Copy to Clipboard

If you enjoyed this helpful article – let us know

AI, like any other technology, raises privacy issues. Make sure you are upfront about how you utilize AI on your website and that you are in compliance with any applicable data protection rules. Respect users’ privacy by giving them the choice to opt out of data gathering and processing.

Creator

Hubrizer logo - click here to go back to the home page

This article was created by the HUBRIZER. Team

Some content on this page has been influenced by humans and possibly generated by AI (Artificial Intelligence), be responsible with generative content.

Subscribe to receive our latest chatter

Receive FREE subscription to our latest updates and keep up with our chatter.

Add notice about your Privacy Policy here.

  • Read about New Event to Post Cloner for “The Events Calendar” Plugin

    New Event to Post Cloner for “The Events Calendar” Plugin

  • Read about AI and How to Benefit + What Not to Do

    AI and How to Benefit + What Not to Do

  • Read about What does it cost to have a website?

    What does it cost to have a website?