2011-02-09 15 views
26

मैं एक छोटा रूबी प्रोग्राम लिख रहा हूं जो डेटाबेस से रिकॉर्ड खींच लेगा और प्रतिदिन एक HTML ईमेल भेज देगा। मैं इसके लिए एक्शनमेलर 3.0.3 का उपयोग करने का प्रयास कर रहा हूं, लेकिन मैं मुद्दों पर चल रहा हूं। रेल के बाहर एक्शनमेलर का उपयोग करने के लिए मैंने अब तक की सभी खोजों को संस्करण 3 से पहले के संस्करणों पर लागू किया है। क्या कोई मुझे सही दिशा में इंगित कर सकता है कि यह कैसे करें इसे कैसे करें? यहां है जहां मैं अपने मेलर फ़ाइल पर अब तक हूं:एक्शनमेलर 3 रेल के बिना

# lib/bug_mailer.rb 
require 'action_mailer' 

ActionMailer::Base.delivery_method = :file 

class BugMailer < ActionMailer::Base 
    def daily_email 
    mail(
      :to  => "[email protected]", 
      :from => "[email protected]", 
      :subject => "testing mail" 
    ) 
    end 
end 

BugMailer.daily_email.deliver 

मैं निश्चित रूप से अपने विचार कहां रखूं, इस पर अटक गया हूं। एक्शनमेलर को बताने के लिए मैंने जो भी प्रयास किया है, जहां मेरे टेम्पलेट्स असफल हो गए हैं।

मुझे लगता है कि मुझे यह भी पूछना चाहिए कि इस कार्यक्रम को पूरा करने के लिए कोई अलग तरीका है या नहीं। असल में, मैं इस बिंदु पर खरोंच से सबकुछ कर रहा हूं। स्पष्ट रूप से रेलों को क्या भयानक बनाता है यह सम्मेलन है, इसलिए रेल के कुछ हिस्सों को अपने समय पर बर्बाद करने की कोशिश कर रहा है? एक पूर्ण उड़ा रेल ऐप बनाये बिना रेल की तरह पर्यावरण प्राप्त करने का कोई तरीका है?

उत्तर

44

कुछ गंभीर डीबगिंग के बाद, मैंने पाया कि इसे कैसे कॉन्फ़िगर किया जाए।

फ़ाइल mailer.rb

require 'action_mailer' 

ActionMailer::Base.raise_delivery_errors = true 
ActionMailer::Base.delivery_method = :smtp 
ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port  => 587, 
    :domain => "domain.com.ar", 
    :authentication => :plain, 
    :user_name  => "[email protected]", 
    :password  => "passw0rd", 
    :enable_starttls_auto => true 
    } 
ActionMailer::Base.view_paths= File.dirname(__FILE__) 

class Mailer < ActionMailer::Base 

    def daily_email 
    @var = "var" 

    mail( :to  => "[email protected]", 
      :from => "[email protected]", 
      :subject => "testing mail") do |format| 
       format.text 
       format.html 
    end 
    end 
end 

email = Mailer.daily_email 
puts email 
email.deliver 

फ़ाइल मेलर/daily_email.html.erb

<p>this is an html email</p> 
<p> and this is a variable <%= @var %> </p> 

फ़ाइल मेलर/daily_email.text.erb

this is a text email 

and this is a variable <%= @var %> 

अच्छा सवाल! इससे मुझे थोड़ा और समझने में मदद मिली कि रेल 3 कैसे काम करता है :)

+0

धन्यवाद ... कि मदद की। तो, जहां मैं आगे अटक गया हूं वह कहां है मेरे विचार। मुझे पता है कि, अगर मैं रेल ऐप बना रहा था, तो वे 'ऐप/व्यू' में रहेंगे, लेकिन मुझे नहीं पता कि मुझे अपने विचार कहां रखना चाहिए। अभी के लिए मेरा विचार इस प्रकार है: 'lib/bug_mailer/daily_email.html.erb'। कोई विचार? –

+0

हा, स्टैकओवरफ्लो में सभी उत्तरों हैं :) http://stackoverflow.com/questions/741989/actionmailer-and-ramaze – Augusto

+0

वास्तव में रेल के बाहर एक्शनमेलर की खोज में मैंने देखा कि यह पहला पोस्ट था। मैंने वहां वर्णित विधि का पालन किया, लेकिन नहीं जाना। यहां बताया गया है कि वर्तमान में मुझे सब कुछ कैसे स्थापित किया गया है। मेलर यहां है: 'bugs_email/lib/bug_mailer.rb'। दृश्य यहां है: 'bugs_email/lib/bug_mailer/daily_email.html.erb'। तो जब तक मुझे कुछ याद नहीं आ रहा है (जो संभव है!) मैं उस उदाहरण की तरह स्थापित हूँ। –

2

मुझे इसे (गैर-रेल) में काम करने में कुछ समय लगा। मुझे संदेह है कि यह सिर्फ इसलिए है क्योंकि: 'आवश्यकता => झूठी' मेरे सभी Gemfile से अधिक है, लेकिन मैं यह काम करने के लिए निम्नलिखित जोड़ने के लिए की जरूरत:

require 'action_view/record_identifier' 
require 'action_view/helpers' 
require 'action_mailer' 

ऊपर कोड के बिना, मैं undefined method 'assign_controller' के साथ एक NoMethodError हो रही है।

उसके बाद, मैं ActionMailer कॉन्फ़िगर किया गया इस प्रकार है:

ActionMailer::Base.smtp_settings = { 
    address: 'localhost', port: '25', authentication: :plain 
} 
ActionMailer::Base.default from: '[email protected]' 
ActionMailer::Base.raise_delivery_errors = true 
ActionMailer::Base.logger = Logger.new(STDOUT) 
ActionMailer::Base.logger.level = Logger::DEBUG 
ActionMailer::Base.view_paths = [ 
    File.join(File.expand_path("../../", __FILE__), 'views', 'mailers') 
    # Note that this is an Array 
] 

टेम्पलेट्स lib/<GEM_NAME>/views/mailers/<MAILER_CLASS_NAME>/<MAILER_ACTION_NAME>.erb में जाना (MAILER_ACTION_NAME अपने मेलर वर्ग है कि आप ईमेल भेजने के लिए फोन की जनता उदाहरण विधि है)।

अन्त में, भूल नहीं अपने spec_helper में इस डाल करने के लिए:

ActionMailer::Base.delivery_method = :test 
संबंधित मुद्दे