jquery - Sending attachment using ajax and sinatra -
i have sent emails using ajax , sinatra using pony , tried add in attachments, when try add cannot send attachment. send email attachment set noname , when change it's extension .docx view it, looks this
----==_mimepart_559cc76aa4b6f_84433ffe5e0ae1b8555f0 content-type: text/plain; charset=utf-8 content-transfer-encoding: 7bit joe bloggs has applied position of software engineer joe@example.com ----==_mimepart_559cc76aa4b6f_84433ffe5e0ae1b8555f0 content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8; filename=test_resume_1.docx content-transfer-encoding: base64 content-disposition: attachment; filename=test_resume_1.docx content-id: <test_resume_1.docx@simons-macbook-pro-2.local> uesdbbqabgaiaaaaiqaxwq+8iaeaabmgaaataagcw0nvbnrlbnrfvhlwzxnd lnhtbccibaiooaacaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *repeated*
i have been banging head against table last 2 days , cannot work out. don't know part of code wrong, far can tell getting correct information forms using ajax , it's being sent of working except attachment believe doing wrong attachment
here code using
html
<form id="application-form" class="box" action="/job-form" method="post" enctype="multipart/form-data"> <div class="form-group"> <input type="text" class="form-control" id="fullname" placeholder=" name" required> </div> <div class="form-group"> <input type="hidden" id="position" value=""> <input type="email" class="form-control" id="email" placeholder=" e-mail" required> </div> <div class="form-group"> <div class="form-control" id="cv" placeholder=" cv"> <i class="fa fa-file-text"></i> <span class="file-text">upload cv </span> </div> <input type="file" id="cv-file" name="attachement" style="float:right;display:none"/> </div> <div class="form-group"> <textarea rows="5" class="form-control" id="cover-letter" placeholder=" cover letter"></textarea> </div> <div class="form-group"> <div class="col-sm-offset-"> <button type="submit" class="btn btn-form">submit</button> </div> </div> </form>
javascript
$('#application-form').on('submit', function(event) { event.preventdefault(); var form = $(this); var fd = new formdata(); fd.append( 'file', $("#cv-file")[0].files[0] ); fd.append("fullname", $("#fullname").val()); fd.append("email", $("#email").val()); fd.append("coverletter", $("#cover-letter").val()); fd.append("position", $("#position").val()); $.ajax({ url: form.attr('action'), processdata: false, contenttype: false, type: 'post', data: fd, error: function(req, err){ console.log('error message: ' + err); $(".form-message-box").html(err); $(".form-message-box").animate({"opacity":"1"},"slow").animate({"opacity":"0"},2000); }, success: function(json) { $(".form-message-box").html("successful!"); $(".form-message-box").animate({"opacity":"1"},"slow").animate({"opacity":"0"},2000); } }) });
ruby
post '/job-form', :provides => :json pony.mail({ :to => env["to_address"], :via => :smtp, :from => env["email_address"], :subject => "application #{params["position"]}", :body => params["fullname"] + " has applied position of " + params["position"] + "\n" + params["email"] + "\n\n" + params["coverletter"], :attachments => { file.basename(params[:file][:filename]) => file.read(params[:file][:tempfile]) }, :headers => { "content-type" => "multipart/mixed", "content-transfer-encoding" => "base64", "content-disposition" => "attachment" }, :via_options => { :address => 'smtp.gmail.com', :port => '25', :user_name => env["email_address"], :password => env["email_password"], :authentication => :plain, :domain => env["domain"] } }) puts file puts params end
fixed using mail
i switched mail , had no problems sending attachment. here mail code else having same problems pony , haven't worked out
options = { :address => "smtp.gmail.com", :port => 25, :domain => env["domain"], :user_name => env["email_address"], :password => env["email_password"], :authentication => 'plain', :enable_starttls_auto => true } mail.defaults delivery_method :smtp, options end post '/job-form' full_name = params["fullname"] position = params["position"] email = params["email"] cover_letter = params["coverletter"] file_name = params[:file][:filename] tempfile = params[:file][:tempfile] mail.deliver env["to_address"] env["email_address"] subject "application #{position}" body "#{full_name} has applied position of #{position}\n#{email}\n\n#{cover_letter}" add_file :filename => file_name, :content => file.read(tempfile) end end
Comments
Post a Comment