Sending Free Emails Using Google Apps Script

Sending Free Emails Using Google Apps Script

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:

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!

 

Related Posts

Leave a Reply

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