[英]Whats the best practice for creating and sending an email attachment using Rails API
[英]Sending email with attachment using Rails 4 and Carrierwave
我有一個帶有載波處理文件上傳功能的Rails 4應用程序。 它已正確安裝並且文件已正確存儲。 但是,我無法使用Activemailer將它們作為附件發送。 這是我正在使用的郵件程序:
def trigger_email(send_mail, contact, user)
@mailer = send_mail
@email = contact.email
@user = User.find(user)
attachments[@mailer.attachment.original_filename] = File.read(@mailer.attachment.current_path)
mail(to: @email, subject: @mailer.subject, from: @user.email)
end
我在這里想念什么?
我找到了適合我的解決方案(rails 4.2,ruby 2.2.0,development env,本地存儲)
def send_email email, whatever
attachments[whatever.attachment.file.filename.to_s] = whatever.attachment.read
mail(to: email, subject: whatever.subject)
end
請記住同時添加兩種電子郵件類型(.text.erb和.html.erb)
希望能解決您的問題
在我的Mailer
像這樣的東西就像一個護身符
all_documents.each do |document|
filename = document.pdf_file.file.filename
attachments[filename] = document.pdf_file.read
puts "attached!"
end
在your carrierwave uploader model
中查找file.filename
以及read
屬性,在我的情況下,我的上傳器的列名是pdf_file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.