Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

JavaScriptProgramming

Sending Free Emails Using Google Apps Script

By Admin
January 20, 2025 4 Min Read
0

Email communication is an integral part of modern-day correspondence, but sometimes the high costs associated with email automation platforms can discourage small-scale developers or businesses from taking full advantage of these tools. Fortunately, there is an efficient and completely free alternative: Google Apps Script.

Google Apps Script is a cloud-based JavaScript platform that allows you to automate tasks across Google Workspace apps. By integrating this tool with a bit of PHP, you can seamlessly send emails programmatically at zero cost. This approach leverages the powerful Gmail API and ensures reliable email delivery without the need for expensive third-party services.

Follow this video for complete guidance:

https://www.youtube.com/watch?v=2xURJGqYYOA

Google App Script – Source Code

function doGet(e) {
  return ContentService.createTextOutput("This web app is working. Use POST requests to send emails.");
}

function doPost(e) {
  var data = JSON.parse(e.postData.contents);
  var recipient = data.recipient;
  var subject = data.subject;
  var htmlBody = data.htmlBody; // HTML content

  GmailApp.sendEmail(recipient, subject, '', { htmlBody: htmlBody });

  return ContentService.createTextOutput("Email sent successfully with HTML.");
}

PHP Script to Trigger Google App Script – Source Code

<?php
// Google Apps Script Web App URL
$webAppUrl = "https://script.google.com/macros/s/AKfycbzuPPShJWjUuXiiKoc-JmjmsDP5yZNojr6DfV3L_kY183MbKfozfK_EsqsI-j_siwvdhg/exec"; 

// Email data
$emailData = [
    "recipient" => "[email protected]", // Replace with recipient's email
    "subject" => "Free Email Using Google App Script",
    "htmlBody" => "<h1>Hello from Google App Script</h1><p>This is an <b>HTML</b> email sent using Google Apps Script!</p>"

];

// Initialize cURL session
$ch = curl_init($webAppUrl);

// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($emailData));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

// Execute cURL request
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Display response
echo $response;
?>

 

Why Use Google Apps Script for Emails?

  • Free of Cost: As long as you stay within the daily email sending limits of Gmail (which is 100 emails per day for free accounts and 1,500 for Google Workspace accounts), you won’t spend a single penny.
  • Customizability: You can fully control the email content, including HTML layouts for professional-looking messages.
  • Ease of Integration: Google Apps Script works effortlessly with other Google services, and by using PHP, you can integrate this functionality into any web application or backend service.
  • No Extra Infrastructure: Since Google handles the email sending and hosting, you don’t need to set up SMTP servers or maintain complicated configurations.

Setting Up the Google Apps Script

To get started, you’ll first need to create a Google Apps Script project. This script will handle email sending. Using the GmailApp class, you can specify the recipient, subject, and even the HTML body for the email.

Once the script is ready, you deploy it as a web app. Google Apps Script generates a unique URL that serves as an endpoint for interacting with your script via HTTP requests. This makes it possible to send email data from external sources—in our case, a PHP application.

Why Combine Google Apps Script with PHP?

PHP is one of the most widely used server-side languages for web development. By combining PHP with Google Apps Script, you can easily send email notifications or transactional emails directly from your website or application. For example:

  • An e-commerce site can send order confirmations.
  • A blog can send newsletters.
  • A support portal can send automated ticket updates.

The key advantage of this integration is that your PHP application doesn’t need to deal with SMTP configurations or email deliverability challenges. Instead, you leverage Google’s robust infrastructure for hassle-free email delivery.

Key Features of the Integration

  • HTML Email Support: Using Google Apps Script, you can include htmlBody to create visually appealing emails. HTML support enables formatting, inline images, and even interactive elements.
  • Secure Communication: The PHP application sends email data to the Google Apps Script endpoint over HTTPS, ensuring secure transmission.
  • Ease of Debugging: Both Google Apps Script and PHP provide detailed error logs, making it easy to debug and optimize the system.

Deploying Your Google Apps Script

When deploying the script as a web app, make sure to grant it the necessary permissions to access your Gmail account. You’ll also need to select “Anyone with the link can access” during deployment to enable external systems (like your PHP application) to interact with it.

Once deployed, your web app URL will act as the endpoint for sending POST requests containing email details. This URL will be integrated into your PHP application for seamless communication.

Sending Emails from PHP

The PHP code communicates with the Google Apps Script web app by sending a POST request with the necessary email data. It supports dynamic recipient addresses, custom subjects, and even fully formatted HTML content for professional-grade emails.

To ensure smooth operation, the cURL configuration in PHP handles any redirection responses from Google Apps Script. By following best practices like encoding data as JSON and specifying appropriate HTTP headers, the integration works flawlessly.

Benefits of This Approach

  • Reliability: Since Gmail powers the emails, delivery rates are high and spam issues are minimal.
  • Cost Efficiency: You’re essentially leveraging Google’s infrastructure for free.
  • Scalability: While free Gmail accounts have a lower daily sending limit, upgrading to Google Workspace increases this capacity significantly.

Google Apps Script offers a simple yet powerful way to send emails for free, and integrating it with PHP expands its utility for web developers and businesses. Whether you’re sending transactional emails or newsletter updates, this method provides a robust and cost-effective solution. By combining the strengths of Google’s ecosystem with PHP’s flexibility, you can automate email workflows without worrying about budget constraints or infrastructure complexities.

If you’re looking to streamline your email-sending processes without spending a fortune, this approach is definitely worth exploring. With just a bit of scripting, you’ll have a fully functional email automation system ready to meet your needs!

 

Tags:

Emailgoogle
Author

Admin

Follow Me
Other Articles
Previous

Creating a Realtime Location Tracker with JavaScript and OpenStreet Map

Next

Build a Simple Password Manager App Using jQuery

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme