使用PHP通过电子邮件发送来自html表单的上传文件(Emailing uploaded file from html form with PHP)

我刚刚为我的一个wordpress网站完成了一个HTML表单,效果很好。 当它被提交时,它会在WP_User数据库表中创建一个用户,并向我发送一封电子邮件,其中包含表单中的所有信息。 但是,我的html表单允许用户上传所需的文件,但电子邮件只显示文件名。 我正在寻找一种方法来通过电子邮件发送要查看和下载的实际文件。 到目前为止,这是我的代码:

<div id="container"> <form method="post" name="myForm"> <h1 align="center">Welcome!</h1> <h2 align="center">Thank you for your interest. We look forward to working with you.</h2> <h3>First Name</h3> <input type="text" name="fName" style="width: 355px;"/> <h3>Last Name</h3> <input type="text" name="lName" style="width: 355px;"/> <h3>Company Name</h3> <input type="text" name="compName" style="width: 355px;"/> <h3>Business Needs</h3> <select name="businessNeeds" style="width: 355px;"> <option value="">Select...</option> <option value="IntDesign">Interior Designer</option> <option value="Ecom">E-Commerce Only</option> <option value="Retail">Retail Store Only</option> <option value="RetailEcom">Retail and E-Commerce</option> <option value="Mult">Multiple Locations</option> </select> <h3>Address</h3> <input type="text" name="address" style="width: 355px;"/> <h3>City</h3> <input type="text" name="city" style="width: 355px;"/> <h3>State</h3> <input type="text" name="state" style="width: 355px;"/> <h3>Zip</h3> <input type="text" name="zip" style="width: 355px;"/> <h3>Phone</h3> <input type="text" name="phone" style="width: 355px;"/> <h3>Email</h3> <input id="email" type="text" name="uemail" style="width: 355px;"/> <h3>Create a Username</h3> <input type="text" name="uname" style="width: 355px;"/> <h3>Create a Password</h3> <input type="password" name="pass" style="width: 355px;"/> <h3>Confirm Password</h3> <input type="password" name="ConfPass" style="width: 355px;"/> <h3>Sales Tax ID</h3> <input type="text" name="taxID" style="width: 355px;"/> <h3>Upload Tax ID Certificate</h3> <input type="file" name="fileUpload" style="width: 355px;"/> <input type="submit" value="Submit" /> </form> </div> <?php function create_account(){ $fName = ( isset($_POST['fName']) ? $_POST['fName'] : '' ); $lName = ( isset($_POST['lName']) ? $_POST['lName'] : '' ); $compName = ( isset($_POST['compName']) ? $_POST['compName'] : '' ); $businessNeeds = ( isset($_POST['businessNeeds']) ? $_POST['businessNeeds'] : '' ); $address = ( isset($_POST['address']) ? $_POST['address'] : '' ); $city = ( isset($_POST['city']) ? $_POST['city'] : '' ); $state = ( isset($_POST['state']) ? $_POST['state'] : '' ); $zip = ( isset($_POST['zip']) ? $_POST['zip'] : '' ); $phone = ( isset($_POST['phone']) ? $_POST['phone'] : '' ); $email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' ); $user = ( isset($_POST['uname']) ? $_POST['uname'] : '' ); $pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' ); $ConfPass = ( isset($_POST['ConfPass']) ? $_POST['ConfPass'] : '' ); $taxID = ( isset($_POST['taxID']) ? $_POST['taxID'] : '' ); $fileUpload = ( isset($_POST['fileUpload']) ? $_POST['fileUpload'] : '' ); $email_from = 'admin'; $email_subject = "New Wholesale Form Submission"; $message = "You have received a new message from: \n"; $message .= "First Name: " .$_POST["fName"]. "\n"; $message .= "Last Name: ".$_POST["lName"]. "\n"; $message .= "Company Name: ".$_POST["compName"]. "\n"; $message .= "Business Type: " .$_POST["businessNeeds"]. "\n"; $message .= "Address: ".$_POST["address"]. "\n"; $message .= "City: ".$_POST["city"]. "\n"; $message .= "State: ".$_POST["state"]. "\n"; $message .= "Zip: ".$_POST["zip"]. "\n"; $message .= "Phone: ".$_POST["phone"]. "\n"; $message .= "Email: ".$_POST["uemail"]. "\n"; $message .= "Tax ID: ".$_POST["taxID"]. "\n"; $message .= "Certificate: ".$_POST["fileUpload"]. "\n"; $to = "email"; $headers = "From: $email_from \r\n"; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); if( !is_wp_error($user_id) ) { //user has been created $user = new WP_User( $user_id ); $user->set_role( 'subscriber' ); wp_mail( $to, $email_subject, $message, $headers ); //Redirect wp_redirect( 'URL' ); exit; } else { //$user_id is a WP_Error object. Manage the error } } } add_action('init','create_account'); ?>

I just finished an HTML form for one of my wordpress sites, and it works great. When it's submitted it creates the person as a user in the WP_User database table and it sends me an email with all of the info from the form. However, my html form allows the user to upload a required file but the email only shows the file name. I'm looking for a way to email the actual file to be viewed and downloaded. Here's my code thus far:

<div id="container"> <form method="post" name="myForm"> <h1 align="center">Welcome!</h1> <h2 align="center">Thank you for your interest. We look forward to working with you.</h2> <h3>First Name</h3> <input type="text" name="fName" style="width: 355px;"/> <h3>Last Name</h3> <input type="text" name="lName" style="width: 355px;"/> <h3>Company Name</h3> <input type="text" name="compName" style="width: 355px;"/> <h3>Business Needs</h3> <select name="businessNeeds" style="width: 355px;"> <option value="">Select...</option> <option value="IntDesign">Interior Designer</option> <option value="Ecom">E-Commerce Only</option> <option value="Retail">Retail Store Only</option> <option value="RetailEcom">Retail and E-Commerce</option> <option value="Mult">Multiple Locations</option> </select> <h3>Address</h3> <input type="text" name="address" style="width: 355px;"/> <h3>City</h3> <input type="text" name="city" style="width: 355px;"/> <h3>State</h3> <input type="text" name="state" style="width: 355px;"/> <h3>Zip</h3> <input type="text" name="zip" style="width: 355px;"/> <h3>Phone</h3> <input type="text" name="phone" style="width: 355px;"/> <h3>Email</h3> <input id="email" type="text" name="uemail" style="width: 355px;"/> <h3>Create a Username</h3> <input type="text" name="uname" style="width: 355px;"/> <h3>Create a Password</h3> <input type="password" name="pass" style="width: 355px;"/> <h3>Confirm Password</h3> <input type="password" name="ConfPass" style="width: 355px;"/> <h3>Sales Tax ID</h3> <input type="text" name="taxID" style="width: 355px;"/> <h3>Upload Tax ID Certificate</h3> <input type="file" name="fileUpload" style="width: 355px;"/> <input type="submit" value="Submit" /> </form> </div> <?php function create_account(){ $fName = ( isset($_POST['fName']) ? $_POST['fName'] : '' ); $lName = ( isset($_POST['lName']) ? $_POST['lName'] : '' ); $compName = ( isset($_POST['compName']) ? $_POST['compName'] : '' ); $businessNeeds = ( isset($_POST['businessNeeds']) ? $_POST['businessNeeds'] : '' ); $address = ( isset($_POST['address']) ? $_POST['address'] : '' ); $city = ( isset($_POST['city']) ? $_POST['city'] : '' ); $state = ( isset($_POST['state']) ? $_POST['state'] : '' ); $zip = ( isset($_POST['zip']) ? $_POST['zip'] : '' ); $phone = ( isset($_POST['phone']) ? $_POST['phone'] : '' ); $email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' ); $user = ( isset($_POST['uname']) ? $_POST['uname'] : '' ); $pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' ); $ConfPass = ( isset($_POST['ConfPass']) ? $_POST['ConfPass'] : '' ); $taxID = ( isset($_POST['taxID']) ? $_POST['taxID'] : '' ); $fileUpload = ( isset($_POST['fileUpload']) ? $_POST['fileUpload'] : '' ); $email_from = 'admin'; $email_subject = "New Wholesale Form Submission"; $message = "You have received a new message from: \n"; $message .= "First Name: " .$_POST["fName"]. "\n"; $message .= "Last Name: ".$_POST["lName"]. "\n"; $message .= "Company Name: ".$_POST["compName"]. "\n"; $message .= "Business Type: " .$_POST["businessNeeds"]. "\n"; $message .= "Address: ".$_POST["address"]. "\n"; $message .= "City: ".$_POST["city"]. "\n"; $message .= "State: ".$_POST["state"]. "\n"; $message .= "Zip: ".$_POST["zip"]. "\n"; $message .= "Phone: ".$_POST["phone"]. "\n"; $message .= "Email: ".$_POST["uemail"]. "\n"; $message .= "Tax ID: ".$_POST["taxID"]. "\n"; $message .= "Certificate: ".$_POST["fileUpload"]. "\n"; $to = "email"; $headers = "From: $email_from \r\n"; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); if( !is_wp_error($user_id) ) { //user has been created $user = new WP_User( $user_id ); $user->set_role( 'subscriber' ); wp_mail( $to, $email_subject, $message, $headers ); //Redirect wp_redirect( 'URL' ); exit; } else { //$user_id is a WP_Error object. Manage the error } } } add_action('init','create_account'); ?>

最满意答案

您需要在表单标记中添加enctype="multipart/form-data"

enctype属性设置或返回表单中enctype属性的值。

enctype属性指定在将表单数据发送到服务器之前应如何编码表单数据。

默认情况下,表单数据被编码为“application / x-www-form-urlencoded”。 这意味着所有字符在发送到服务器之前都会被编码(空格转换为“+”符号,特殊字符转换为ASCII HEX值)。

in multipart / form-data不编码任何字符。 使用具有文件上载控件的表单时,此值是必需的

you need to add enctype="multipart/form-data" in your form tag

The enctype property sets or returns the value of the enctype attribute in a form.

The enctype attribute specifies how form-data should be encoded before sending it to the server.

The form-data is encoded to "application/x-www-form-urlencoded" by default. This means that all characters are encoded before they are sent to the server (spaces are converted to "+" symbols, and special characters are converted to ASCII HEX values).

in multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control

更多推荐