2016-06-09 59 views
0

我正在开发一个电子邮件系统,允许用户发送电子邮件给其他人。我希望用户能够做的是能够指定他们发送电子邮件的电子邮件地址 - 因此,在一分钟内从我通过以下编码为development.rb的通用电子邮件地址发送电子邮件:允许用户更改开发的一部分.rb

Rails.application.configure do 

    config.cache_classes = false 

    config.eager_load = false 

    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    config.action_mailer.raise_delivery_errors = false 

    config.active_support.deprecation = :log 

    config.active_record.migration_error = :page_load 
    config.assets.debug = true 

    config.assets.digest = true 

    config.assets.raise_runtime_errors = true 

    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'example.com', 
    user_name:   '[email protected]', #replace with your username Eg. john.smith 
    password:    'mypassword', #replace with your gmail password 
    authentication:  'plain', 
    enable_starttls_auto: true } 

end 

因此,根据用户在表单上输入的内容我想要更改development.rb以便可以从用户的帐户发送电子邮件。我假设某种实例变量是必要的,但这可能吗?

我的格式如下:

<%= form_for @email do |f| %> 

    <% if @email.errors.any? %> 
    <div id="error_explanation"> 
     <h2> 
     <%= pluralize(@email.errors.count, "error") %> prohibited 
     this email from being saved: 
     </h2> 
     <ul> 
     <% @email.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <p> 
    <%= f.label :address %><br> 
    <%= f.text_field :address %> 
    </p> 
    <p> 
    <%= f.label :port %><br> 
    <%= f.text_field :port %> 
    </p> 
    <p> 
    <%= f.label :domain %><br> 
    <%= f.text_field :domain %> 
    </p> 

    <p> 
    <%= f.label :user_name %><br> 
    <%= f.text_field :user_name %> 
    </p> 

    <p> 
    <%= f.label :password %><br> 
    <%= f.text_field :password %> 
    </p> 
    <p> 
    <%= f.label :authentication %><br> 
    <%= f.text_field :authentication %> 
    </p> 
    <p> 
    <%= f.label :bcc_address %><br> 
    <%= f.text_field :bcc_address %> 
    </p> 

    <p> 
    <%= f.label :enable_starttls_auto %><br> 
    <%= f.text_field :enable_starttls_auto %> 
    </p> 
    <p> 
    <%= f.label :cc_address %><br> 
    <%= f.text_field :cc_address %> 
    </p> 
    <p> 
    <%= f.label :bcc_address %><br> 
    <%= f.text_field :bcc_address %> 
    </p> 

    <p> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </p> 

    <p> 
    <%= f.label :text %><br> 
    <%= f.text_area :text %> 
    </p> 

    <p> 
    <%= f.submit %> 
    </p> 

<% end %> 

我的控制器有以下几点:

def create 
    @email = Email.new(email_params) 
    if @email.save 
     UserMailer.welcome_email(@email).deliver 
     redirect_to @email 
    else 
     render 'new' 
    end 
end 
private 
    def email_params 
     params.require(:email).permit(:address, :port, :domain, :user_name, :password, :authentication, :enable_starttls_auto, :to_address, :cc_address, :bcc_address, :title, :text) 
    end 

而且我梅勒是:

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

    def welcome_email(email) 
    @url = 'http://localhost:3000/users/login' 
    @email = email 
    mail(to: @email.to_address, cc: @email.cc_address, bcc: @email.bcc_address, subject: @email.title) 
    end 
end 

但有可能使用户输入他们的电子邮件详细信息,系统然后在development.rb文件中使用这些文件发送电子邮件?

回答

0

根据您的要求,您不需要更改邮件程序设置。只要做到如下:

class UserMailer < ApplicationMailer 


def welcome_email(email) 
@url = 'http://localhost:3000/users/login' 
@email = email 
mail(from:"[email protected]",to: @email.to_address, cc: @email.cc_address, bcc: @email.bcc_address, subject: @email.title) 
end 
end 

在地方的from你需要把从您要发送的邮件帐户的用户的电子邮件。

+0

感谢您的回答,尽管这仍然行不通。当它收到电子邮件时说它来自'development.rb'中指定的电子邮件地址 –