-1

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="Example@gmail.com" 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 = 'mymail@gmail.com'; 
        $EmailSubject = 'Free test'; 
        $mailheader = "From: ".$_POST["lastName"]."\r\n"; 
        $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
        $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
        $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");
?>
BSaitak
  • 3
  • 2
  • https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – zimorok Jan 11 '21 at 03:06
  • go through the replies if you want to use `mail()` function – zimorok Jan 11 '21 at 03:07
  • 1
    i suggest use PHP Mailer – Anas Fanani Jan 11 '21 at 03:08
  • I don't see `` anywhere. You probably want to create that Element dynamically anyways, and just click it without seeing it. You will need to `FormDateInstance.append(yourFile)` once you have one. Pseudo-code: `const fd = new FormData, upload = document.querySelector('input[type=file]'); upload.onchange = function(){ if(this.files){ const file = this.files[0], name = file.name, dot = name.lastIndexOf('.'); if(dot !== -1 && name.slice(dot).toLowerCase() === '.pdf'){ fd.append('fileGlobal', this.files[0], 'uploadName.pdf'); } } }`. Access as `$_FILES['fileGlobal']`. – StackSlave Jan 11 '21 at 03:19
  • In regards to my last comment you send `FormData` via the `XMLHttpRequest`, by the way. – StackSlave Jan 11 '21 at 03:22
  • does this question have anything to do with [java]? – geocodezip Jan 11 '21 at 03:24
  • zimorok - Thanks! i want to use mail() but i'm open to other answers geocodezip - Maybe someone could suggest an Java solution :) StackSlave - Yeah sorry, i got the file but didn't put it because wasn't working, i will edit and add it. But i got a question, that code will get the email sent with the attached file? or you recomend to use PHPMailer. and if i wanted to use PHOMailer, how can i make it to work? – BSaitak Jan 11 '21 at 03:47
  • @zimorok does phpmailer work if i want them to upload a file to me? and how :/? the URL you attached talked about sending a pdf, not reciving – BSaitak Jan 11 '21 at 04:06
  • it is the same though because the user will need to upload the files then your script will email it to your email with the attachment. – zimorok Jan 11 '21 at 04:12

2 Answers2

0

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()?

0

I hope it will be helping u I will test it and successfully send the mail with attachment with a .zip file.

$my_file = "somefile.zip";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);