Jump to content

File not uploaded using AddAttachment with PHPMailer


Jen2002
 Share

Go to solution Solved by Jen2002,

Recommended Posts

I want to send an attachement file by the user from an html form and send it by email with PHPMailer.

I can send every input, except the attachement, can you please help... Here's my HTML form code

...
<div class="form-group">
      <input type="file" id="Attachment" class="form-control-textarea" placeholder=" Upload file:" name="Attachment" style="text-decoration: underline"></input>
</div>
<div class="form-group">
      <button onClick="sendContact();" type="button" class="form-control-submit-button" name="submit">Submit</button>
</div>
</form>

Now the Jquery that send form inputs to PHPMailer:

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    function sendContact()
    {
        var valid;
        valid = validateContact();

        if(valid)
        {
            jQuery.ajax(
            {
            url: "mail_handler.php",
            /*data: $("#ContactFrm").serialize(), */
            data: new FormData(this), // grab all form contents including files
            type: "POST",
            success:function(data)
            {
            $("#feedback").html(data);

I use FormData() to grab the file information.

Finally the PHP code:


$file_to_attach = $_FILES['Attachment']['tmp_name'];
$filename = $_FILES['Attachment']['name'];

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->addAttachment($file_to_attach, $filename);

Like I said email is sent but no attachment...

Thanks

Link to comment
Share on other sites

Do you know that the file that you attempted to upload actually got to your server/php?  Have you tried to do a move_uploaded_file to create a permanent copy that you can look for afterwards and know that your upload was a success?

10 mins later.

I notice that you did NOT use move_uploaded_file to save the upload under the name that you later use to make an attachment.  That is your problem.  If you had use the tmpname then you would have had a file.  I suggest that you use the move function to save the uploaded file into a special folder for such attachments and then attach that newly saved file to your email.

Edited by ginerjm
Link to comment
Share on other sites

Before with the serialize() function it was the case, then I switch to data: new FormData(this), to grab all form contents including the uploaded files...

I will read about move_uploaded_file, and try it tomorrow at lunch...will post back result

Thanks

Link to comment
Share on other sites

I think the problem I have with this situation, it's the email that is sent with all the other informations. After submitting the form I receive no error, and the email lands in my inbox, but no attachment ?

Edited by Jen2002
Link to comment
Share on other sites

  • Solution

Found out what was going wrong thanks to F12...Attachment [object] was always null so I added these two lines in my Ajax code:

processData: false,
contentType: false,

 

Edited by Jen2002
Link to comment
Share on other sites

Yes it works, by default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, which is the case here, set this option to false.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.