如何让我的表单将提交的信息发送到我的电子邮件

问题描述 投票:0回答:1

我一直在试图弄清楚如何在我的网站上获取我的联系表格,以便在有人提交时发送到我的电子邮件。

我正在使用 Google Clouds 实例并托管在 google 域上。我尝试使用 process_form.php,但即使我已经在实例中使用 SSH 安装了 php,它也不起作用。

我选择了一种更简单的方法,但效果不佳。这似乎与我几年前尝试提交的“联系我们”表格不同。

这是我的联系我们表单的 HTML 代码:

   <div class="container py-5">
        <div class="row">
            <div class="col-lg-6">
                <h2 class="text-dark">Get in Touch</h2>
                <p class="text-dark">For inquiries, bookings, or any questions, feel free to contact us:</p>
                <ul class="list-unstyled text-dark">
                    <li><strong>Phone:</strong> +1 (123) 456-7890</li>
                    <li><strong>Email:</strong> [email protected]</li>
                </ul>
                <p class="text-dark">Connect with us on social media:</p>
                <div class="d-flex justify-content-start">
                    <a href="#" class="text-dark me-3"><i class="fab fa-facebook-f"></i></a>
                    <a href="#" class="text-dark me-3"><i class="fab fa-twitter"></i></a>
                    <a href="#" class="text-dark me-3"><i class="fab fa-instagram"></i></a>
                    <a href="#" class="text-dark me-3"><i class="fab fa-linkedin-in"></i></a>
                </div>
            </div>

            <!-- Right Section - Contact Form -->
            <div class="col-lg-6">  
                 <form action="mailto:[email protected]" method="post" enctype="text/plain">
                    <div class="mb-3">
                        <label for="name" class="form-label">Name</label>
                        <input type="text" class="form-control" id="name" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="contact" class="form-label">Contact Number</label>
                        <input type="text" class="form-control" id="contact" name="contact" required>
                    </div>
                    <div class="mb-3">
                        <label for="email" class="form-label">Email address</label>
                        <input type="email" class="form-control" id="email" name="email" required>
                    </div>
                    <div class="mb-3">
                        <label for="date" class="form-label">Preferred Date</label>
                        <input type="date" class="form-control" id="date" name="date" required>
                    </div>
                    <div class="mb-3">
                        <label for="destination" class="form-label">Fishing Destination</label>
                        <select class="form-select" id="destination" name="destination" required>
                            <option value="">Select Destination</option>
                            <option value="Destination 1">Destination 1</option>
                            <option value="Destination 2">Destination 2</option>
                            <option value="Destination 3">Destination 3</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="anglers" class="form-label">Number of Anglers</label>
                        <select class="form-select" id="anglers" name="anglers" required>
                            <option value="">Select Number of Anglers</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="rent" class="form-label">Need to rent fishing rods?</label>
                        <select class="form-select" id="rent" name="rent" required>
                            <option value="">Select</option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="rodsets" class="form-label">Number of Rod Sets</label>
                        <input type="number" class="form-control" id="rodsets" name="rodsets" min="0">
                    </div>
                    <button type="submit" class="btn btn-dark">Submit</button>
                </form>
            </div>
        </div>
    </div

我如何真正让它发挥作用?我已经为此花了 4 个小时,但毫无结果。请帮忙。

我尝试使用process_form.php:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace
    $name = trim($_POST["name"]);
    $contact = trim($_POST["contact"]);
    $email = trim($_POST["email"]);
    $date = trim($_POST["date"]);
    $destination = trim($_POST["destination"]);
    $anglers = trim($_POST["anglers"]);
    $rent = trim($_POST["rent"]);
    $rodsets = trim($_POST["rodsets"]);

    // Set the recipient email address
    $to = "[email protected]";

    // Set the email subject
    $subject = "New Contact Form Submission";

    // Build the email content
    $email_content = "Name: $name\n";
    $email_content .= "Contact Number: $contact\n";
    $email_content .= "Email: $email\n";
    $email_content .= "Preferred Date: $date\n";
    $email_content .= "Fishing Destination: $destination\n";
    $email_content .= "Number of Anglers: $anglers\n";
    $email_content .= "Need to rent fishing rods: $rent\n";
    $email_content .= "Number of Rod Sets: $rodsets\n";

    // Build the email headers
    $email_headers = "From: $name <$email>";

    // Send the email
    if (mail($to, $subject, $email_content, $email_headers)) {
        // Redirect to the thank you page if the email is sent successfully
        header("Location: index.html");
    } else {
        // Set a 500 internal server error response if the email failed to send
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }
} else {
    // Set a 403 forbidden response if the form submission method is not POST
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}
?>

这也行不通。我的主要联系我们表单之前已链接到 process_form.php。居然成功了,到了index.html.

我不知道该怎么办了。这是最后一个障碍。我应该选择可能更容易使用的网络托管公司。在 Google 云上使用 SSH 非常困难,一开始我遇到了困难,弄清楚如何使用 google 地图 api 等上传我的 index.html 等。现在这只是我的最后一个障碍,我希望有人可以帮助我。

php html email google-cloud-platform
1个回答
0
投票

需要修复的一些问题

(A) 您需要从

获取 HTML 表单的表单标签
<form action="mailto:[email protected]" method="post" enctype="text/plain">

<form action="so29mar24.php" method="post">

(B) 并将 PHP 更改为

 $email_headers = "From: $name <$email>";

    $encoding = "utf-8";
    $email_headers = "Content-type: text/plain; charset=".$encoding." \r\n";
    $email_headers .= "From: ".$name." <".$email."> \r\n";
    $email_headers .= "MIME-Version: 1.0 \r\n";
    $email_headers .= "Content-Transfer-Encoding: 8bit \r\n";

(C) 如评论所述,请将“发件人电子邮件”更改为您的服务器将视为有效电子邮件发件人的有效电子邮件地址,否则可能会被收件人电子邮件服务器视为垃圾邮件

© www.soinside.com 2019 - 2024. All rights reserved.