You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

求助:配置InfinityFree网站表单通过Gmail SMTP发送邮件

Hey there! As someone who’s helped folks navigate InfinityFree’s limitations before, I’ve got you covered. Since they block the native PHP mail() function, we’ll use SMTP directly with PHPMailer—a totally free, beginner-friendly library that works perfectly on their platform. Let’s break this down into simple steps:

Step 1: Prep Your Gmail Account for SMTP Access

First, Gmail blocks most third-party apps from accessing your account by default, so we need to set up an app-specific password (this is way more secure than enabling "less secure apps"):

  • Turn on 2-Step Verification for your Google Account (you can find this in the "Security" section of your account settings).
  • Once that’s done, go back to Security settings and look for "App passwords".
  • Create a new app password, name it something like "InfinityFree Form", and save that 16-character code somewhere safe—you’ll need it later.
Step 2: Set Up the Form & PHP Script

We’ll create a simple PHP script that uses PHPMailer to send your form data via Gmail’s SMTP server:

  1. Get PHPMailer: Download the latest zip from its official repository, extract it, and upload the src folder to your InfinityFree website (use their file manager or an FTP client like FileZilla).
  2. Create the Send Script: Make a new file called send-form.php in your website’s root folder, and paste this code (replace the placeholders with your info!):
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load PHPMailer files
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

// Initialize the mailer
$mail = new PHPMailer(true);

try {
    // Gmail SMTP settings (don't change these unless you know what you're doing)
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-full-gmail@gmail.com'; // Your Gmail address
    $mail->Password = 'your-16-char-app-password'; // The app password you made earlier
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = 465;

    // Who the email is from and to (send to your own Gmail)
    $mail->setFrom('your-full-gmail@gmail.com', 'Your Website Name');
    $mail->addAddress('your-full-gmail@gmail.com');

    // Grab data from your form (match these names to your form's input fields!)
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // Email content (you can customize this!)
    $mail->isHTML(true);
    $mail->Subject = 'New Form Submission from Your Website';
    $mail->Body = "
        <h3>New Contact Form Submission</h3>
        <p><strong>Name:</strong> $name</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Message:</strong> $message</p>
    ";

    // Send the email!
    $mail->send();
    echo "Success! Your message has been sent to your Gmail.";
} catch (Exception $e) {
    echo "Oops, something went wrong. Error: {$mail->ErrorInfo}";
}
?>
  1. Update Your HTML Form: Make sure your form submits to this script. Here’s a basic example (adjust the fields to match your existing form):
<form method="post" action="send-form.php">
    <label for="name">Your Name:</label>
    <input type="text" id="name" name="name" required>

    <label for="email">Your Email:</label>
    <input type="email" id="email" name="email" required>

    <label for="message">Your Message:</label>
    <textarea id="message" name="message" rows="5" required></textarea>

    <button type="submit">Send Message</button>
</form>
Step 3: Upload & Test
  • Upload the src folder and send-form.php to your InfinityFree site.
  • Double-check that you replaced all the placeholder values (Gmail address, app password) in the PHP script.
  • Go to your website, fill out the form, and hit submit. You should see a success message, and the email should pop up in your Gmail inbox (check the spam folder if it doesn’t show up right away!).
Quick Beginner Tips
  • Security: Keep that app password private—don’t share it or post it anywhere public.
  • Spam Avoidance: Make sure the "From" email matches your Gmail address; otherwise, Google might flag it as spam.
  • Troubleshooting: If you get an error, the message in send-form.php will tell you exactly what’s wrong (most common issues are wrong app password or 2-Step Verification not being enabled).

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

火山引擎 最新活动