If you're having issues sending emails from your website (contact forms, reset password, etc.), setting up Gmail SMTP is a reliable alternative. This guide will walk you through the step-by-step process to use your Gmail account to send emails from your site.


 Requirements

  1. A Gmail or Google Workspace (G Suite) account

  2. Access to your web hosting control panel (cPanel or WHM)

  3. SMTP support in your script or app (most CMS platforms support this)

  4. "Less secure apps" access enabled (for Gmail personal accounts), or an App Password if 2FA is enabled


Step 1: Enable SMTP Access in Gmail

Option 1: For accounts without 2FA

  1. Go to: https://myaccount.google.com/security

  2. Scroll down to "Less secure app access"

  3. Turn Allow less secure apps to ON

⚠️ Google may phase this out. Use Option 2 if you can't find it.


Option 2: For accounts with 2FA enabled (recommended)

You’ll need to create an App Password.

  1. Go to: https://myaccount.google.com/security

  2. Make sure 2-Step Verification is enabled

  3. Click on App Passwords

  4. Select AppOther (Custom name) → e.g., My Website

  5. Click Generate

  6. Copy the 16-character app password


⚙️ Step 2: Configure SMTP in Your Application

Use the following SMTP settings in your app or website:

Setting Value
SMTP Host smtp.gmail.com
SMTP Port 587 (TLS) or 465 (SSL)
Encryption TLS or SSL
SMTP Username Your full Gmail address
SMTP Password Your Gmail password or App Password
From Email Your Gmail address
From Name Anything you prefer

Port 587 with TLS is the most commonly used.


Example Configuration in WordPress (Using WP Mail SMTP)

  1. Install the WP Mail SMTP plugin

  2. Go to WP Mail SMTP > Settings

  3. Choose Mailer: "Other SMTP"

  4. Enter:

    • SMTP Host: smtp.gmail.com

    • SMTP Port: 587

    • Encryption: TLS

    • SMTP Username: your Gmail address

    • SMTP Password: your Gmail/App password

  5. Save and test!


Example Configuration in PHP

php
 
$mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'your@gmail.com'; $mail->Password = 'your_password_or_app_password'; $mail->SMTPSecure = 'tls'; // or 'ssl' $mail->Port = 587; $mail->setFrom('your@gmail.com', 'Your Name'); $mail->addAddress('recipient@example.com'); $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email from Gmail SMTP'; $mail->send();

⚠️ Common Issues & Fixes

Issue Solution
SMTP connect() failed Use the correct port (587 or 465) and check your firewall
Username and Password not accepted Ensure your Gmail password/App Password is correct
Gmail blocks the sign-in attempt Check Gmail security alerts or use App Password
Port 25 blocked Use port 587 or 465 instead
Email goes to spam Set proper “From Name” and SPF records

Test the SMTP Connection (Optional)

From your hosting terminal:

bash
telnet smtp.gmail.com 587

Or use PHP’s mail() test or WP Mail SMTP’s test email feature.


✅ Final Tips

  • Use App Passwords if 2FA is enabled — they are more secure.

  • Never share your Gmail credentials in public scripts or repos.

  • For heavy email sending, consider Gmail API or use a transactional email service like Mailgun, SendGrid, or Amazon SES.

這篇文章有幫助嗎? 0 用戶發現這個有用 (0 投票)