Rails 3:发送带有附件的邮件-首次发送后损坏的文件 [英] Rails 3: Sending Mail with Attachment - corrupted file after first send

查看:101
本文介绍了Rails 3:发送带有附件的邮件-首次发送后损坏的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个真正的Simpel通讯。
因此,我可以选择所需的收件人。

I want to implement a real simpel newsletter. Therefor I can select as much recipients I want.

在该新闻通讯中,我可以附加文件。这对于第一封邮件非常有用。接下来的所有邮件都有损坏的附件(1字节大小)。

To that newsletter I can attach a file. This works great for the first mail. All next mails have corrupted attachments (1 byte size).

actionmailer:

actionmailer:

def send_newsletter(recipient,subject,content,file)
    @content = content

    if file
      attachments[file.original_filename] =  {
        :content=> file.read, 
        :mime_type=> file.content_type
      }
    end

    mail(:to => recipient, :template_name => "deliver_newsletter",
             :subject => subject).deliver!
  end

应用程序控制器:

  def create
    @customers = Customer.where("CHAR_LENGTH(email) > 0")
    @recipients = params[:sent_to]
    @subject = params[:subject]
    @content = params[:content].html_safe
    @file = params[:file]

    if @recipients
      @recipients.each do |mail_recipient|
        Newsletter.send_newsletter(mail_recipient,@subject,@content,@file)
      end
    end

    respond_to do |format|
      format.html { redirect_to bills_path, notice: "everything works fine" }
    end
  end

,最后是发送新闻通讯的形式:

and finally the form for sending newsletters:

    <%= form_tag ('/newsletters'), :multipart => true do %>
            <%= t 'views.newsletter.to_recipient' %>:<br>
            <%= select_tag 'sent_to', options_from_collection_for_select(@customers, 'email', 'name'), :multiple => true, :class => 'sent_to' %><br><br>
          <%= t 'views.newsletter.subject' %>:<br>
            <%= text_field_tag 'subject' %><br><br>
          <%= t 'views.newsletter.content' %>:<br>
            <%= text_area_tag 'content', "".html_safe, :size=>"20x8" %><br><br>
<%= t 'views.newsletter.attachment' %>:<br>
<%= file_field_tag 'file' %><br><br>
          <%= submit_tag t('views.buttons.newsletter_send_now'), :class => "btn btn-primary", :disable_with => t('views.buttons.newsletter_sending') %>
    <% end %>

另一个小问题:为什么 delay_jobs gem中的 delay在这里不起作用?可以通过键入 Newsletter.delay.send_newsletter(...)来发送所有其他邮件。

another small question: why is "delay" from the "delay_jobs" gem not working here? All other mails can be sent by typing "Newsletter.delay.send_newsletter(...)"

推荐答案

这可能是因为 file.read 使您位于文件末尾,无需读取任何内容。我会在您的ApplicationController中添加 file_contents = file.read ,然后将其作为附加参数传递给每个 send_newsletter 调用,假设文件很小。

This is probably happening because file.read leaves you at the end of the file with nothing left to read. I would add a file_contents = file.read in your ApplicationController, and then pass that in as an additional parameter to each send_newsletter call, assuming the file is small.

这篇关于Rails 3:发送带有附件的邮件-首次发送后损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆