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

如何用PHP将表单变量传递至邮件模板?

Hey there! Let's walk through how to pass your form variables into an email template in your postit.php file. Here's a step-by-step solution tailored to your existing setup:

Step 1: Retrieve and Sanitize Form Data

First, grab the values submitted via POST from your form, and add basic sanitization/validation to avoid issues like empty submissions or malicious content:

// Get and clean up form inputs
$dear = isset($_POST['Dear__1']) ? trim($_POST['Dear__1']) : '';
$fullName = isset($_POST['Full_Name__2']) ? trim($_POST['Full_Name__2']) : '';
$billingAddress = isset($_POST['Billing_Address__3']) ? trim($_POST['Billing_Address__3']) : '';

// Backend validation (critical even with frontend checks)
if (empty($fullName) || empty($billingAddress)) {
    die("Please fill in all required fields.");
}

// Escape special characters if using HTML emails to prevent injection
$dear = htmlspecialchars($dear, ENT_QUOTES, 'UTF-8');
$fullName = htmlspecialchars($fullName, ENT_QUOTES, 'UTF-8');
$billingAddress = htmlspecialchars($billingAddress, ENT_QUOTES, 'UTF-8');

Step 2: Build Your Email Template

Use PHP's HEREDOC syntax to create a clean, readable template—this lets you format content naturally without escaping quotes:

// Plain text email template (simple and reliable)
$emailContent = <<<TEXT
Hello,

You've received a new order submission with these details:

- Salutation: $dear
- Full Name: $fullName
- Billing Address:
$billingAddress

Best regards,
Your Website Team
TEXT;

// OR, if you want a styled HTML email:
$emailContent = <<<HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <p>Hello,</p>
    <p>You've received a new order submission:</p>
    <ul>
        <li><strong>Salutation:</strong> $dear</li>
        <li><strong>Full Name:</strong> $fullName</li>
        <li><strong>Billing Address:</strong><br>$billingAddress</li>
    </ul>
    <p>Best regards,<br>Your Website Team</p>
</body>
</html>
HTML;

Step 3: Configure Headers and Send the Email

Finish setting up email parameters and send it using PHP's mail() function (for more reliability, you could use libraries like PHPMailer, but this works for basic use cases):

$to = 'My@email.com';
$subject = 'New Order Submission';

// Set headers to ensure proper encoding and sender identity
$headers = "From: Your Website <noreply@yourdomain.com>\r\n";
// Swap to text/plain if using the plain text template
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

// Send the email
if (mail($to, $subject, $emailContent, $headers)) {
    echo "Thank you! Your order has been submitted successfully.";
} else {
    echo "Oops, something went wrong. Please try submitting your order again later.";
}

Key Tips to Keep in Mind

  • Character Encoding: Always include charset=UTF-8 in headers to avoid garbled text for non-English characters.
  • Security: Sanitize user inputs to prevent email injection attacks, especially if using HTML emails.
  • Validation: Never rely solely on frontend validation—backend checks ensure you don't process empty or invalid data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:31:32