ROR根据帐号发送电子邮件

时间:2014-05-15 18:37:50

标签: ruby-on-rails ruby ruby-on-rails-3 email

我们正在使用Ruby 1.9和Rails 3.2我们将AS / 400用于我们的数据库。我们之前尝试过使用Active Record,因为我们的Ruby和Rails版本较旧,并且需要将它与400连接,所以它不想工作。

我们有一个在线订购网站,您必须设置一个帐户才能访问。根据您设置的帐户类型,您可能必须由某人批准您的订单。即如果我是一个下落账户,我的经销商必须批准我订购的东西。它的设置方式,经销商没有得到任何类型的批准电子邮件。

我们一直在控制台中尝试下面的代码,让它运行起来。如果我们输入帐号而不是将其留空,则会返回将收到批准电子邮件的电子邮件地址列表。太棒了!如果它如下所示留空,则返回一个空白数组。有道理,我们还没有登录,所以它还不知道我们的帐号。当用户登录时,他们的帐号应自动替换为。

Contact.find_by_sql ["SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = ‘’”]

但是,当我们将该代码段添加到order.rb文件中时,它会返回错误:

wrong number of arguments (1 for 4)

显然,我们设置线路的方式并不正确:

Mailer.deliver_order_distributor_approval_email('Contact.find_by_sql (SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"')

它只是让人感到困惑......我们似乎与错误一起走了一圈。我们尝试修复它,但接下来我们只是得到了与该行有关的新错误。

deliver_order_distributor_approval_email方法位于以下 mailer.rb 文件中:

class Mailer < ActionMailer::Base

##################################################################################
##################################################################################
# NOTE: in all cases, use sanitize_email to ensure nothing goes out by accident. #
##################################################################################
##################################################################################

def order_confirmation_email(recipient_email, from_email, subject, email_details)

    recipient_email = Mailer.sanitize_email(recipient_email)

    # Get the template of the email body from the database and then perform all replacements.
    email_body = Setting.first.email_order_body
    email_details.each {|key, value| email_body.gsub!("##" + key.upcase.gsub("_"," ") + "##", value)}

    recipients      recipient_email
    from                    from_email
    subject             subject
    bcc                     Setting.first[:email_admin_to]

    part :content_type => "text/html",
    :body =>    email_body

    #f=File.open("/var/www/onlineordering.example.com/log/debugger.txt")
   #f.puts get_email = $get_email
   #f.close

end

def order_coastal_notify_email(from_email, subject, email_details)

    # Get the template of the email body from the database and then perform all replacements.
    email_body = Setting.first.email_order_coastal_notify
    email_details.each {|key, value| email_body.gsub!("##" + key.upcase.gsub("_"," ") + "##", value)}

    recipients      Setting.first[:email_order_placed_to]
    from                    from_email
    subject             subject
    bcc                     Setting.first[:email_admin_to]

    part :content_type => "text/html",
    :body =>    email_body

end

def order_distributor_approval_email(recipient_email, from_email, subject, email_details)

    recipient_email = Mailer.sanitize_email(recipient_email)

    # Get the template of the email body from the database and then perform all replacements.
    # We run the attachment and the email body through replacement tags.
    email_body = Setting.first.email_order_attachment_body
    email_attachment_body = Setting.first.email_order_attachment
    email_details.each {|key, value| email_body.gsub!("##" + key.upcase.gsub("_"," ") + "##", value)}
    email_details.each {|key, value| email_attachment_body.gsub!("##" + key.upcase.gsub("_"," ") + "##", value)}

    # If their email is blank, we'll send it to admin.
    recipients      recipient_email.blank? ? Setting.first[:email_admin_to] : recipient_email
    from                    from_email
    subject             subject
    bcc                     Setting.first[:email_order_placed_to]

    part :content_type => "text/html",
    :body =>    email_body

    attachment "application/pdf" do |a|
        a.body = WickedPdf.new.pdf_from_string(email_attachment_body)
        a.filename = "Drop Ship Approval Form.pdf"
    end

end

def order_again_reminder_email(name, recipient_email, from_email, subject)

    recipient_email = Mailer.sanitize_email(recipient_email)

    recipients recipient_email
    from       from_email
    bcc              Setting.first[:email_admin_to]
    subject    subject
    body       :content => Setting.first.email_reminder_body.gsub!(/##NAME##/, name.to_s.titleize)

end

def forgot_password_email(recipient_email, from_email, subject, password)

    recipient_email = Mailer.sanitize_email(recipient_email)

    recipients recipient_email
    from       from_email
    bcc              Setting.first[:email_admin_to]
    subject    subject
    body       :password => password

end

def register_email(recipient_email, from_email, subject, params)

    recipient_email = Mailer.sanitize_email(recipient_email)

    recipients recipient_email
    from       from_email
    bcc              Setting.first[:email_admin_to]
    subject    subject
    body       :params => params

end

private
    def self.sanitize_email(recipient_email)

        # Comma separate multiple email addresses.
        case ENV['RAILS_ENV']
        when 'production'
            recipient_email = recipient_email
        when 'development'
            recipient_email = "John Doe<john@example.com>"
        else
            # This is really the production, since they don't have a true production server
            # should resolve to "dev". 
            recipient_email = recipient_email
        end
        recipient_email
    end
end

如果order.rb文件有用,我可以附上它...它只是有点冗长,所以我没有把它包含在帖子中。如果您对如何更改mailer.deliver_order_distributor_approval_email行有任何建议,请告诉我们。我真的很感激!提前谢谢!

修改

Mailer.deliver_order_distributor_approval_email ('Contact.find_by_sql SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"'),"Coastal Pet Online Ordering<noreply@coastalpet.com>", "Order Confirmation-customer", email_details

有了这个(在经过一些调整之后),我们能够毫无错误地提交订单,但我们没有收到任何电子邮件。奇。看起来我们可能会错过一些邮件或其他东西吗?

修改 在修改了脚本建议之后,我们想出了这个......

 target_email = Contact.find_by_sql ["SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = ''"]        
Mailer.deliver_order_confirmation_email(target_email, "Coastal Pet Online Ordering<noreply@coastalpet.com>", "Order Confirmation-customer", email_details)    

它&#34;工作&#34;没有错误 - 我们可以登录并提交订单,但仍然无法收到任何电子邮件。 奇怪的是,当通过控制台运行时,该片段会返回以下错误。

ActiveRecord::StatementInvalid: 37000 (-10) [IBM][iSeries Access ODBC Driver][DB 2 UDB]SQL0010 - String constant beginning '        ' not delimited.

如果我们输入如下的帐号,它会运行并提交..但仍然没有电子邮件。它也会在控制台上返回错误。

target_email = Contact.find_by_sql ["SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 ='30153'"]                        
Mailer.deliver_order_confirmation_email(target_email, "Coastal Pet Online Ordering<noreply@coastalpet.com>", "Order Confirmation-customer", email_details)    

错误:

NameError: undefined local variable or method `email_details' for main:Object

想法?

1 个答案:

答案 0 :(得分:0)

您收到的错误消息:

wrong number of arguments (1 for 4)

表示您调用了一个需要四个参数的方法,但是您只提供了一个参数。这是您的方法调用:

Mailer.deliver_order_distributor_approval_email('Contact.find_by_sql (SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"')

如上所述,您提供了一个参数:字符串'Contact.find_by_sql (SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"'

当您调用方法Mailer.deliver_order_distributor_approval_email时,它最终会将给定的参数传递给您在order_distributor_approval_email类中定义的Mailer方法。该方法需要四个参数,但你只给它一个,因此错误。您需要提供所有四个参数才能使方法调用起作用。

<强>更新

您正在传递字符串'Contact.find_by_sql SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"'作为第一个参数,因此邮件程序正在尝试将该字符串视为电子邮件地址,这自然不起作用。大概你想在find_by_sql上实际调用Contact方法来获取真实的电子邮件地址,在这种情况下你会想要这样的东西:

target_email = Contact.find_by_sql('SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = "30153"').first
Mailer.deliver_order_distributor_approval_email(target_email, "Coastal Pet Online Ordering<noreply@coastalpet.com>", "Order Confirmation-customer", email_details)

这应执行查询并尝试将电子邮件发送到第一个结果。您需要进一步调整以发送给多个收件人或发送多封电子邮件。