我正在开发一个允许用户向另一个人发送电子邮件的电子邮件系统。我希望用户能够做的是能够指定他们发送电子邮件的电子邮件地址 - 所以,当电子邮件从我已编码为development.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: 'myemail@emailaddress.com', #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: 'no-reply@example.com'
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 :(得分:0)
根据您的要求,您无需更改邮件设置。请按以下步骤操作:
class UserMailer < ApplicationMailer
def welcome_email(email)
@url = 'http://localhost:3000/users/login'
@email = email
mail(from:"example@example.com",to: @email.to_address, cc: @email.cc_address, bcc: @email.bcc_address, subject: @email.title)
end
end
代替from
,您需要将用户的电子邮件发送到您想要发送邮件的帐户。