function sendNewsletter() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var startRow = 2; // Skip the header row
var numRows = sheet.getLastRow() - 1; // Number of rows with data
var batchSize = 10; // Number of emails to send per batch
var maxEmailsPerDay = 500; // Daily limit
var maxEmailsPerWeek = 3500; // Weekly limit
var maxRetries = 5; // Maximum number of retries for exponential backoff
var dataRange = sheet.getRange(startRow, 1, numRows, 1); // Only one column needed
var data = dataRange.getValues();
var scriptProperties = PropertiesService.getScriptProperties();
// Reset properties for each run
scriptProperties.setProperty('LAST_PROCESSED_ROW', startRow - 1); // Start from the beginning
scriptProperties.setProperty('EMAILS_SENT_TODAY', 0); // Reset daily count
scriptProperties.setProperty('EMAILS_SENT_THIS_WEEK', 0); // Reset weekly count
scriptProperties.setProperty('LAST_RUN_DATE', new Date()); // Update last run date
var lastProcessedRow = parseInt(scriptProperties.getProperty('LAST_PROCESSED_ROW')) || startRow - 1;
var emailsSentToday = parseInt(scriptProperties.getProperty('EMAILS_SENT_TODAY')) || 0;
var emailsSentThisWeek = parseInt(scriptProperties.getProperty('EMAILS_SENT_THIS_WEEK')) || 0;
var emailsSent = 0; // Count emails sent in this session
// HTML email template
const htmlContent = `
Get the latest updates from StarklyTech – your source for all things mobile tech.
|
|
Your Go-To Guide For Mobile Tech
|
Top Story of the Week
The Latest Breakthrough in Mobile Technology
By John Doe | August 28, 2024
Read More
|
|
|
5 Ways to Improve Your Smartphone Photography
Discover the top tips to enhance your photos and capture stunning moments...
Read More
|
|
|
The Future of 5G: What You Need to Know
Understand how 5G technology is set to revolutionize mobile communications...
Read More
|
|
|
Top 10 Tech Gadgets of 2024
A roundup of the must-have tech gadgets of the year, perfect for any enthusiast...
Read More
|
|
|
Understanding AI in Mobile Apps
Explore how AI is transforming mobile applications, making them smarter...
Read More
|
|
|
Smartphone Security: Tips and Tricks
Learn the best practices to secure your smartphone from potential threats...
Read More
|
|
|
Virtual Reality: The Next Big Thing in Mobile Gaming
Discover how virtual reality is set to change the landscape of mobile gaming...
Read More
|
|
|
Stay Connected with Us
|
Visit Our Website
|
© 2024 StarklyTech. All rights reserved.
You are receiving this email because you subscribed to receive newsletters and promotional materials from Starklytech.com.
Unsubscribe |
Update Preferences
|
`;
for (var i = lastProcessedRow + 1; i < startRow + numRows; i++) { // Fix here
if (emailsSentToday >= maxEmailsPerDay || emailsSentThisWeek >= maxEmailsPerWeek) {
Logger.log('Daily or weekly email limit reached.');
break;
}
var emailAddress = data[i - startRow][0]; // First column holds the email address
try {
MailApp.sendEmail({
to: emailAddress,
subject: 'Essential maintenance tips for your mobile device',
htmlBody: htmlContent,
name: 'StarklyTech'
});
emailsSent++;
emailsSentToday++;
emailsSentThisWeek++;
scriptProperties.setProperty('LAST_PROCESSED_ROW', i);
scriptProperties.setProperty('EMAILS_SENT_TODAY', emailsSentToday);
scriptProperties.setProperty('EMAILS_SENT_THIS_WEEK', emailsSentThisWeek);
Utilities.sleep(500); // Delay to avoid hitting email sending rate limits
} catch (e) {
Logger.log('Failed to send email to: ' + emailAddress + '. Error: ' + e.toString());
// Implement retry logic with exponential backoff
var retryCount = 0;
while (retryCount < maxRetries) {
try {
Utilities.sleep(Math.pow(2, retryCount) * 1000); // Exponential backoff
MailApp.sendEmail({
to: emailAddress,
subject: 'Essential maintenance tips for your mobile device',
htmlBody: htmlContent,
name: 'StarklyTech'
});
emailsSent++;
emailsSentToday++;
emailsSentThisWeek++;
scriptProperties.setProperty('LAST_PROCESSED_ROW', i);
scriptProperties.setProperty('EMAILS_SENT_TODAY', emailsSentToday);
scriptProperties.setProperty('EMAILS_SENT_THIS_WEEK', emailsSentThisWeek);
break;
} catch (e) {
retryCount++;
Logger.log('Retry ' + retryCount + ' failed for email to: ' + emailAddress);
}
}
}
}
Logger.log('Emails sent: ' + emailsSent);
}