You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP7容器中Swiftmailer搭配Gmail SMTP无法发邮件,求助排查

Configuring Swiftmailer with Google SMTP in Codeanywhere PHP7 Container (No Framework)

I recently went through setting up Swiftmailer in Codeanywhere's PHP7 container without any framework, using Google's SMTP service since I didn't have my own server. Here's a practical breakdown of what worked for me:

Prerequisites

First, confirm you have these in place:

  • Codeanywhere PHP7 container (Linux-based)
  • Composer installed
  • Swiftmailer added via Composer (composer require swiftmailer/swiftmailer)

Google Account Prep for SMTP

Google has strict security settings, so you need to adjust your account first:

  • If 2-Step Verification is enabled (highly recommended), create an App Password: Go to your Google Account > Security > App Passwords (re-sign in) > Select "Mail" as the app, "Other" as the device, then generate the password. Use this instead of your regular account password.
  • If 2-Step Verification is disabled, enable "Less secure app access" (note: this is not ideal for long-term use due to security risks) under Google Account > Security > Less secure app access.

Complete Swiftmailer Configuration Code

Building on your initial snippet, here's the full working code:

<?php
require_once '/box/workspace/vendor/autoload.php';

// Create SSL-based SMTP Transport
$transport = (new Swift_SmtpTransport('ssl://smtp.gmail.com', 465))
  ->setUsername('your-google-email@gmail.com')
  ->setPassword('your-app-password-or-account-password');

// Initialize Mailer with the Transport
$mailer = new Swift_Mailer($transport);

// Compose your email
$message = (new Swift_Message('Test Email from Swiftmailer'))
  ->setFrom(['your-google-email@gmail.com' => 'Your Display Name'])
  ->setTo(['recipient@example.com' => 'Recipient Name'])
  ->setBody('This is a test email sent using Swiftmailer with Google SMTP!');

// Send the email
$result = $mailer->send($message);

// Verify success
if ($result) {
  echo "Email sent successfully! Delivered to $result recipient(s)";
} else {
  echo "Failed to send email.";
}
?>

Key Details:

  • We use ssl://smtp.gmail.com on port 465 (Google's recommended SSL port for SMTP).
  • Replace placeholders like your-google-email@gmail.com and the password with your actual credentials.
  • Adjust the setFrom, setTo, and message content to match your needs.

Testing the Setup

Save the code as send-email.php in your workspace, then run it via the terminal:

php send-email.php

If configured correctly, you’ll see a success message, and the recipient will get the test email.

Common Troubleshooting

  • Authentication Errors: Double-check your email and password/App Password. If you get a "Username/Password not accepted" error, confirm your Google account settings are applied correctly.
  • Connection Issues: Codeanywhere’s default setup allows outgoing connections to port 465, but if you have custom firewall rules, verify they don’t block smtp.gmail.com:465.
  • Spam Folder: Test emails might land in spam. Ensure your From address matches your Gmail account, and avoid spammy language in the subject/body.

内容的提问来源于stack exchange,提问作者aysee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:47:31