Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

javascript - PHP upload file to a contact form and email it

I need to add in a contact form a section for people to upload their information (in PDF or image (JPG/PNG/ETC)). I got the section to upload the file, but it isn't sending the PDF with the e-mail (e-mail is working fine, but it arrives without the file uploaded, i don't know why). i will put my HTML/PHP code, so if you could help me it to know where is the problem or how can i achieve to upload a file, it would be great!

HTML

<form class="mb-2" action="freetest.php"  onsubmit="return validateForm();" method="post">
                <div class="form-group">
                    <div>
                        <label class="mb-0">Name</label>
                    </div>
                    <div class="form-group">
                        <input class="form-control-input" type="text" id="firstName" name="firstName" placeholder="Your Name" required>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="mb-0">Last Name</label>
                    </div>
                    <div class="form-group">
                        <input class="form-control-input" type="text" id="lastName" name="lastName" placeholder="Amelie" required>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="mb-0">Mail</label>
                    </div>
                    <div>
                        <input class="form-control-input" type="text" name="email" id="yourEmail" placeholder="[email protected]" required>
                    </div>
                </div>
                <div class=form-group>
                <label class="mb-0">?Any Question?</label>
                <textarea class="form-control-textarea" rows="8" name="message" required></textarea>
                </div>
                    <div class="form-group">
                        <div>
                            <label for="file-upload" class="custom-file-upload mt-2"><i class="fas fa-cloud-upload-alt"></i> Upload File</label>
                            <input id="file-upload" type="file" name="file" multiple="multiple" required>
                        </div>
                    </div>
                <button type="submit" class="btn-solid-lg">Send</button>
            </form>

PHP

 <?php
$ToEmail = '[email protected]'; 
        $EmailSubject = 'Free test'; 
        $mailheader = "From: ".$_POST["lastName"]."
"; 
        $mailheader .= "Reply-To: ".$_POST["email"]."
"; 
        $mailheader .= "Content-type: text/html; charset=iso-8859-1
"; 
        $MESSAGE_BODY = "First Name: ".$_POST["firstName"]."<br/>"; 
        $MESSAGE_BODY .= "Last Name: ".$_POST["lastName"]."<br/>"; 
        $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br/>"; 
        $MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"]).""; 
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

but it arrives without the file uploaded, i don't know why

I can tell you WHY:

You DO NOT ATTACH the file to the mail.

You only send text.

For some solutions how to do this please visit this thread which will explain how to do this with and without phpmailer

Send attachments with PHP Mail()?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...