The shift in Google's security infrastructure for the Gmail service has created new challenges for the software development community. Sending emails via the SMTP protocol is no longer just about logging in with a basic password. Since Google ended support for "less secure apps" in 2025, the use of App Passwords or OAuth 2.0 has become a mandatory standard.
This tutorial has been updated by Tan Phat Digital for 2026, providing a detailed look at how to implement Gmail sending in 5 of today's most popular programming languages.
Background Gmail Security and Authentication Platform 2026
For successful integration, your Gmail account must meet the following prerequisites:
2-Step Verification: Required to be enabled in Google Account security settings.
App Password: A unique 16-character code used for applications that do not support direct login.
Standard server parameters:
Address:
smtp.gmail.comPort: 587 (STARTLS required) or 465 (SSL/TLS required).
SPF certificate, DKIM, DMARC: For high volume sending (>5,000 emails/day), Google requires these records to be configured to authenticate the sending domain.
1. Deployed With Node.js (Nodemailer)
Node.js is the top choice for applications that need high asynchronous processing speed. The Nodemailer library makes configuration extremely simple.
JavaScript
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'xxxx xxxx xxxx xxxx' // 16-character application password
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test Website 2026 - Tan Phat Digital',
text: 'Email content sent from Node.js'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) return console.log(error);
console.log('✅ Email sent successfully!');
});
2. Deploying With Python (smtplib)
Python provides the powerful smtplib library for manual SMTP connection management, or libraries such as yagmail for code simplification.
Python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_gmail_2026():
sender = "[email protected]"
receiver = "[email protected]"
app_pass = "xxxx xxxx xxxx xxxx"
msg = MIMEMultipart()
msg["From"] = sender
msg = receiver
msg = "Gmail integration 2026 - Tan Phat Digital"
msg.attach(MIMEText("Python application sent", "plain"))
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender, app_pass)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
print("✅ Python email sent successfully!")
except Exception as e:
print(f"❌ Error: {e}")
send_gmail_2026()
3. Deployment With Go (Gomail)
Go (Golang) is popular for its outstanding concurrency performance through Goroutines.
Go
package main
import (
"fmt"
"gopkg.in/mail.v2"
)
func main() {
m := mail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Subject", "Go SMTP 2026 - Tan Phat Digital")
m.SetBody("text/plain", "Email sent from Go backend system")
d := mail.NewDialer("smtp.gmail.com", 587, "[email protected]", "your_app_password")
if err := d.DialAndSend(m); err!= nil {
fmt.Println("❌ Error sending mail:", err)
} else {
fmt.Println("✅ Go has successfully sent email!")
}
}
4. Deployment With Ruby (Mail Gem)
Ruby on Rails often integrates ActionMailer, but for small scripts, the mail gem is the most elegant solution.
Ruby
require 'mail'
Mail.defaults do
delivery_method :smtp, {
address: 'smtp.gmail.com',
port: 587,
user_name: '[email protected]',
password: 'xxxx xxxx xxxx xxxx',
authentication: 'plain',
enable_starttls_auto: true
}
end
begin. begin
Mail.delivered by
to '[email protected]'
from '[email protected]'
subject 'Ruby Mail 2026 - Tan Phat Digital'
body 'Email content sent from Ruby script'
end
puts "✅ Ruby sent email successfully!"
rescue => e
puts "❌ Error: #{e.message}"
end
5. Deployment With PHP (PHPMailer)
PHPMailer is still the gold standard for PHP websites, especially when deployed on Shared Hosting services.
PHP
isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_app_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Tan Phat Digital');
$mail->addAddress('[email protected]');
$mail->Subject = 'PHP SMTP 2026 - Tan Phat Digital';
$mail->Body = 'Email content sent via PHPMailer 2026';
$mail->send();
echo '✅ PHP sent email successfully!';
} catch (Exception $e) {
echo "❌ Error: {$mail->ErrorInfo}";
}
?>
Solving Common SMTP Errors
Based on troubleshooting experience at Tan Phat Digital, below are common errors and how to fix them:
Error 535 (Authentication Failed):
Cause: Incorrect application password or 2FA is disabled.
Solution: Regenerate a new 16-character code and check the 2FA status of your Google account.
Error 534 (Security Settings):
Cause: Google detected a strange login from a new IP address or the authentication mechanism was too weak.
Solution: Log in to the browser on the same server to verify your identity or switch to using OAuth2.
Error 421 (Service Unavailable):
Cause: Simultaneous connection limit exceeded or IP rate limit temporarily exceeded.
Solution: Implement the "Exponential Backoff" algorithm to resend after an increasing timeout period.
Managing Sending Limits and Queue Strategies
Google applies strict sending limits to combat spam:
Personal Gmail account: Limit 500 emails or 500 recipients per day.
Google Workspace account: Limit 2,000 emails/day and up to 10,000 total recipients.
Optimal strategy from Tan Phat Digital:
To handle large volumes of mail without crashing your website, we recommend using a queuing system:
Node.js: Use BullMQ combined with Redis to manage mail sending jobs, support automatic retry when encountering errors.
Python: Use Celery to push email sending tasks into the background, helping to free up resources for the main processing thread of the website.
Golden rule: Keep sending frequency below 20 emails/hour for new accounts and gradually increase (warm-up) over 45-90 days to build domain reputation.
Advanced Security: Secrets Management
In 2026, saving App Passwords directly in an .env file or source code is considered a serious security risk.
Recommended implementation:
Use Secret Manager: Integrate services like Google Secret Manager or Doppler to retrieve application passwords directly into virtual machine memory upon launch, avoiding leaks through system logs.
Least Privilege: Only grant Secret access to Service Accounts necessary to perform the task of sending mail.
Static data encryption: Ensure any SMTP credentials are encrypted with master keys (such as AES-256) before storing.
Gmail API vs. SMTP: Which Choice Is For You?
Tan Phat Digital provides a comparison table to help you make the right technical decision:
SMTP (Traditional Protocol):
Advantages: Easy to install, supported in all languages, no complicated application registration required.
Suitable: Small-scale website, simple notification email, old system (Legacy).
Gmail API (Modern Protocol):
Advantages: Faster speed thanks to reduced handshake steps, high security with OAuth2 token, supports in-depth email status tracking.
Suitable for: SaaS applications, integrated email marketing systems, enterprise-level security requirements.Compare Email Transmission Performance
To help you choose the appropriate language for your website, Tan Phat Digital summarizes the following technical characteristics:
Node.js (Nodemailer): Processing performance Highest I/O efficiency thanks to Event-driven model. Very suitable for websites that need to send instant email notifications.
Go (Gomail): Extremely good mass sending ability through Worker Pool. This is the optimal choice for large-scale newsletter sending systems.
Python (smtplib): Source code is easy to maintain, library supports processing rich email content. Suitable for websites integrating AI or data analysis.
PHP (PHPMailer): Best compatibility with all types of server infrastructure today. A safe choice for WordPress or Laravel websites.
Ruby (Mail gem): DSL syntax is close to natural language, helping to speed up project development.
To integrate sending Gmail into your website in 2026 to be most effective and safe, you need to note:
Information security: Absolutely do not save passwords application password directly in the source code. Use the
.envfile or Secret managers.Queue management (Queue): For websites with a large number of users, push mail sending tasks into the queue (like Redis, RabbitMQ) to avoid crashing the application and exceeding Google's sending limit.
Use STARTTLS: Always prioritize port 587 to ensure The connection is encrypted according to modern standards.
Error tracking (Logging): Always record email sending logs so that you can control and promptly handle errors arising from the Google server.
Mastering the Gmail integration process not only enhances the user experience but also affirms the professionalism in the technical operation of the website.
Share








