I work on Rails and Action Mailer. I created a form. When the visitors filled the form and sends it, it should trigger the sending of the form, record the form's data in my db, send an automatic email containing the form data to the email of the club chosen by the visitor, then a redirection is set and a flash message shows up to confirm that everything went fine. All is working except the email sending. I am stuck since a few days and can't seem to find why it does not work. I try to send the email through GMail and the account uses the double authentication. This is my config development code:
# Configurations pour l'environnement de développement
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '[email protected]',
password: 'abcdefghijk',
authentication: 'plain',
enable_starttls: true,
open_timeout: 5,
read_timeout: 5
}
This is my trials_controller:
def create
@trial = Trial.new(trial_params)
club_name = params[:trial][:club]
club = Club.find_by(name: club_name)
if club
@trial.club_id = club.id
if @trial.save
ClubMailer.rendezvous_email(@trial, club).deliver_now
flash[:notice] = "Your message was successfully sent! You will be contacted soon by the club."
redirect_to root_path
else
flash.now[:danger] = "An error occurred. Please try again."
render :new
end
else
flash.now[:danger] = "Club not found."
render :new
end
end
This is my mailer/club_mailer file:
class ClubMailer < ApplicationMailer
def rendezvous_email(trial, club)
@trial = trial
@club = club
begin
mail(to: @club.email, subject: "Free trial in #{club.name} club! ")
rescue => e
Rails.logger.error "Error sending email: #{e.message}"
raise e
end
end
end
Thank you Dbugger, Mike and Smathy for your replies and help. I solved the problem, it came from a credential matter (master key) finally!