I'm using Ruby 2.6.3
and Rails 5.2
. I want to make a contact form on my website, with a box for user email address, and a box for the message content. When a user fill those boxes and click on "Submit", i would like that it send me an email. No need for the user to sign up or anything.
My website is a one page website, i don't have any models, and i just have 1 controller: pages_controller.rb
.
I used the documentation on Action mailer but i didn't suceed.
Here are the steps i followed:
rails generate mailer ContactMailer
config/environments/development.rb :
config.action_mailer.default charset: 'utf-8'
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.mail.yahoo.com",
port: 465,
domain: "yahoo.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "myemailaddress@yahoo.com",
password: "********"
}
app/mailers/contact_mailer.rb :
class ContactMailer < ApplicationMailer
default from: "from@example.com"
def contact(message)
@message = message
mail(to: 'myemailaddress@yahoo.com', subject: 'Test')
end
end
app/views/contact_mailer/contact.html.erb :
<p>Hello world</p>
<%= @message %>
app/controllers/pages_controller.rb :
class PagesController < ApplicationController
def home
end
def send_contact
ContactMailer.contact(params[:message]).deliver
end
end
app/views/pages/home.html.erb :
<form action="/send_contact" method="post">
<input name="email" type="email" class="form-control mb-1" placeholder="Your email">
<textarea name="message" class="form-control" placeholder="Your message"></textarea>
<button type="submit">Submit</button>
</form>
config/routes.rb :
Rails.application.routes.draw do
root to: 'pages#home'
post "send_contact" => "pages#send_contact"
end
This is supposed to send me an email when clicking on submit. Instead, i get this error: EOFError in PagesController#send_contact end of file
So i tried with a gmail address with the right username / password and the right config:
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
user_name: ENV["USERNAME"],
password: ENV["PASSWORD"]
}
And i have this error: 535-5.7.8 Username and Password not accepted.
. I tried to allow "less secure apps" and desactivate 2FA. I also tried to generate an app specific password. Same error.
I don't understand what is going on.
EDIT :
When i printed ENV["USERNAME"]
i didn't have what i wrote in my .env
file. I had instead my first name, i don't know why. So i changed my .env
file to MAIL_USERNAME
(instead of USERNAME
) and now it's working in development, but unfortunately i get an error 500 in production, and i have no idea how to debug this.
EDIT 2 :
Problem solved. I just had to add my .env variables in heroku
Gmail used to allow simply passing your valid email and password via a Rails app in order to send emails. But they killed that functionality off, and require that you configure an App Specific Password for this purpose:
https://support.google.com/accounts/answer/185833?hl=en
I think the change came in 2018 or so, I remember that our Rails app was humming along just fine and suddenly it stopped sending messages. At that time, it wasn't widely announced by Gmail that they had made this change, if announced at all, which caught us off guard.
Create your app specific password and use those credentials. See if that starts sending emails for you. Your code looks fine.
I've never been able to successfully use a Yahoo account with a Rails app by the way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.