2016-04-14 21 views
1

Ich habe wirklich Probleme damit. Kann jemand sehen, was hier falsch ist? Ich kann den Mailer nicht zur Arbeit bringen ... Ich versuche ein Kontaktformular auf meiner Seite zu erstellen, so dass Benutzer mir E-Mails von der Seite senden können. Ich erstelle einen "Kontakt" mit einem Namen, einer E-Mail, einem Betreff und einem Text und sende ihn dann als E-Mail.Mailer in Schienen einrichten 0_o

Controller/contacts_controller.rb

class ContactsController < ApplicationController 
    def index 
    end 

    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(contact_params) 
    respond_to do |format| 
     if @contact.save 
     UserMailer.mail_jill(@contact).deliver 

     format.html { redirect_to(@contact, :notice => 'User was successfully created.') } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @contact.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 


    def contact_params 
    params.require(:contact).permit(:name, :email, :subject, :body) 
    end 
end 

Mailer/user_mailer.rb

class UserMailer < ActionMailer::Base 
    def mail_jill(message) 
    @name = message.name 
    @email = message.email 
    @subject = message.subject 
    @body = message.body 

    mail(to: '<my_email_address>', subject: @subject, body: @body, from: @email) 
    end 
end 

views/user_mailer/mail_jill.html.erb:

<h1>From, <%= @contact.name %></h1> 
<p> 
@contact.body 
</p> 
<p>-<%= @contact.email %></p> 

$ RAILS_ENV.rb:

ActionMailer::Base.delivery_method = :smtp 
ActionMailer::Base.sendmail_settings = { 
    :address    => "smtp.gmail.com", 
    :port     => "465", 
    :domain    => "gmail.com", 
    :user_name   => "<my_gmail_address>", 
    :password    => "<my_gmail_password>", 
    :authentication  => "plain", 
    :enable_starttls_auto => true 
} 

config/Umgebungen/

development.rb:

# mailer 
    config.active_support.deprecation = :log 
    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.perform_deliveries = true 
    config.action_mailer.default_url_options = { :host => "localhost:3000" } 
    config.action_mailer.delivery_method = :smtp 

    config.action_mailer.smtp_settings = { 
    :address    => ENV['address'], 
    :port     => ENV['port'], 
    :user_name   => ENV['gmail_username'], 
    :password    => ENV['gmail_password'], 
    :authentication  => ENV['authentication'], 
    :enable_starttls_auto => true 

Vollausgabefehler von der Kommandozeile nach einer E-Mail von Live-Website einreichen:

2016-04-15T23:40:12.932560+00:00 app[web.1]: Sent mail to [email protected] (348.5ms) 
2016-04-15T23:40:12.932577+00:00 app[web.1]: Date: Fri, 15 Apr 2016 23:40:12 +0000 
2016-04-15T23:40:12.932579+00:00 app[web.1]: From: [email protected] 
2016-04-15T23:40:12.932579+00:00 app[web.1]: To: [email protected] 
2016-04-15T23:40:12.932581+00:00 app[web.1]: Message-ID: <[email protected]27267.mail> 
2016-04-15T23:40:12.932583+00:00 app[web.1]: Subject: hi 
2016-04-15T23:40:12.932583+00:00 app[web.1]: Mime-Version: 1.0 
2016-04-15T23:40:12.932584+00:00 app[web.1]: Content-Type: text/plain; 
2016-04-15T23:40:12.932585+00:00 app[web.1]: charset=UTF-8 
2016-04-15T23:40:12.932585+00:00 app[web.1]: Content-Transfer-Encoding: 7bit 
2016-04-15T23:40:12.932586+00:00 app[web.1]: 
2016-04-15T23:40:12.932587+00:00 app[web.1]: hiiiii 
2016-04-15T23:40:12.932687+00:00 app[web.1]: Completed 500 Internal Server Error in 367ms 
2016-04-15T23:40:12.933562+00:00 app[web.1]: 
2016-04-15T23:40:12.933579+00:00 app[web.1]: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at 
2016-04-15T23:40:12.933580+00:00 app[web.1]:): 
2016-04-15T23:40:12.933581+00:00 app[web.1]: app/controllers/contacts_controller.rb:12:in `create' 
2016-04-15T23:40:12.933582+00:00 app[web.1]: 
2016-04-15T23:40:12.933583+00:00 app[web.1]: 
+0

was genau passiert, wenn Sie versuchen, die Mail zu senden? erhalten Sie einen Fehler? – sixty4bit

+0

@ sechzig4bit Ich habe die vollständige Fehlerausgabe hinzugefügt^ –

+0

Ist die Portnummer korrekt? – hypern

Antwort

2

versuchen, diese

config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'gmail.com', 
    user_name:   ENV['gmail_username'], 
    password:    ENV['gmail_password'], 
    authentication:  'plain', 
    enable_starttls_auto: true 
} 
+0

Danke! Das funktionierte lokal, aber immer noch kein Glück auf der Produktionsstätte: { –

+0

könnte Firewalls sein? –

+0

Hm, nun, ich habe meine Fehlerprotokolle aktualisiert, um zu zeigen, was passiert, wenn ich eine E-Mail von der Live-Site sende ... –

1

Habe es zur Arbeit. Danke, Zhiliang, für deine Antwort, die mich näher gebracht hat. Am Ende war es meine $ RAILS_ENV Datei, die falsch war. Ich habe das aus meinem Ordner "Umgebungen" gelöscht und die Datei "smtp_settings.rb" zum Initialisierungsordner hinzugefügt. Hier sind die Inhalte der Initialisierer/smtp_settings.rb:

ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port => 587, 
    :domain => "gmail.com", 
    :user_name => "<my_email_address>", 
    :password => "<my_gmail_password>", 
    :authentication => :plain, 
    :enable_starttls_auto => true 
}