Jen2002 Posted October 19, 2021 Share Posted October 19, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/ Share on other sites More sharing options...
ginerjm Posted October 19, 2021 Share Posted October 19, 2021 (edited) 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 October 19, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591226 Share on other sites More sharing options...
Jen2002 Posted October 20, 2021 Author Share Posted October 20, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591237 Share on other sites More sharing options...
Jen2002 Posted October 20, 2021 Author Share Posted October 20, 2021 (edited) 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 October 20, 2021 by Jen2002 Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591238 Share on other sites More sharing options...
Solution Jen2002 Posted October 20, 2021 Author Solution Share Posted October 20, 2021 (edited) 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 October 20, 2021 by Jen2002 Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591239 Share on other sites More sharing options...
ginerjm Posted October 20, 2021 Share Posted October 20, 2021 And your attached file showed up? Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591246 Share on other sites More sharing options...
Jen2002 Posted October 20, 2021 Author Share Posted October 20, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314048-file-not-uploaded-using-addattachment-with-phpmailer/#findComment-1591257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.