使用 ajax 和 sinatra 发送附件

Sending attachment using ajax and sinatra

我已经使用 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*

过去 2 天我一直在用头撞 table,无法解决这个问题。我不知道我的代码的哪一部分是错误的,据我所知,我正在使用 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>&nbsp;&nbsp;<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);
      }
    })
  });

Ruby

  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

已使用邮件修复

我终于切换到 Mail 并且发送附件没有问题。这是我的邮件代码,适用于其他与小马有同样问题但尚未解决的人

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 do
  delivery_method :smtp, options
end


post '/job-form' do

  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 do
    to      ENV["TO_ADDRESS"]
    from    ENV["EMAIL_ADDRESS"]
    subject "Application for #{position}"
    body    "#{full_name} has applied for the position of #{position}\n#{email}\n\n#{cover_letter}"
    add_file :filename => file_name, :content => File.read(tempfile)
  end

end