Rails MVC应用PDF邮件附件无法打开问题求助
解决Rails邮件附件在Gmail中无法加载PDF的问题
嘿,我看你遇到了Rails邮件附件在Gmail里打不开的问题,提示Failed to load PDF document对吧?这大概率是因为你处理上传PDF的方式有问题,导致附件内容损坏了,咱们来一步步修复它:
问题根源分析
你控制器里处理上传文件的逻辑有明显漏洞:params[:attachment]其实是Rails封装的ActionDispatch::Http::UploadedFile对象,你直接把它传给StringIO,还创建了Tempfile却根本没把文件内容写入进去,最后邮件里的附件要么是空的,要么是乱码,自然会被Gmail判定为无效PDF。
修复方案
1. 重构控制器的文件处理逻辑
把update和enviar方法改成下面这样,直接操作上传文件对象,不用绕Tempfile和StringIO的弯路:
def update @file_content = nil @file_name = "" # 用present?替代nil?和blank?的组合,更简洁 if params[:attachment].present? uploaded_file = params[:attachment] @file_name = uploaded_file.original_filename # 直接读取上传文件的内容 @file_content = uploaded_file.read end respond_to do |format| if @study_request.update(study_request_params) format.html { redirect_to study_requests_path, notice: 'Answer Sended.' } format.json { render :show, status: :ok, location: @study_request } enviar() else format.html { render :edit } format.json { render json: @study_request.errors, status: :unprocessable_entity } end end end def enviar # 直接把文件内容传给邮件器 StudyRequestMailer.new_analysis(@study_request, @file_name, @file_content).deliver_now end
2. 修改邮件器代码
不需要再读取文件,直接用控制器传过来的文件内容添加附件:
class StudyRequestMailer < ApplicationMailer def new_analysis(study_request, file_name, file_content) @studyRequest = study_request if file_content.present? attachments[file_name] = file_content end mail to: @studyRequest.email, subject: "Answer" end end
3. 额外优化(可选但推荐)
- 限制上传文件类型:在表单的文件输入框里添加
accept属性,只允许用户选择PDF文件:<%= file_field_tag :attachment, class: "form-control btn-metis-7 btn-file", accept: 'application/pdf' %> - 添加文件类型验证:在控制器里验证上传的是PDF,避免非PDF文件导致的后续问题:
if params[:attachment].present? uploaded_file = params[:attachment] unless uploaded_file.content_type == 'application/pdf' format.html { render :edit, alert: 'Only PDF files are allowed' } return end @file_name = uploaded_file.original_filename @file_content = uploaded_file.read end
这样修改后,邮件里的PDF附件内容是完整且正确的,Gmail就能正常加载了。
内容的提问来源于stack exchange,提问作者Mauri Rios
相关产品推荐
相关产品推荐

