我已经使用 ajax 和使用 pony 的 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*
在过去的两天里,我一直在用头撞桌子,无法解决这个问题。我不知道我的代码的哪一部分是错误的,据我所知,我正在使用 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