2015-11-03 66 views
2

在我的控制文件:如何通過設計發送電子郵件附件?

class RegistrationsController < Devise::RegistrationsController 
    layout "devise" 
    before_action :check_url, only: [:new] 

    def create 
    attachments["logo.png"] = File.read("#{Rails.root}/public/assets/images/logo.png") 
    super 
    end 
end 

在我看來文件:

<%= image_tag attachments['logo.png'].url %> 

後比我得到這個錯誤,

未定義的局部變量或方法'附件的

+0

我要報名郵件發送到用戶並在這我想在郵件模板中添加logo圖片。 –

+0

@pulkit Agarwal這不適用於我:( –

+0

張貼您的控制器代碼在這裏 –

回答

2

覆蓋設計cr eate方法並不是很好的解決方案。你可以爲此寫回調。

class User < ActiveRecord::Base 
    # ... 

    after_create :send_admin_mail 
    def send_admin_mail 
    UserMailer.send_new_user_message(self).deliver 
    end 

    # ... 
end 

和您的用戶郵件應該像

class UserMailer < ApplicationMailer 
    default from: '[email protected]' 

    def send_new_user_message(user) 
    @user = user 
    @url = 'http://example.com/login' 
    attachments.inline['image.jpg'] = File.read('/path/to/image.jpg') 
    mail(to: @user.email, subject: 'Welcome to My Awesome Site') 
    end 
end 
+0

我實現了這一點,但我不能看到郵件模板中的圖像 –

+0

你能分享你git repo或gist,所以我可以檢查哪裏出了問題? –

+1

這對我來說工作得很好。我在我的Gmail帳戶中被封鎖了圖片附件。日Thnx –

相關問題