Ruby on Rails:为什么我不能将变量传递给邮件程序?

时间:2016-09-08 10:31:27

标签: ruby-on-rails ruby email arguments actionmailer

在我的Ruby on Rails应用程序中,我尝试使用登录用户的电子邮件帐户发送电子邮件,我尝试通过在邮件程序中设置SMTP设置并从控制器传递电子邮件地址用户名来执行此操作,如下所示:

控制器:

def create
    # Make a new email
    @email = Email.new(email_params)
    # For the email, store the user id as the id of the user logged in
    @email.user_id = session[:user_id]  
    # store the user id
    @user = session[:user_id]
    # if the email has saved in the database
    if @email.save
        email_to_name = @email.to_name
        # split the email addresses selected by the ";"
        @emails = (email_params[:to]).split("; ")
        # for each email address selected do the following
        @emails.each do |emailaddress|
            # the "to" section of the email is to one email address
            @email.to = emailaddress
            # find who to address the email to using the Contact model
            @email.to_name = address_email_to(email_to_name, @email.prefix, emailaddress)
            # find the contact the user wants to email
            contact = Contact.find_by_email(emailaddress).id
            # generate a unsubscribe token
            @unsubscribe = Rails.application.message_verifier(:unsubscribe).generate(contact)       
            # PASS THE ACCOUNT ID TO THE SMTP SETTINGS METHOD IN MY MAILER
            UserEmails.smtp_settings(@email.account_id)
            # send the email
            UserEmails.send_email(@email, @unsubscribe, @email.logo).deliver_now
        end
        # show the email
        redirect_to @email, notice: 'Email was successfully created.'
    # if not saved
    else
        # go back to the new email page
        redirect 'new'
    end
  end

邮件程序:

class UserEmails < ApplicationMailer

    if Rails.env.development?
        class <<self
          def smtp_settings(account)
            options = YAML.load_file("#{Rails.root}/config/mailers.yml")[Rails.env]['send_email']
            @@smtp_settings = {
              :address              => 'smtp.gmail.com',
              :port                 => 587,
              :domain               => 'my-domain.com',
              :authentication       => 'plain',
              # FIND THE USER-NAME IN THE ACCOUNT MODEL
              :user_name            => Account.find_by_id(account).email,
              :password             => 'my password', 
            }
          end
        end
    end

    def send_email(email, unsubscribe, logo)
        @url  = 'http://localhost:3000/users/login'
        @email = email
        @unsubscribe = unsubscribe
        @logo = logo
        mail(from: "#{@email.from_name} <#{@email.account.email}>", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
    end
end

所以我试图将帐户ID从控制器传递到邮件程序,但我收到错误:wrong number of arguments (0 for 1)

我理解这意味着smtp_settings期望接收数据并且控制器没有传递数据,但我不明白为什么我有行UserEmails.smtp_settings(@email.account_id)

有人可以帮我解决这个问题吗?

0 个答案:

没有答案