如何从表单发送文件

时间:2014-03-09 09:29:22

标签: php html

我创建了一个表单,如果用户输入他们的详细信息并单击提交,则会将电子邮件发送到定义的电子邮件地址,我的问题是如何允许用户将文件添加到表单然后发送到已定义的电子邮件地址:

HTML:

<form>

                                         名称                 

            <p>
                <input class = 'input-text' type = 'text' name = 'Number' placeholder = 'Number' required/>
                <label>Number</label>
            </p>
            <p>
                <input class = 'input-text' type = 'email' name = 'Email' placeholder = 'Email' required/>
                <label>Email</label>
            </p>
            <p>
                <input class = 'input-text' type = 'text' name = 'Company' placeholder = 'Company' required/>
                <label>Company</label>
            </p>
            <p>
                <input class = 'upload' type = 'file' name = 'File' placeholder = 'Please upload your business plan'>
                <label>Upload a business plan </label>
            </p>
            <textarea type = 'text' placeholder = 'Any Comments?'></textarea>
            <br />
            <button class = 'send' value = 'submit' type = 'submit'>Send</button>
            <button class = 'reset' value = 'reset' type = 'reset'>Reset</button>
        </form>

PHP:

<?php
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$comments = $_POST['comments'];


$to = "jackbryan49@msn.com";
$subject = "ADSML Contact Form";
$body = "This is an email from the ADMSL website, Please do not reply to this email. \n\n From:  $name \n Contact Number:  $number \n Contact Email:  $email \n Company Name:  $company \n User Comments:  $comments";

mail ($to, $subject, $body);
header( 'Location: http://paulbryanpotatoes.com/ADMSL/Contact.html' ) ;

&GT;

2 个答案:

答案 0 :(得分:1)

使用PHP的mail()函数发送附件是一件痛苦的事,不值得烦恼。我会改用PHPMailer。希望主页上的示例足以让您启动并运行它。

答案 1 :(得分:0)

要使用PHP处理文件,您应该使用$_FILE

Offical API-Docs

如果您想通过电子邮件发送文件,您应该在Base64中加密文件的字节流,并将其作为附件添加到电子邮件中。