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

HTML表单添加文件上传后无法实现邮件附件功能求解决方案

Issue with File Upload & Email Attachment in HTML Form (PHP/AJAX)

Hey folks,

I've got a landing page form that collects customer info and sends all submissions via email—this part works perfectly fine. But recently I added a file upload field ($userfile) to the form, and I can't get the file upload and email attachment functionality to work properly, even after multiple rounds of debugging.

Here's my current PHP code (built from an existing template):

$to = "email@email.com"; 
$nome = $_POST["nome"]; 
$email = $_POST["email"]; 
$website = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

if (isset($email) && isset($nome)) { 
    $subject = "Lead: $nome"; 
    $headers = "MIME-Version: 1.0" . "\n"; 
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\n"; 
    $headers .= "From: ".$nome." <".$email.">\n"."Reply-To: ".$email."\n" ; 

    $msg = 'Hey Admin, <br/> <br/> Here are the details:'; 
    $msg .= ' <br/> <br/> <table border="1" cellpadding="6" cellspacing="0" style="border: 1px solid #eeeeee;">'; 
    foreach ($_POST as $label => $value) { 
        $msg .= "<tr><td width='100'>". ucfirst($label) . "</td><td width='300'>" . $value . " </tr>"; 
    } 
    $msg .= " </table> <br> --- <br>Sent by $website"; 

    $mail = mail($to, $subject, $msg, $headers); 
    if($mail) { 
        echo 'success'; 
    } else { 
        echo 'failed'; 
    } 
} 
// END isset 
?>

And here's the AJAX POST code I'm using:

var dataString = $(form).serialize(); 
/* AJAX POST --------- */ 
$.ajax({ 
    type: "POST", 
    data: dataString, 
    url: "php/contact.php", 
    cache: false, 
    success: function(d) { 
        $("form-group").removeClass("has-success"); 
        if (d == 'success') { 
            if (noredirect) { 
                $('#js-contact-result').fadeIn('slow').html('<div class="alert alert-success top-space">' + success_msg + '</div>').delay(3000).fadeOut('slow'); 
            } else { 
                window.location.href = redirect; 
            } 
        } else { 
            $('#js-contact-result').fadeIn('slow').html('<div class="alert alert-danger top-space">' + error_msg + '</div>').delay(3000).fadeOut('slow'); 
        } 
        $("#js-contact-btn").attr("disabled", false); 
    } 
}); 

I'd really appreciate any help figuring this out, or even some learning resources that walk through handling file uploads with email attachments in PHP/AJAX. Thanks a ton!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:44:19