私はすでにポニーを使用してajaxとsinatraを使用してメールを送信したので、添付ファイルを追加しようとしましたが、追加しようとすると添付ファイルを送信できません。メールは送信されますが、添付ファイルは noname に設定されており、拡張子を .docx に変更して表示すると、次のようになります。
----==_mimepart_559cc76aa4b6f_84433ffe5e0ae1b8555f0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Joe Bloggs has applied for the 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*
私はこの 2 日間、頭をテーブルにぶつけていて、これを解決することができません。コードのどの部分が間違っているのかわかりません.Ajaxを使用してフォームからすべての正しい情報を取得しており、送信されている間、添付ファイルを除いてほとんどが機能していると思います.添付ファイルに対して私が間違っていることです
これが私が使用している私のコードです
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 YOUR 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);
}
})
});
ルビー
post '/job-form', :provides => :json do
Pony.mail({
:to => ENV["TO_ADDRESS"],
:via => :smtp,
:from => ENV["EMAIL_ADDRESS"],
:subject => "Application for #{params["position"]}",
:body => params["fullName"] + " has applied for the 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