I want to create a simple contact form where the user can send a message to a specific mail adress (mine) just by writing his name, email and message. I'm using mailForm to do it but it's not working and i don't know why, i need some help... I obviously looked previous subjects about this but it didn't help me.
Here is my code:
Model
class Contact < MailForm::Base
attribute :name, validate: true, length: { minimum: 2 }
attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
attribute :message, validate: true, length: { minimum: 10 }
attribute :nickname, captcha: true
def headers
{ subject: "My Contact Form",
to: "mypersonalemail@gmail.com",
from: %("#{name}" <#{email}>)
}
end
end
Controller
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contacts_params)
@contact.request = request
if @contact.deliver
flash.now[:notice] = 'Merci pour votre message, je reviens vers vous très prochainement !'
else
flash.now[:alert] = "Votre message n'a pas pu être envoyé, veuillez vérifier les données saisies"
render :new
end
end
private
def contacts_params
params.require(:contact).permit(:name, :email, :message)
end
end
As it's a gmail adress i have this in development.rb
config.action_mailer.default_url_options = { host: 'https://localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'mypersonalemail@gmail.com',
password: 'mypersonalpassword',
authentication: 'plain',
enable_starttls_auto: true }
Everything is like working fine, especially when i enter this in the console:
Running via Spring preloader in process 4086
Loading development environment (Rails 6.0.3.3)
irb(main):001:0> c = Contact.new(name: 'Nobody', email: 'nobody@email.com', message: 'testestest')
=> #<Contact:0x00007fe140203e00 @name="Nobody", @email="nobody@email.com", @message="testestest">
irb(main):002:0> c.valid?
=> true
irb(main):003:0> c.deliver
Rendering /home/slegrez/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mail_form-1.8.1/lib/mail_form/views/mail_form/contact.erb
Rendered /home/slegrez/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/mail_form-1.8.1/lib/mail_form/views/mail_form/contact.erb (Duration: 22.8ms | Allocations: 1843)
MailForm::Notifier#contact: processed outbound mail in 62.8ms
Delivered mail 5fc8ed66de0be_ff62b269f0d999015752@DESKTOP-DQRGQT4.mail (890.4ms)
Date: Thu, 03 Dec 2020 15:51:34 +0200
From: Nobody <nobody@email.com>
To: mypersonalemail@gmail.com
Message-ID: <5fc8ed66de0be_ff62b269f0d999015752@DESKTOP-DQRGQT4.mail>
Subject: My Contact Form
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<h4 style="text-decoration:underline">My Contact Form</h4>
<p><b>Name:</b>
Nobody</p>
<p><b>Email:</b>
nobody@email.com</p>
<p><b>Message:</b>
testestest</p>
=> true
After that, i don't get any mail, even in the spams. I mean, c.valid? is given true so i really don't understand why it's not sending anything. I checked the email adress and the password, no mistakes. I disabled gmail security on less secured applications because i received a notification from gmail (so it means that's they catched something no ?). Well, please, help :(
Did you add the
config.action_mailer.perform_deliveries = true
In your config/environments/development.rb
?
In order to rails send emails locally you need to add this in your development environment.