I am developing an email system that allows a user to send an email to another person. What I want a user to be able to do is be able to specify the email address that they send the email from - so at the minute an email gets sent from a generic email address that I have coded into development.rb
as per below:
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
So, depending on what a user enters on the form I want the development.rb
to change so that the email can be sent from the user's account. I'm assuming that some kind of instance variable is necessary, but is this possible?
My form is as follows:
<%= 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 %>
My controller has the following:
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
And my Mailer is:
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
But is it possible for a user to enter their email details, and the system then use these in the development.rb
file to send out the email?
Aucun commentaire:
Enregistrer un commentaire