带有附件 wordpress 的联系表

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

您好,我想创建一个带附件的表单,单击提交时,表单详细信息和附件将作为附件发送到我的电子邮件和此目录: /var/www/html/attach(/var/www/html/ 是我的 Wordpress 根目录)

这就是我所做的,表格已发送到我的电子邮件,但没有附件,在这个目录中我什么也找不到:

PHP代码:

<?php
if(isset($_POST['submit'])) {
    $to = '[email protected]'; // Replace with your own email address
    $subject = $_POST['subject']; // Get the value of the subject field from the form
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // File upload handling
    if(isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
        $target_dir = '/var/www/html/attach/'; // Replace with your desired upload directory
        $target_file = $target_dir . basename($_FILES["image"]["name"]);
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        $uploadOk = 1;

        // Check file size (max 2MB)
        if ($_FILES["image"]["size"] > 2 * 1024 * 1024) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }

        // Allow only PNG and JPG files
        if($imageFileType != "png" && $imageFileType != "jpg") {
            echo "Sorry, only PNG and JPG files are allowed.";
            $uploadOk = 0;
        }

        // Upload the file if everything is ok
        if ($uploadOk == 1 && move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            $attachment_path = $target_file;
            $attachment_name = basename($_FILES["image"]["name"]);
        }
    }

    // Set the email headers
    $headers = array();
    $headers[] = "From: {$name} <{$email}>";
    $headers[] = "Reply-To: {$name} <{$email}>";
    $headers[] = "MIME-Version: 1.0";

    // Set the email boundary
    $boundary = "--" . md5(uniqid(rand(), true));

    // Set the email content type and boundary
    $headers[] = "Content-Type: multipart/mixed; boundary=\"{$boundary}\"";
    $message_body = "This is a multi-part message in MIME format.\r\n\r\n";
    $message_body .= "--{$boundary}\r\n";
    $message_body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $message_body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $message_body .= $message . "\r\n\r\n";

    // Add the attachment to the email if it exists
    if(isset($attachment_path)) {
        $file_content = file_get_contents($attachment_path);
        $file_content_base64 = base64_encode($file_content);
        $message_body .= "--{$boundary}\r\n";
        $message_body .= "Content-Type: {$imageFileType}; name=\"{$attachment_name}\"\r\n";
        $message_body .= "Content-Transfer-Encoding: base64\r\n";
        $message_body .= "Content-Disposition: attachment; filename=\"{$attachment_name}\"\r\n\r\n";
        $message_body .= $file_content_base64 . "\r\n\r\n";
    }

    // Send the email
    $sent = wp_mail($to, $subject, $message_body, $headers);

    if ($sent) {
        // Redirect to thank you page
        wp_redirect('/thank-you-contact-form-sent'); // Replace with the URL of your thank-you page
        exit();
    } else {
        // Redirect to sorry page
        wp_redirect('/sorry-contact-form-not-sent'); // Replace with the URL of your sorry page
        exit();
    }
}
?>

这是 Wordpress 页面中的表格:

<form id="contact-svg-order" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" class="form-control" id="name" name="name" optional>
    </div>
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" class="form-control" id="email" name="email" optional>
    </div>
    <div class="form-group">
        <label for="subject">Subject:</label>
        <input type="text" class="form-control" id="subject" name="subject" optional>
    </div>
    <div class="form-group">
        <label for="message">Message:</label>
        <textarea class="form-control" id="message" name="message" rows="5" optional></textarea>
    </div>
<br>
    <div class="form-group">
        <label for="image">Image (PNG or JPG, max 2MB):</label>
        <input type="file" class="form-control" id="image" name="image" accept=".png,.jpg" required>
    </div>
<br>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" name="submit" style="width: 100%;">Send Message</button>
    </div>
</form>
php wordpress attachment email-attachments custom-wordpress-pages
© www.soinside.com 2019 - 2024. All rights reserved.