3

I know lot of qeustions about this have already been asked about phpmailer but i couldnt find a solution for myself. Here is my HTML form:

 <div class="info">
 <form class="solliciteren" action="" method="post">
                            Naam
                            <input type="text" name="naam" value="">Telefoonnummer
                            <input type="text" name="telefoonnummer" value="">Email
                            <input type="email" name="emailid" value="">Leeftijd
                            <input type="number" name="leeftijd" value="">Upload uw CV
                            <input type="file" name="uploaded_file" value="CV">
                            <input type="submit" name="verzenden" value="Verzenden">
                        </form> 

When i use my php code it will send everything to my gmail account but the problem is if i send a file it will show that there is a file send but i cant open or download it in gmail because it is saved as an .tmp file. Here is my PHP code:

<?php
              if(isset($_POST['verzenden']))
              {

              $message=
              'naam:    '.$_POST['naam'].'<br />
              telefoonnummer:   '.$_POST['telefoonnummer'].'<br />
              email:    '.$_POST['emailid'].'<br />
              leeftijd: '.$_POST['leeftijd'].'<br />
              upload:   '.$_POST['uploaded_file'].'
              ';
                  require "phpmailer/class.phpmailer.php";
                  $mail = new PHPMailer();

                  $mail->IsSMTP();
                  $mail->SMTPAuth = true;
                  $mail->SMTPSecure = "ssl";
                  $mail->Host = "smtp.gmail.com";
                  $mail->Port = 465;
                  $mail->Encoding = '7bit';


                  $mail->Username   = "mygmailaccount@gmail.com";
                  $mail->Password   = "mygmailpassword";


                  $mail->SetFrom($_POST['emailid'], $_POST['naam']);
                  $mail->AddReplyTo($_POST['emailid'], $_POST['naam']);
                  $mail->Subject = "Nieuwe Sollicitatie!";
                  $mail->MsgHTML($message);

                  $mail->AddAddress("mygmailaccount@gmail.com", "my name");
                  $result = $mail->Send();
                    $message = $result ? 'Sollicitatie is ontvangen!' : 'Bericht is niet verstuurd! Probeer het opnieuw!';
                unset($mail);
              }
              ?>

My Qeustion I want people to upload a file(.word/.txt/.jpg or anything else) in my html form which i can open/download in the gmail account where the mail is been send to. The problem now is that it will show a file is send, but i can't open or download it because it is saved as a .TMP file.

ICTMitchell
  • 91
  • 1
  • 12
  • 1
    You will have to use `enctype="multipart/form-data"` in your `
    ` element, and then access the file(s) using the `$_FILES` superglobal variable. The files can be uploaded to a temporary directory on your server, and then subsequently be attached to the mail; or be directly passed to PHPMailer. Once the mail is sent, the temporary file can be removed (or leave it to be cleaned up by garbage collection, although I don't typically do that).
    – Terry Jan 13 '17 at 15:39
  • 1
    Possible duplicate of [Send File Attachment from Form Using phpMailer and PHP](http://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php) – Terry Jan 13 '17 at 15:40
  • 1
    You really should check what is right in front of you - as well as many duplicates on here, PHPMailer is bundled with a whole load of examples, [one of which does precisely what you're asking](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps) - so *you already have the code to do this*! – Synchro Jan 13 '17 at 16:06
  • @Terry Do you have an example? i tried using it in my code but it will now send a file but it saves as .tmp so i still cant open it – ICTMitchell Jan 16 '17 at 10:26
  • @Synchro thanks for the example but still it didnt work.. it saves as a .tmp file so i still cant open it – ICTMitchell Jan 16 '17 at 10:27
  • You need to say more than "it doesn't work". That something saves as a .tmp file makes no difference, and should have no effect on adding it as an attachment because it's abstracted away by the code. – Synchro Jan 16 '17 at 10:30
  • @Synchro i used the Autoload file, and edited my form, but the problem is if you upload like a .word or .txt file it will email but in the email it will be saved as a .tmp so i can't open it. – ICTMitchell Jan 16 '17 at 10:34
  • You need to post your code - what you've posted above doesn't even try to add an attachment. Also, don't do this: `$mail->SetFrom($_POST['emailid'], $_POST['naam']);`; it's forgery, and will result in your messages being bounced. Put the submitter's address in reply-to (which you're already doing) and put your own address as the from address. – Synchro Jan 16 '17 at 10:42

1 Answers1

1

Along with the file path in attachment, you also need to specify the file name.

Example: $mail->AddAttachment($_FILES['atachment']['tmp_name'], $_FILES['atachment']['name']);

If you only do $mail->AddAttachment($_FILES['atachment']['tmp_name']);

Then a .tmp file will be sent as a attachment.

Muhammadpen
  • 55
  • 2
  • 10