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
文件中使用這些文件發送電子郵件?
感謝您的回答,儘管這仍然行不通。當它收到電子郵件時說它來自'development.rb'中指定的電子郵件地址 –